[−][src]Struct fixedvec::FixedVec
Methods
impl<'a, T> FixedVec<'a, T> where
T: 'a + Copy,
[src]
T: 'a + Copy,
pub fn new(memory: &'a mut [T]) -> Self
[src]
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());
pub fn capacity(&self) -> usize
[src]
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);
pub fn len(&self) -> usize
[src]
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);
pub fn available(&self) -> usize
[src]
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());
pub fn is_empty(&self) -> bool
[src]
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());
pub fn as_slice(&self) -> &[T]
[src]
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]);
pub fn as_mut_slice(&mut self) -> &mut [T]
[src]
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);
pub fn insert(&mut self, index: usize, element: T) -> Result<()>
[src]
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());
pub fn remove(&mut self, index: usize) -> T
[src]
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]);
pub fn push(&mut self, value: T) -> Result<()>
[src]
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());
pub fn pop(&mut self) -> Option<T>
[src]
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);
pub fn push_all(&mut self, other: &[T]) -> Result<()>
[src]
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]);
pub fn clear(&mut self)
[src]
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);
pub fn map_in_place<F>(&mut self, f: F) where
F: Fn(&mut T),
[src]
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]);
ⓘImportant traits for Iter<'a, T>pub fn iter(&self) -> Iter<T>
[src]
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); }
ⓘImportant traits for IterMut<'a, T>pub fn iter_mut(&mut self) -> IterMut<T>
[src]
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]);
pub fn swap_remove(&mut self, index: usize) -> T
[src]
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]);
pub fn resize(&mut self, new_len: usize, value: T)
[src]
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]);
pub fn retain<F>(&mut self, f: F) where
F: Fn(&T) -> bool,
[src]
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]);
pub fn get(&self, index: usize) -> Option<&T>
[src]
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));
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
[src]
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));
pub unsafe fn get_unchecked(&self, index: usize) -> &T
[src]
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));
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
[src]
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));
impl<'a, T> FixedVec<'a, T> where
T: 'a + Copy + PartialEq<T>,
[src]
T: 'a + Copy + PartialEq<T>,
pub fn dedup(&mut self)
[src]
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
impl<'a, T: Debug + 'a + Copy> Debug for FixedVec<'a, T>
[src]
impl<'a, T> PartialEq<FixedVec<'a, T>> for FixedVec<'a, T> where
T: Copy + PartialEq,
[src]
T: Copy + PartialEq,
fn eq(&self, other: &FixedVec<'a, T>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<'a, T> Eq for FixedVec<'a, T> where
T: Copy + Eq,
[src]
T: Copy + Eq,
impl<'a, T> Index<usize> for FixedVec<'a, T> where
T: Copy,
[src]
T: Copy,
impl<'a, T> IndexMut<usize> for FixedVec<'a, T> where
T: Copy,
[src]
T: Copy,
impl<'a, T> Hash for FixedVec<'a, T> where
T: Copy + Hash,
[src]
T: Copy + Hash,
fn hash<H: Hasher>(&self, state: &mut H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<'a, T: Copy> IntoIterator for &'a FixedVec<'a, T>
[src]
type Item = &'a T
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, T>fn into_iter(self) -> Iter<'a, T>
[src]
impl<'a, T: Copy> IntoIterator for &'a mut FixedVec<'a, T>
[src]
type Item = &'a mut T
The type of the elements being iterated over.
type IntoIter = IterMut<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for IterMut<'a, T>fn into_iter(self) -> IterMut<'a, T>
[src]
impl<'a, T> Extend<T> for FixedVec<'a, T> where
T: Copy,
[src]
T: Copy,
fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I)
[src]
Auto Trait Implementations
impl<'a, T> Unpin for FixedVec<'a, T>
impl<'a, T> Sync for FixedVec<'a, T> where
T: Sync,
T: Sync,
impl<'a, T> Send for FixedVec<'a, T> where
T: Send,
T: Send,
Blanket Implementations
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,