Enum tinyvec::TinyVec [−][src]
Expand description
A vector that starts inline, but can automatically move to the heap.
- Requires the
alloc
feature
A TinyVec
is either an Inline(ArrayVec
) or
Heap(Vec
). The
interface for the type as a whole is a bunch of methods that just match on
the enum variant and then call the same method on the inner vec.
Construction
Because it’s an enum, you can construct a TinyVec
simply by making an
ArrayVec
or Vec
and then putting it into the enum.
There is also a macro
let empty_tv = tiny_vec!([u8; 16]); let some_ints = tiny_vec!([i32; 4] => 1, 2, 3);
Variants
Inline(ArrayVec<A>)
Implementations
Shrinks the capacity of the vector as much as possible.
It is inlined if length is less than A::CAPACITY
.
use tinyvec::*; let mut tv = tiny_vec!([i32; 2] => 1, 2, 3); assert!(tv.is_heap()); let _ = tv.pop(); assert!(tv.is_heap()); tv.shrink_to_fit(); assert!(tv.is_inline());
Moves the content of the TinyVec to the heap, if it’s inline.
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3); assert!(tv.is_inline()); tv.move_to_the_heap(); assert!(tv.is_heap());
If TinyVec is inline, moves the content of it to the heap. Also reserves additional space.
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3); assert!(tv.is_inline()); tv.move_to_the_heap_and_reserve(32); assert!(tv.is_heap()); assert!(tv.capacity() >= 35);
Reserves additional space.
Moves to the heap if array can’t hold n
more items
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4); assert!(tv.is_inline()); tv.reserve(1); assert!(tv.is_heap()); assert!(tv.capacity() >= 5);
Reserves additional space.
Moves to the heap if array can’t hold n
more items
From Vec::reserve_exact
Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future insertions are expected.
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4); assert!(tv.is_inline()); tv.reserve_exact(1); assert!(tv.is_heap()); assert!(tv.capacity() >= 5);
Makes a new TinyVec with at least the given capacity.
If the requested capacity is less than or equal to the array capacity you get an inline vec. If it’s greater than you get a heap vec.
let t = TinyVec::<[u8; 10]>::with_capacity(5); assert!(t.is_inline()); assert!(t.capacity() >= 5); let t = TinyVec::<[u8; 10]>::with_capacity(20); assert!(t.is_heap()); assert!(t.capacity() >= 20);
Remove an element, swapping the end of the vec into its place.
Panics
- If the index is out of bounds.
Example
use tinyvec::*; let mut tv = tiny_vec!([&str; 4] => "foo", "bar", "quack", "zap"); assert_eq!(tv.swap_remove(1), "bar"); assert_eq!(tv.as_slice(), &["foo", "zap", "quack"][..]); assert_eq!(tv.swap_remove(0), "foo"); assert_eq!(tv.as_slice(), &["quack", "zap"][..]);
Remove and return the last element of the vec, if there is one.
Failure
- If the vec is empty you get
None
.
The capacity of the TinyVec
.
When not heap allocated this is fixed based on the array type. Otherwise its the result of the underlying Vec::capacity.
Reduces the vec’s length to the given value.
If the vec is already shorter than the input, nothing happens.
A mutable pointer to the backing array.
Safety
This pointer has provenance over the entire backing array/buffer.
A const pointer to the backing array.
Safety
This pointer has provenance over the entire backing array/buffer.
Walk the vec and keep only the elements that pass the predicate given.
Example
use tinyvec::*; let mut tv = tiny_vec!([i32; 10] => 1, 2, 3, 4); tv.retain(|&x| x % 2 == 0); assert_eq!(tv.as_slice(), &[2, 4][..]);
Helper for getting the mut slice.
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> TinyVecDrain<'_, A>ⓘNotable traits for TinyVecDrain<'p, A>impl<'p, A: Array> Iterator for TinyVecDrain<'p, A> type Item = A::Item;
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> TinyVecDrain<'_, A>ⓘNotable traits for TinyVecDrain<'p, A>impl<'p, A: Array> Iterator for TinyVecDrain<'p, A> type Item = A::Item;
impl<'p, A: Array> Iterator for TinyVecDrain<'p, A> type Item = A::Item;
Creates a draining iterator that removes the specified range in the vector and yields the removed items.
Note: This method has significant performance issues compared to matching on the TinyVec and then calling drain on the Inline or Heap value inside. The draining iterator has to branch on every single access. It is provided for simplicity and compatability only.
Panics
- If the start is greater than the end
- If the end is past the edge of the vec.
Example
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3); let tv2: TinyVec<[i32; 4]> = tv.drain(1..).collect(); assert_eq!(tv.as_slice(), &[1][..]); assert_eq!(tv2.as_slice(), &[2, 3][..]); tv.drain(..); assert_eq!(tv.as_slice(), &[]);
Clone each element of the slice into this vec.
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2); tv.extend_from_slice(&[3, 4]); assert_eq!(tv.as_slice(), [1, 2, 3, 4]);
Wraps up an array and uses the given length as the initial length.
Note that the From
impl for arrays assumes the full length is used.
Panics
The length must be less than or equal to the capacity of the array.
Place an element onto the end of the vec.
Panics
- If the length of the vec would overflow the capacity.
use tinyvec::*; let mut tv = tiny_vec!([i32; 10] => 1, 2, 3); tv.push(4); assert_eq!(tv.as_slice(), &[1, 2, 3, 4]);
Resize the vec to the new length.
If it needs to be longer, it’s filled with clones of the provided value. If it needs to be shorter, it’s truncated.
Example
use tinyvec::*; let mut tv = tiny_vec!([&str; 10] => "hello"); tv.resize(3, "world"); assert_eq!(tv.as_slice(), &["hello", "world", "world"][..]); let mut tv = tiny_vec!([i32; 10] => 1, 2, 3, 4); tv.resize(2, 0); assert_eq!(tv.as_slice(), &[1, 2][..]);
Resize the vec to the new length.
If it needs to be longer, it’s filled with repeated calls to the provided function. If it needs to be shorter, it’s truncated.
Example
use tinyvec::*; let mut tv = tiny_vec!([i32; 3] => 1, 2, 3); tv.resize_with(5, Default::default); assert_eq!(tv.as_slice(), &[1, 2, 3, 0, 0][..]); let mut tv = tiny_vec!([i32; 2]); let mut p = 1; tv.resize_with(4, || { p *= 2; p }); assert_eq!(tv.as_slice(), &[2, 4, 8, 16][..]);
pub fn splice<R, I>(
&mut self,
range: R,
replacement: I
) -> TinyVecSplice<'_, A, Fuse<I::IntoIter>>ⓘNotable traits for TinyVecSplice<'p, A, I>impl<'p, A, I> Iterator for TinyVecSplice<'p, A, I> where
A: Array,
I: Iterator<Item = A::Item>, type Item = A::Item;
where
R: RangeBounds<usize>,
I: IntoIterator<Item = A::Item>,
pub fn splice<R, I>(
&mut self,
range: R,
replacement: I
) -> TinyVecSplice<'_, A, Fuse<I::IntoIter>>ⓘNotable traits for TinyVecSplice<'p, A, I>impl<'p, A, I> Iterator for TinyVecSplice<'p, A, I> where
A: Array,
I: Iterator<Item = A::Item>, type Item = A::Item;
where
R: RangeBounds<usize>,
I: IntoIterator<Item = A::Item>,
impl<'p, A, I> Iterator for TinyVecSplice<'p, A, I> where
A: Array,
I: Iterator<Item = A::Item>, type Item = A::Item;
Creates a splicing iterator that removes the specified range in the vector, yields the removed items, and replaces them with elements from the provided iterator.
splice
fuses the provided iterator, so elements after the first None
are ignored.
Panics
- If the start is greater than the end.
- If the end is past the edge of the vec.
- If the provided iterator panics.
Example
use tinyvec::*; let mut tv = tiny_vec!([i32; 4] => 1, 2, 3); let tv2: TinyVec<[i32; 4]> = tv.splice(1.., 4..=6).collect(); assert_eq!(tv.as_slice(), &[1, 4, 5, 6][..]); assert_eq!(tv2.as_slice(), &[2, 3][..]); tv.splice(.., None); assert_eq!(tv.as_slice(), &[]);
Wraps an array, using the given length as the starting length.
If you want to use the whole length of the array, you can just use the
From
impl.
Failure
If the given length is greater than the capacity of the array this will
error, and you’ll get the array back in the Err
.
Trait Implementations
Mutably borrows from an owned value. Read more
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more