Add try_with_capacity fallible constructor - #500
Closed
Jorge-Polanco-Roque wants to merge 1 commit into
Closed
Conversation
Fallible analogue to with_capacity, mirroring the existing try_reserve path (uses try_grow, returns Result<Self, CollectionAllocErr>). Fixes servo#416 Signed-off-by: Jorge Polanco <55784702+Jorge-Polanco-Roque@users.noreply.github.com>
alejandro-vaz
self-requested a review
August 29, 2026 23:46
alejandro-vaz
requested changes
Aug 29, 2026
Comment on lines
+852
to
+864
| /// Constructs a new, empty `SmallVec` with at least the specified capacity, | ||
| /// returning an error if the allocation fails. | ||
| /// | ||
| /// This is the fallible version of [`with_capacity`](Self::with_capacity). | ||
| #[inline] | ||
| pub fn try_with_capacity(capacity: usize) -> Result<Self, CollectionAllocErr> { | ||
| let mut this = Self::new(); | ||
| if capacity > Self::inline_size() { | ||
| this.try_grow(capacity)?; | ||
| } | ||
| Ok(this) | ||
| } | ||
|
|
Collaborator
There was a problem hiding this comment.
this adds unnecessary overhead
the point of try_with_capacity is that it simplifies try_grow by realizing what its initial state is
for example, here try_with_capacity will call try_grow which will check things like "is the instance spilled??" "what is its length??"
all that overhead can be removed
Collaborator
|
also, AI contributions are not allowed in any @servo repository, I'm closing this PR https://book.servo.org/contributing/getting-started.html#ai-contributions please read it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #416.
Adds
try_with_capacity, the fallible analogue towith_capacity. It mirrors the existingtry_reservepath: usestry_growand returnsResult<Self, CollectionAllocErr>.I kept it ungated, matching
try_reserve/try_grow(also ungated). You mentioned it might live behind a feature gate — happy to move it if you'd prefer; just let me know which feature.Test added in
tests/main.rscovering the inline and spilled cases.