pub struct FixedVec<'a, T: 'a + Copy> { /* private fields */ }
Implementations§
Source§impl<'a, T> FixedVec<'a, T>where
T: 'a + Copy,
impl<'a, T> FixedVec<'a, T>where
T: 'a + Copy,
Sourcepub fn new(memory: &'a mut [T]) -> Self
pub fn new(memory: &'a mut [T]) -> Self
Create a new FixedVec
from the provided slice, in the process taking
ownership of the slice.
§Example
let mut space = alloc_stack!([u8; 16]);
let vec = FixedVec::new(&mut space);
assert_eq!(vec.capacity(), 16);
assert_eq!(vec.len(), 0);
assert_eq!(&[] as &[u8], vec.as_slice());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the vector.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
assert_eq!(vec.capacity(), 16);
vec.push(1).unwrap();
assert_eq!(vec.capacity(), 16);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the vector. This will always be
less than or equal to the capacity()
.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
vec.push(1).unwrap();
vec.push(2).unwrap();
assert_eq!(vec.len(), 2);
Sourcepub fn available(&self) -> usize
pub fn available(&self) -> usize
Returns the number of available elements in the vector. Adding more than this number of elements (without removing some elements) will cause further calls to element-adding functions to fail.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
assert_eq!(vec.available(), 16);
vec.push(1).unwrap();
assert_eq!(vec.available(), 15);
assert_eq!(vec.available(), vec.capacity() - vec.len());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the vector contains no elements.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
assert!(vec.is_empty());
vec.push(1);
assert!(!vec.is_empty());
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Extracts a slice containing the entire vector.
Equivalent to &s[..]
.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3, 4]).unwrap();
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Extracts a mutable slice of the entire vector.
Equivalent to &mut s[..]
.
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
vec.push(1).unwrap();
let mut slice = vec.as_mut_slice();
slice[0] = 2;
assert_eq!(slice[0], 2);
Sourcepub fn insert(&mut self, index: usize, element: T) -> Result<()>
pub fn insert(&mut self, index: usize, element: T) -> Result<()>
Inserts an element at position index
within the vector, shifting all
elements after position i
one position to the right.
§Panics
Panics if index
is greater than the vector’s length.
§Example
let mut space = alloc_stack!([u8; 5]);
let mut vec = FixedVec::new(&mut space);
// Inserting in the middle moves elements to the right
vec.push_all(&[1, 2, 3]).unwrap();
vec.insert(1, 15).unwrap();
assert_eq!(vec.as_slice(), &[1, 15, 2, 3]);
// Can also insert at the end of the vector
vec.insert(4, 16).unwrap();
assert_eq!(vec.as_slice(), &[1, 15, 2, 3, 16]);
// Cannot insert if there is not enough capacity
assert!(vec.insert(2, 17).is_err());
Sourcepub fn remove(&mut self, index: usize) -> T
pub fn remove(&mut self, index: usize) -> T
Removes and returns the element at position index
within the vector,
shifting all elements after position index
one position to the left.
§Panics
Panics if index
is out of bounds.
§Examples
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
// Remove element from the middle
vec.push_all(&[1, 2, 3]).unwrap();
assert_eq!(vec.remove(1), 2);
assert_eq!(vec.as_slice(), &[1, 3]);
// Remove element from the end
assert_eq!(vec.remove(1), 3);
assert_eq!(vec.as_slice(), &[1]);
Sourcepub fn push(&mut self, value: T) -> Result<()>
pub fn push(&mut self, value: T) -> Result<()>
Appends an element to the back of the vector.
§Example
let mut space = alloc_stack!([u8; 3]);
let mut vec = FixedVec::new(&mut space);
// Pushing appends to the end of the vector
vec.push(1).unwrap();
vec.push(2).unwrap();
vec.push(3).unwrap();
assert_eq!(vec.as_slice(), &[1, 2, 3]);
// Attempting to push a full vector results in an error
assert!(vec.push(4).is_err());
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from the vector and returns it, or None
if
the vector is empty
§Example
let mut space = alloc_stack!([u8; 16]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2]).unwrap();
assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.pop(), Some(1));
assert_eq!(vec.pop(), None);
Sourcepub fn push_all(&mut self, other: &[T]) -> Result<()>
pub fn push_all(&mut self, other: &[T]) -> Result<()>
Copies all elements from slice other
to this vector.
§Example
let mut space = alloc_stack!([u8; 5]);
let mut vec = FixedVec::new(&mut space);
// All elements are pushed to vector
vec.push_all(&[1, 2, 3, 4]).unwrap();
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
// If there is insufficient space, NO values are pushed
assert!(vec.push_all(&[5, 6, 7]).is_err());
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the vector, removing all values.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3]).unwrap();
assert_eq!(vec.len(), 3);
vec.clear();
assert_eq!(vec.len(), 0);
Sourcepub fn map_in_place<F>(&mut self, f: F)
pub fn map_in_place<F>(&mut self, f: F)
Applies the function f
to all elements in the vector, mutating the
vector in place.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3]).unwrap();
vec.map_in_place(|x: &mut u8| { *x *= 2 });
assert_eq!(vec.as_slice(), &[2, 4, 6]);
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Provides a forward iterator.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3]).unwrap();
{
let mut iter = vec.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), None);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Provides a mutable forward iterator.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3]).unwrap();
{
let mut iter = vec.iter_mut();
let mut x = iter.next().unwrap();
*x = 5;
}
assert_eq!(vec.as_slice(), &[5, 2, 3]);
Sourcepub fn swap_remove(&mut self, index: usize) -> T
pub fn swap_remove(&mut self, index: usize) -> T
Removes an element from anywhere in the vector and returns it, replacing it with the last element.
This does not preserve ordering, but is O(1)
§Panics
Panics if index
is out of bounds
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[0, 1, 2, 3]).unwrap();
assert_eq!(vec.swap_remove(1), 1);
assert_eq!(vec.as_slice(), &[0, 3, 2]);
Sourcepub fn resize(&mut self, new_len: usize, value: T)
pub fn resize(&mut self, new_len: usize, value: T)
Resizes the vector in-place so that len()
is equal to new_len
.
New elements (if needed) are cloned from value
.
§Panics
Panics if new_len
is greater than capacity
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
assert_eq!(vec.len(), 0);
vec.resize(5, 255);
assert_eq!(vec.as_slice(), &[255, 255, 255, 255, 255]);
vec.resize(2, 0);
assert_eq!(vec.as_slice(), &[255, 255]);
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all elements e
such that f(&e)
returns
false. This method operates in-place, in O(N) time, and preserves the
order of the retained elements.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 3, 4]).unwrap();
vec.retain(|&x| x%2 == 0);
assert_eq!(vec.as_slice(), &[2, 4]);
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index, or None
if the
index is out of bounds.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[10, 40, 30]).unwrap();
assert_eq!(Some(&40), vec.get(1));
assert_eq!(None, vec.get(3));
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 the element at the given index, or
None
if the index is out of bounds.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[10, 40, 30]).unwrap();
{
let x = vec.get_mut(1).unwrap();
*x = 50;
}
assert_eq!(Some(&50), vec.get(1));
assert_eq!(None, vec.get(3));
Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &T
pub unsafe fn get_unchecked(&self, index: usize) -> &T
Returns a reference to the element at the given index, without doing bounds checking. Note that the result of an invalid index is undefined, and may not panic.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[10, 40, 30]).unwrap();
assert_eq!(&40, unsafe { vec.get_unchecked(1) });
// Index beyond bounds is undefined
//assert_eq!(None, vec.get(3));
Sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Returns a mutable reference to the element at the given index, without doing bounds checking. Note that the result of an invalid index is undefined, and may not panic.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[10, 40, 30]).unwrap();
{
let mut x = unsafe { vec.get_unchecked_mut(1) };
*x = 50;
}
assert_eq!(Some(&50), vec.get(1));
// Index beyond bounds is undefined
//assert_eq!(None, vec.get(3));
Source§impl<'a, T> FixedVec<'a, T>
impl<'a, T> FixedVec<'a, T>
Sourcepub fn dedup(&mut self)
pub fn dedup(&mut self)
Removes consecutive repeated elements in the vector in O(N) time.
If the vector is sorted, this removes all duplicates.
§Example
let mut space = alloc_stack!([u8; 10]);
let mut vec = FixedVec::new(&mut space);
vec.push_all(&[1, 2, 2, 3, 2]).unwrap();
vec.dedup();
assert_eq!(vec.as_slice(), &[1, 2, 3, 2]);
Trait Implementations§
Source§impl<'a, T> Extend<T> for FixedVec<'a, T>where
T: Copy,
impl<'a, T> Extend<T> for FixedVec<'a, T>where
T: Copy,
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: 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
)