pub struct Vec<T> { /* private fields */ }Expand description
A concurrent, in-place growable vector.
This type behaves similarly to the standard library Vec except that it is guaranteed to never
reallocate, and as such can support concurrent reads while also supporting growth. The vector
grows similarly to the standard library vector, but instead of reallocating, it commits more
memory.
All operations are lock-free. In order to support this, each element consists of a T and an
AtomicBool used to denote whether the element has been initialized. That means that there is
a memory overhead equal to align_of::<T>(). You can avoid this overhead if you have a byte of
memory to spare in your T or by packing the bit into an existing atomic field in T by using
RawVec.
If you don’t specify a [growth strategy], exponential growth with a growth factor of 2 is used,
which is the same strategy that the standard library Vec uses.
Implementations§
Source§impl Vec<()>
impl Vec<()>
Sourcepub const fn builder(max_capacity: usize) -> VecBuilder
pub const fn builder(max_capacity: usize) -> VecBuilder
Returns a builder for a new Vec.
max_capacity is the maximum capacity the vector can ever have. The vector is guaranteed
to never exceed this capacity. The capacity can be excessively huge, as none of the memory
is committed until you push elements into the vector or reserve capacity.
This function is implemented on Vec<()> so that you don’t have to import the VecBuilder
type too. The type parameter has no relation to the built Vec.
Source§impl<T> Vec<T>
impl<T> Vec<T>
Sourcepub fn new(max_capacity: usize) -> Self
pub fn new(max_capacity: usize) -> Self
Creates a new Vec.
max_capacity is the maximum capacity the vector can ever have. The vector is guaranteed
to never exceed this capacity. The capacity can be excessively huge, as none of the memory
is committed until you push elements into the vector or reserve capacity.
See the builder function for more creation options.
§Panics
- Panics if the
max_capacitywould exceedisize::MAXbytes. - Panics if reserving the allocation returns an error.
Sourcepub const fn dangling() -> Self
pub const fn dangling() -> Self
Creates a dangling Vec.
This is useful as a placeholder value to defer allocation until later or if no allocation is needed.
Sourcepub fn max_capacity(&self) -> usize
pub fn max_capacity(&self) -> usize
Returns the maximum capacity that was used when creating the Vec.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of elements the vector can hold without committing more memory.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the vector.
This number may exceed capacity, doesn’t correspond to the number of initialized
elements, and also doesn’t synchronize with setting the capacity.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the vector contains no elements.
This counts elements that failed to be pushed due to an error.
Sourcepub fn push(&self, value: T) -> usize
pub fn push(&self, value: T) -> usize
Appends an element to the end of the vector. Returns the index of the inserted element.
Sourcepub fn push_with(&self, f: impl FnOnce(usize) -> T) -> usize
pub fn push_with(&self, f: impl FnOnce(usize) -> T) -> usize
Appends an element to the end of the vector.
f is called with the index of the element and the result is written to the element.
Returns the index of the element.
Sourcepub fn push_mut(&mut self, value: T) -> usize
pub fn push_mut(&mut self, value: T) -> usize
Appends an element to the end of the vector. Returns the index of the inserted element.
Sourcepub fn push_with_mut(&mut self, f: impl FnOnce(usize) -> T) -> usize
pub fn push_with_mut(&mut self, f: impl FnOnce(usize) -> T) -> usize
Appends an element to the end of the vector.
f is called with the index of the element and the result is written to the element.
Returns the index of the element.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to an element of the vector.
Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &T
pub unsafe fn get_unchecked(&self, index: usize) -> &T
Sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator that yields references to elements of the vector.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns an iterator that yields mutable references to elements of the vector.
Sourcepub fn reserve(&self, additional: usize)
pub fn reserve(&self, additional: usize)
Reserves capacity for at least additional more elements.
Not to be confused with reserving virtual memory; this method is named so as to match the standard library vector.
The new capacity is at least self.len() + additional. If this capacity is below the one
calculated using the growth strategy, the latter is used. Does nothing if the capacity is
already sufficient.
§Panics
- Panics if
self.len() + additionalwould exceedself.max_capacity(). - Panics if committing the new capacity fails.
Sourcepub fn reserve_exact(&self, additional: usize)
pub fn reserve_exact(&self, additional: usize)
Reserves the minimum capacity required for additional more elements.
Not to be confused with reserving virtual memory; this method is named so as to match the standard library vector.
The new capacity is at least self.len() + additional. The growth strategy is ignored,
but the new capacity can still be greater due to the alignment to the page size. Does
nothing if the capacity is already sufficient.
§Panics
- Panics if
self.len() + additionalwould exceedself.max_capacity(). - Panics if committing the new capacity fails.
Sourcepub fn try_reserve(&self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements, returning an error on
failure.
Not to be confused with reserving virtual memory; this method is named so as to match the standard library vector.
The new capacity is at least self.len() + additional. If this capacity is below the one
calculated using the growth strategy, the latter is used. Does nothing if the capacity is
already sufficient.
§Errors
- Returns an error if
self.len() + additionalwould exceedself.max_capacity(). - Returns an error if committing the new capacity fails.
Sourcepub fn try_reserve_exact(
&self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &self, additional: usize, ) -> Result<(), TryReserveError>
Tries to reserve the minimum capacity required for additional more elements, returning an
error on failure.
Not to be confused with reserving virtual memory; this method is named so as to match the standard library vector.
The new capacity is at least self.len() + additional. The growth strategy is ignored,
but the new capacity can still be greater due to the alignment to the page size. Does
nothing if the capacity is already sufficient.
§Errors
- Returns an error if
self.len() + additionalwould exceedself.max_capacity(). - Returns an error if committing the new capacity fails.
Trait Implementations§
Source§impl<'a, T: Copy> Extend<&'a T> for Vec<T>
impl<'a, T: Copy> Extend<&'a T> for Vec<T>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T> Extend<T> for Vec<T>
impl<T> Extend<T> for Vec<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, T> IntoIterator for &'a Vec<T>
impl<'a, T> IntoIterator for &'a Vec<T>
Source§impl<'a, T> IntoIterator for &'a mut Vec<T>
impl<'a, T> IntoIterator for &'a mut Vec<T>
Source§impl<T> IntoIterator for Vec<T>
impl<T> IntoIterator for Vec<T>
Source§impl<T: Ord> Ord for Vec<T>
impl<T: Ord> Ord for Vec<T>
Source§impl<T: PartialEq<U> + Clone, U> PartialEq<Vec<U>> for Cow<'_, [T]>
Available on crate feature alloc only.
impl<T: PartialEq<U> + Clone, U> PartialEq<Vec<U>> for Cow<'_, [T]>
alloc only.