Struct stash::UniqueStash

source ·
pub struct UniqueStash<V> { /* private fields */ }
Expand description

An O(1) amortized table that does not reuse keys.

Guarantee: No two calls to put on the same UniqueStash will ever return the same Key.

An example use case is a file descriptor table.

An example use case is a session table where expired session IDs should never be re-used.

Implementations§

source§

impl<V> UniqueStash<V>

source

pub const fn new() -> Self

Constructs a new, empty UniqueStash<T>.

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

Examples
use stash::UniqueStash;

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

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty UniqueStash<T> with the specified capacity.

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::UniqueStash;

let mut stash: UniqueStash<i32> = UniqueStash::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);
source

pub fn capacity(&self) -> usize

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

Examples
use stash::UniqueStash;

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

pub fn len(&self) -> usize

The number of items in the stash.

Examples
use stash::UniqueStash;

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

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

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

Panics

Panics if the new capacity overflows usize.

Examples
use stash::UniqueStash;

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

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

Reserves the minimum capacity for exactly additional more elements to be put into the given UniqueStash<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::UniqueStash;

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

pub fn put(&mut self, value: V) -> Tag

Put a value into the stash.

Returns the index at which this value was stored.

source

pub fn extend<I>(&mut self, iter: I) -> Extend<'_, I> where I: Iterator<Item = V>,

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.

source

pub fn iter(&self) -> Iter<'_, V>

Iterate over the items in this UniqueStash<V>.

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

source

pub fn iter_mut(&mut self) -> IterMut<'_, V>

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

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

source

pub fn values(&self) -> Values<'_, V>

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

source

pub fn values_mut(&mut self) -> ValuesMut<'_, V>

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

source

pub fn into_values(self) -> IntoValues<V>

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

source

pub fn is_empty(&self) -> bool

Check if this UniqueStash<V> is empty.

Returns true if this UniqueStash<V> is empty.

source

pub fn take(&mut self, index: Tag) -> Option<V>

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

source

pub fn get(&self, index: Tag) -> Option<&V>

Get a reference to the value at index.

source

pub fn get_mut(&mut self, index: Tag) -> Option<&mut V>

Get a mutable reference to the value at index.

source

pub fn clear(&mut self)

Clear the UniqueStash.

Note: This will not cause Tags to be reused.

Trait Implementations§

source§

impl<V: Clone> Clone for UniqueStash<V>

source§

fn clone(&self) -> UniqueStash<V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<V> Debug for UniqueStash<V>where V: Debug,

source§

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

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

impl<V> Default for UniqueStash<V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<V> Index<Tag> for UniqueStash<V>

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: Tag) -> &V

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

impl<V> IndexMut<Tag> for UniqueStash<V>

source§

fn index_mut(&mut self, index: Tag) -> &mut V

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

impl<'a, V> IntoIterator for &'a UniqueStash<V>

§

type Item = (Tag, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, V> IntoIterator for &'a mut UniqueStash<V>

§

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

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<V> IntoIterator for UniqueStash<V>

§

type Item = (Tag, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<V> RefUnwindSafe for UniqueStash<V>where V: RefUnwindSafe,

§

impl<V> Send for UniqueStash<V>where V: Send,

§

impl<V> Sync for UniqueStash<V>where V: Sync,

§

impl<V> Unpin for UniqueStash<V>where V: Unpin,

§

impl<V> UnwindSafe for UniqueStash<V>where V: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.