[][src]Struct stash::stash::Stash

pub struct Stash<V, Ix = usize> { /* fields omitted */ }

An O(1) amortized table that reuses keys.

Guarantees and non-guarantees:

  1. Stash is deterministic and keys do not depend on the inserted values. This means you can update two stashes in tandem and get the same keys back. This could be useful for, e.g., primary/secondary replication.
  2. Keys will always be less than the maximum number of items that have ever been present in the Stash at any single point in time. In other words, if you never store more than n items in a Stash, the stash will only assign keys less than n. You can take advantage of this guarantee to truncate the key from a usize to some smaller type.
  3. Except the guarantees noted above, you can assume nothing about key assignment or iteration order. They can change at any time.

An example use case is a file descriptor table.

Methods

impl<V> Stash<V, usize>[src]

pub fn new() -> Self[src]

Constructs a new, empty Stash<V, usize>.

This is a convenience method. Use Stash::default for a constructor that is generic in the type of index used.

The stash will not allocate until elements are put onto it.

Examples

use stash::Stash;

let mut stash: Stash<i32> = Stash::new();

pub fn with_capacity(capacity: usize) -> Self[src]

Constructs a new, empty Stash<V, usize> with the specified capacity.

This is a convenience method. Use Stash::default for a constructor that is generic in the type of index used. In that case you can call reserve on the newly created stash to specify the capacity you need.

The stash will be able to hold exactly capacity elements without reallocating. If capacity is 0, the stash will not allocate.

It is important to note that this function does not specify the length of the returned stash , but only the capacity. (For an explanation of the difference between length and capacity, see the main Vec<T> docs in the std::vec module, 'Capacity and reallocation'.)

Examples

use stash::Stash;

let mut stash = Stash::with_capacity(10);

// The stash contains no items, even though it has capacity for more
assert_eq!(stash.len(), 0);

// These are all done without reallocating...
for i in 0i32..10 {
    let _ = stash.put(i);
}

// ...but this may make the stash reallocate
stash.put(11);

impl<V, Ix> Stash<V, Ix> where
    Ix: Index
[src]

pub fn capacity(&self) -> usize[src]

Returns the number of elements the stash can hold without reallocating.

Examples

use stash::Stash;

let stash: Stash<i32> = Stash::with_capacity(10);
assert_eq!(stash.capacity(), 10);

pub fn len(&self) -> usize[src]

The number of items in the stash.

Examples

use stash::Stash;

let mut stash = Stash::new();
assert_eq!(stash.len(), 0);
stash.put("a");
assert_eq!(stash.len(), 1);

pub fn reserve(&mut self, additional: usize)[src]

Reserves capacity for at least additional more elements to be put into the given Stash<T>. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new capacity overflows usize.

Examples

use stash::Stash;

let mut stash: Stash<i32> = Stash::new();
let t1 = stash.put(1);
stash.reserve(10);
assert!(stash.capacity() >= 11);

pub fn reserve_exact(&mut self, additional: usize)[src]

Reserves the minimum capacity for exactly additional more elements to be put into the given Stash<T>. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future puts are expected.

Panics

Panics if the new capacity overflows usize.

Examples

use stash::Stash;

let mut stash: Stash<i32> = Stash::new();
let t1 = stash.put(1);
stash.reserve_exact(10);
assert!(stash.capacity() >= 11);

pub fn next_index(&self) -> Ix[src]

Get the index that would be returned from next call to put.

Panics

Panics if the size of the Stash<V, Ix> would overflow the Ix index type.

pub fn put(&mut self, value: V) -> Ix[src]

Put a value into the stash.

Returns the index at which this value was stored.

Panics

Panics if the size of the Stash<V, Ix> would overflow the Ix index type.

Important traits for Extend<'a, I, Ix>
pub fn extend<I>(&mut self, iter: I) -> Extend<I, Ix> where
    I: Iterator<Item = V>, 
[src]

Put all items in the iterator into the stash.

Returns an iterator over the indices where the items were inserted. The items are actually inserted as the Iterator is read. If the returned Iterator is dropped, the rest of the items will be inserted all at once.

Important traits for Iter<'a, V, Ix>
pub fn iter(&self) -> Iter<V, Ix>[src]

Iterate over the items in this Stash<V>.

Returns an iterator that yields (index, &value) pairs.

Important traits for IterMut<'a, V, Ix>
pub fn iter_mut(&mut self) -> IterMut<V, Ix>[src]

Mutably iterate over the items in this Stash<V>.

Returns an iterator that yields (index, &mut value) pairs.

Important traits for Values<'a, V>
pub fn values(&self) -> Values<V>[src]

Iterate over the values in this Stash<V> by reference.

Important traits for ValuesMut<'a, V>
pub fn values_mut(&mut self) -> ValuesMut<V>[src]

Mutably iterate over the values in this Stash<V> by reference.

Important traits for IntoValues<V>
pub fn into_values(self) -> IntoValues<V>[src]

Iterate over the values in this Stash<V> by value.

pub fn is_empty(&self) -> bool[src]

Check if this Stash<V> is empty.

Returns true if this Stash<V> is empty.

pub fn take(&mut self, index: Ix) -> Option<V>[src]

Take an item from a slot (if non empty).

pub unsafe fn take_unchecked(&mut self, index: Ix) -> V[src]

Take an item from a slot (if non empty) without bounds or empty checking. So use it very carefully!

This can be safely used as long as the user does not mutate indices from put and is sure not to have taken the value associated with the given index.

pub fn get(&self, index: Ix) -> Option<&V>[src]

Get a reference to the value at index.

pub unsafe fn get_unchecked(&self, index: Ix) -> &V[src]

Get a reference to the value at index without bounds or empty checking. So use it very carefully!

This can be safely used as long as the user does not mutate indices from put and is sure not to have taken the value associated with the given index.

pub fn get_mut(&mut self, index: Ix) -> Option<&mut V>[src]

Get a mutable reference to the value at index.

pub unsafe fn get_unchecked_mut(&mut self, index: Ix) -> &mut V[src]

Get a mutable reference to the value at index without bounds or empty checking. So use it very carefully!

This can be safely used as long as the user does not mutate indices from put and is sure not to have taken the value associated with the given index.

pub fn clear(&mut self)[src]

Clear the stash. Cleared stash will give the same keys as a new stash for subsequent puts.

Trait Implementations

impl<V, Ix: Index> IntoIterator for Stash<V, Ix>[src]

type Item = (Ix, V)

The type of the elements being iterated over.

type IntoIter = IntoIter<V, Ix>

Which kind of iterator are we turning this into?

impl<'a, V, Ix: Index> IntoIterator for &'a Stash<V, Ix>[src]

type Item = (Ix, &'a V)

The type of the elements being iterated over.

type IntoIter = Iter<'a, V, Ix>

Which kind of iterator are we turning this into?

impl<'a, V, Ix: Index> IntoIterator for &'a mut Stash<V, Ix>[src]

type Item = (Ix, &'a mut V)

The type of the elements being iterated over.

type IntoIter = IterMut<'a, V, Ix>

Which kind of iterator are we turning this into?

impl<V, Ix: Index> Default for Stash<V, Ix>[src]

impl<V: Clone, Ix: Clone> Clone for Stash<V, Ix>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'a, V, Ix: Index> Index<Ix> for Stash<V, Ix>[src]

type Output = V

The returned type after indexing.

impl<'a, V, Ix: Index> IndexMut<Ix> for Stash<V, Ix>[src]

impl<V, Ix> Debug for Stash<V, Ix> where
    V: Debug,
    Ix: Debug + Index
[src]

Auto Trait Implementations

impl<V, Ix> Sync for Stash<V, Ix> where
    V: Sync

impl<V, Ix> Send for Stash<V, Ix> where
    V: Send

impl<V, Ix> Unpin for Stash<V, Ix> where
    V: Unpin

impl<V, Ix> RefUnwindSafe for Stash<V, Ix> where
    V: RefUnwindSafe

impl<V, Ix> UnwindSafe for Stash<V, Ix> where
    V: UnwindSafe

Blanket Implementations

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

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

type Owned = T

The resulting type after obtaining ownership.

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

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> From<T> for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

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

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

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