Struct slicevec::SliceVec
[−]
[src]
pub struct SliceVec<'a, T: 'a> { /* fields omitted */ }
A Vector using a slice for backing storage (passed in at creation time).
Changes to the vector are visible in the backing storage after the SliceVec
is dropped.
A SliceVec
can be dereferenced to a truncated slice containing all elements in the SliceVec
.
The returned slice is different from the backing slice in that it only contains the first n
values, where n
is the current length of the SliceVec
. The backing slice may contain unused
"dummy" elements after the last element.
This is essentially a less ergonomic but more flexible version of the arrayvec
crate's
ArrayVec
type: You have to crate the backing storage yourself, but SliceVec
works with
arrays of any length (unlike ArrayVec
, which works with a fixed set of lengths, since Rust
doesn't (yet) have integer generics).
Methods
impl<'a, T> SliceVec<'a, T>
[src]
fn new(storage: &'a mut [T]) -> Self
Create a new SliceVec
, using the given slice as backing storage for elements.
The capacity of the vector equals the length of the slice, you have to make sure that the slice is large enough for all elements.
fn capacity(&self) -> usize
Returns the maximum number of elements that can be stored in this vector. This is equal to
the length of the backing storage passed at creation of this SliceVec
.
fn len(&self) -> usize
Returns the number of elements stored in this SliceVec
.
fn is_empty(&self) -> bool
Returns true
if the length of this vector is 0, false
otherwise.
fn is_full(&self) -> bool
Returns true
if the backing slice is completely filled.
When this is the case, all operations that insert additional elements into the SliceVec
will fail.
fn push(&mut self, elem: T) -> Result<(), T>
Tries to append an element to the end of this vector.
If the backing storage is already full, returns Err(elem)
.
fn pop_and_replace(&mut self, elem: T) -> Option<T>
Removes and returns the last elements stored inside the vector, replacing it with elem
.
If the vector is empty, returns None
and drops elem
.
fn truncate(&mut self, len: usize)
Shortens the vector to len
elements.
Excess elements are not dropped. They are kept in the backing slice.
fn clear(&mut self)
Clears the vector, removing all elements.
Equivalent to .truncate(0)
.
fn as_slice(&self) -> &[T]
Extract a slice containing the entire vector.
The returned slice will be shorter than the backing slice if the vector hasn't yet exceeded its capacity.
fn as_mut_slice(&mut self) -> &mut [T]
Extract a mutable slice containing the entire vector.
The returned slice will be shorter than the backing slice if the vector hasn't yet exceeded its capacity.
impl<'a, T: 'a + Default> SliceVec<'a, T>
[src]
fn pop(&mut self) -> Option<T>
Removes and returns the last element in this vector.
Returns None
if the vector is empty.
This operation is restricted to element types that implement Default
, since the element's
spot in the backing storage is replaced by a default value.
fn swap_remove(&mut self, index: usize) -> T
Removes and returns the element at index
and replaces it with the last element.
The last element's place in the backing slice is replaced by T
's default value.
Panics if index
is out of bounds.
fn remove(&mut self, index: usize) -> T
Removes and returns the element at index
and shifts down all elements after it.
Unlike swap_remove
, this preserves the ordering of the vector, but is O(n)
instead of
O(1)
.
Trait Implementations
impl<'a, T: Debug + 'a> Debug for SliceVec<'a, T>
[src]
impl<'a, T> Deref for SliceVec<'a, T>
[src]
type Target = [T]
The resulting type after dereferencing
fn deref(&self) -> &[T]
The method called to dereference a value
impl<'a, T> DerefMut for SliceVec<'a, T>
[src]
fn deref_mut(&mut self) -> &mut [T]
The method called to mutably dereference a value
impl<'a, T> AsRef<[T]> for SliceVec<'a, T>
[src]
fn as_ref(&self) -> &[T]
Performs the conversion.
impl<'a, T> AsMut<[T]> for SliceVec<'a, T>
[src]
fn as_mut(&mut self) -> &mut [T]
Performs the conversion.
impl<'a, T> Borrow<[T]> for SliceVec<'a, T>
[src]
impl<'a, T> BorrowMut<[T]> for SliceVec<'a, T>
[src]
fn borrow_mut(&mut self) -> &mut [T]
Mutably borrows from an owned value. Read more