[][src]Struct enum_vec::vec_u32::EnumVec

pub struct EnumVec<T: EnumLike> { /* fields omitted */ }

A vector which efficiently stores enum variants.

Methods

impl<T: EnumLike> EnumVec<T>
[src]

Returns the number of elements that can be hold without allocating new memory.

use enum_vec::vec_u32::EnumVec;

let ev = EnumVec::<bool>::with_capacity(53);
assert!(ev.capacity() >= 53);

Reserves capacity for at least additional more elements.

use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<Option<()>> = vec![None, None, None].into();
ev.reserve(100);
assert!(ev.capacity() >= 100 + 3);

let mut a: EnumVec<bool> = EnumVec::new();
a.reserve(1);
assert_eq!(a.len(), 0);
assert!(ev.storage().len() > 0);

Shrinks the capacity as much as possible.

Remove an element from an arbitrary position in O(1) time, but without preserving the ordering. This is accomplished by swapping the desired element with the last element, and then calling pop().

use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<bool> = vec![true, true, true, false, false].into();
ev.swap_remove(0);
assert_eq!(&ev.to_vec(), &[false, true, true, false]);
ev.swap_remove(1);
assert_eq!(&ev.to_vec(), &[false, false, true]);

Insert an element into an arbitrary position. This operation is very expensive, as it must shift all the elements to make space for the new one. Prefer using push().

use enum_vec::vec_u32::EnumVec;

let mut ev = EnumVec::new();
ev.insert(0, true);
assert_eq!(ev.to_vec(), vec![true]);
ev.insert(0, false);
assert_eq!(ev.to_vec(), vec![false, true]);
ev.insert(1, true);
assert_eq!(ev.to_vec(), vec![false, true, true]);
ev.insert(1, false);
assert_eq!(ev.to_vec(), vec![false, false, true, true]);

let mut ev: EnumVec<_> = vec![false; 127].into();
assert_eq!(ev.len(), 127);
ev.push(true);
assert_eq!(ev.len(), 127 + 1);
ev.push(true);
assert_eq!(ev.len(), 127 + 2);
ev.insert(0, true);
ev.insert(0, true);
assert_eq!((ev.get(0).unwrap(), ev.get(1).unwrap()), (true, true));
assert_eq!(ev.len(), 127 + 4);

Remove an element from an arbitrary position. This operation is very expensive, as it must shift all the elements to fill the hole. When preserving the order is not important, consider using swap_remove().

use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<_> = vec![None; 129].into();
let item = Some([true, false, true, false]);
ev.insert(0, item);
assert_eq!(ev.remove(0), item);
ev.insert(127, item);
assert_eq!(ev.remove(127), item);
ev.insert(127, item);
ev.insert(128, item);
assert_eq!(ev.remove(127), item);
assert_eq!(ev.remove(127), item);
assert_eq!(ev.remove(127), None);
assert_eq!(ev.remove(127), None);
assert_eq!(ev.len(), 127);

Retains only the elements specified by the predicate

use enum_vec::vec_u32::EnumVec;

let mut v: EnumVec<(bool, bool)> = vec![(true, true), (false, false), (true, false),
    (false, true)].into();
v.retain(|x| x.0 == true);
let a = v.to_vec();
assert_eq!(&a, &[(true, true), (true, false)]);

Push an element to the end of the vector.

use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<_> = vec![false; 127].into();
ev.push(true);

Appends all the elements from other into self, leaving other empty. This can be more efficient than using extend when the internal storage is block-aligned. For example when each element is 4-bits wide and the storage is a u32, it is block-aligned when it has 8*k elements. Also, it doesn't have to map all the elements from T to discr.

use enum_vec::vec_u32::EnumVec;

let mut a: EnumVec<_> = vec![None, None, Some(())].into();
let mut b = a.clone();
b.push(None);
a.append(&mut b);
assert_eq!(a.get(6), Some(None));
assert_eq!(a.len(), 3+4);
assert_eq!(b.len(), 0);

Sets the length to zero, removing all the elements.

use enum_vec::vec_u32::EnumVec;

let mut ev = EnumVec::new();
ev.push(Some(false));
assert_eq!(ev.len(), 1);
ev.clear();
assert_eq!(ev.len(), 0);
assert!(ev.is_empty());

unsafe {
    ev.set_len(1);
    assert_eq!(ev.pop().unwrap(), Some(false));
}

Returns the length of the vector, the number of elements it holds.

Get the raw discriminant without bounds checking

Set the raw discriminant without bounds checking. It is assumed that the discriminant is lower than T::NUM_ELEMENTS.

Swap two elements.

Important traits for EnumVecIter<'a, T>

Apply a function to each element in place, this is a substitute to for loops:

use enum_vec::vec_u32::EnumVec;

let mut v = vec![true, false, true];
for x in v.iter_mut() {
    *x = !*x;
}

// Is equivalent to
let mut ev: EnumVec<_> = vec![true, false, true].into();
ev.for_each(|x| {
    *x = !*x;
});

assert_eq!(v, ev.to_vec());
assert_eq!(&v, &[false, true, false]);

Copies self into a plain Vec.

use enum_vec::vec_u32::EnumVec;

let mut ev = EnumVec::new();
ev.push(true);
ev.push(false);
let v = vec![true, false];
assert_eq!(ev.to_vec(), v);

#[macro_use] extern crate enum_vec;
use enum_vec::vec_u32::EnumVec;

let ev = enum_vec![true, false, false, true];
assert_eq!(ev.len(), 4);
let mut a = enum_vec![false; 8];
a.extend(ev);

Access the internal storage

use enum_vec::vec_u32::EnumVec;

let ev: EnumVec<_> = vec![true; 100].into();
assert_eq!(ev.len(), 100);
assert!(ev.storage().len() > 0);

Access and modify the internal storage. This function is unsafe because shrinking the storage may lead to reading and writing uninitialized memory.

extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<_> = vec![true; 100].into();
assert_eq!(ev.len(), 100);
assert!(ev.storage().len() > 0);
assert_eq!(ev.get(0).unwrap(), true);

unsafe {
    let s = ev.storage_mut();
    // We set the first block to 0, which will set it to false:
    assert_eq!(false.to_discr(), 0);
    s[0] = 0;
}

// The number of modified elements depends on
// the storage size and the element size, but the first element
// will always be changed
assert_eq!(ev.get(0).unwrap(), false);

Check whether any of the elements is equal to x. This method uses arcane bithack magic to test many elements at once.

extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<_> = vec![true; 200].into();
assert_eq!(ev.any(true), true);
assert_eq!(ev.any(false), false);
ev.push(true);
assert_eq!(ev.any(true), true);
assert_eq!(ev.any(false), false);
ev.set(0, false);
assert_eq!(ev.any(false), true);

let mut ev: EnumVec<_> = vec![None; 200].into();
assert_eq!(ev.any(Some(false)), false);
assert_eq!(ev.any(Some(true)), false);
assert_eq!(ev.any(None), true);
ev.set(1, Some(false));
assert_eq!(ev.any(Some(false)), true);
assert_eq!(ev.any(Some(true)), false);
assert_eq!(ev.any(None), true);
ev.set(2, Some(true));
assert_eq!(ev.any(Some(false)), true);
assert_eq!(ev.any(Some(true)), true);
assert_eq!(ev.any(None), true);

Check whether all of the elements are equal to x. This method uses arcane bithack magic to test many elements at once.

extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u32::EnumVec;

let mut ev: EnumVec<_> = vec![true; 200].into();
assert_eq!(ev.all(true), true);
assert_eq!(ev.all(false), false);
ev.push(true);
assert_eq!(ev.all(true), true);
assert_eq!(ev.all(false), false);
ev.set(0, false);
assert_eq!(ev.all(true), false);
assert_eq!(ev.all(false), false);

let mut ev: EnumVec<_> = vec![None; 200].into();
assert_eq!(ev.all(Some(false)), false);
assert_eq!(ev.all(Some(true)), false);
assert_eq!(ev.all(None), true);
ev.set(1, Some(false));
assert_eq!(ev.all(Some(false)), false);
assert_eq!(ev.all(Some(true)), false);
assert_eq!(ev.all(None), false);

Trait Implementations

impl<T: Clone + EnumLike> Clone for EnumVec<T>
[src]

Performs copy-assignment from source. Read more

impl<T: EnumLike> Extend<T> for EnumVec<T>
[src]

impl<T: EnumLike> From<Vec<T>> for EnumVec<T>
[src]

impl<T: EnumLike> Into<Vec<T>> for EnumVec<T>
[src]

impl<T: EnumLike> Eq for EnumVec<T>
[src]

impl<T: EnumLike> Default for EnumVec<T>
[src]

impl<T: EnumLike> PartialEq<EnumVec<T>> for EnumVec<T>
[src]

This method tests for !=.

impl<'a, T: EnumLike> IntoIterator for &'a EnumVec<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

impl<T: EnumLike> IntoIterator for EnumVec<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

impl<T: EnumLike + Debug> Debug for EnumVec<T>
[src]

impl<T: EnumLike> Hash for EnumVec<T>
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<T: EnumLike> FromIterator<T> for EnumVec<T>
[src]

Auto Trait Implementations

impl<T> Send for EnumVec<T> where
    T: Send

impl<T> Sync for EnumVec<T> where
    T: Sync

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]