Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,21 @@ impl<T, const N: usize> SmallVec<T, N> {
}
}

#[inline]
pub fn with_capacity(capacity: usize) -> Self {
pub fn try_with_capacity(capacity: usize) -> Result<Self, CollectionAllocErr> {
let mut this = Self::new();
if capacity > Self::inline_size() {
this.grow(capacity);
if capacity > Self::inline_size() && !Self::IS_ZST {
// SAFETY: we checked all the preconditions
unsafe { this.raw.try_grow_raw(TaggedLen::new(0, false), capacity) }?;

// SAFETY: the allocation succeeded, so self.raw.heap is now active
unsafe { this.set_on_heap() };
}
this
Ok(this)
}

#[inline]
pub fn with_capacity(capacity: usize) -> Self {
infallible(Self::try_with_capacity(capacity))
}

#[inline]
Expand Down