Struct FixedVec

Source
pub struct FixedVec<'a, T: 'a + Copy> { /* private fields */ }

Implementations§

Source§

impl<'a, T> FixedVec<'a, T>
where T: 'a + Copy,

Source

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());
Source

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);
Source

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);
Source

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());
Source

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());
Source

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]);
Source

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);
Source

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());
Source

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]);
Source

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());
Source

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);
Source

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]);
Source

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);
Source

pub fn map_in_place<F>(&mut self, f: F)
where F: Fn(&mut T),

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]);
Source

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);
}
Source

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]);
Source

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]);
Source

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]);
Source

pub fn retain<F>(&mut self, f: F)
where F: Fn(&T) -> bool,

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]);
Source

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));
Source

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));
Source

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));
Source

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>
where T: 'a + Copy + PartialEq<T>,

Source

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: Debug + 'a + Copy> Debug for FixedVec<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Extend<T> for FixedVec<'a, T>
where T: Copy,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, T> Hash for FixedVec<'a, T>
where T: Copy + Hash,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T> Index<usize> for FixedVec<'a, T>
where T: Copy,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T> IndexMut<usize> for FixedVec<'a, T>
where T: Copy,

Source§

fn index_mut(&mut self, index: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T: Copy> IntoIterator for &'a FixedVec<'a, T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<'a, T: Copy> IntoIterator for &'a mut FixedVec<'a, T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, T>

Creates an iterator from a value. Read more
Source§

impl<'a, T> PartialEq for FixedVec<'a, T>
where T: Copy + PartialEq,

Source§

fn eq(&self, other: &FixedVec<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T> Eq for FixedVec<'a, T>
where T: Copy + Eq,

Auto Trait Implementations§

§

impl<'a, T> Freeze for FixedVec<'a, T>

§

impl<'a, T> RefUnwindSafe for FixedVec<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for FixedVec<'a, T>
where T: Send,

§

impl<'a, T> Sync for FixedVec<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for FixedVec<'a, T>

§

impl<'a, T> !UnwindSafe for FixedVec<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.