Struct fixedvec::FixedVec [] [src]

pub struct FixedVec<'a, T: 'a + Copy> {
    // some fields omitted
}

Methods

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<'a, T> FixedVec<'a, T> where T: 'a + Copy + PartialEq<T>
[src]

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

impl<'a, T: Debug + 'a + Copy> Debug for FixedVec<'a, T>
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

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?

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

Creates an iterator from a value. Read more

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?

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

Creates an iterator from a value. Read more

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

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

Feeds this value into the state given, updating the hasher as necessary.

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

Feeds a slice of this type into the state provided.

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

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

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

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

type Output = T

The returned type after indexing

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

The method for the indexing (Foo[Bar]) operation

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

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

The method for the indexing (Foo[Bar]) operation

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

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

This method tests for self and other values to be equal, and is used by ==. Read more

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

This method tests for !=.

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