[][src]Struct one_way_slot_map::SlotMap

pub struct SlotMap<K, P, T> where
    K: SlotMapKey<P>, 
{ /* fields omitted */ }

Implementation of a slot map that limits the restrictions on slotted keys and values by preventing retrieval of original values without explicit replacement

Implementations

impl<K, P, T> SlotMap<K, P, T> where
    K: SlotMapKey<P>, 
[src]

pub fn new() -> SlotMap<K, P, T>[src]

Create a new default simple slot map

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

Get the number of items in the slot map

let mut map = SlotMap::<TestKey,(),usize>::new();

let _ = map.insert((),10);
let _ = map.insert((),42);

assert_eq!(2, map.len());

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

Tells if this map is empty

let mut map = SlotMap::<TestKey,(),usize>::new();

assert_eq!(true, map.is_empty());

let _ = map.insert((),10);
let _ = map.insert((),42);

assert_eq!(false, map.is_empty());

pub fn insert(&mut self, pointer: P, value: T) -> K[src]

insert the given item into the slot map and return its key

define_key_type!(TestKey<String>);
let mut map = SlotMap::<TestKey,String,usize>::new();

let key = map.insert("My Key".to_owned(), 10);
assert_eq!("My Key", key.pointer);
assert_eq!(&SlotMapKeyData::from(0), key.borrow());

pub fn get(&self, key: &K) -> Option<&T>[src]

Get a reference to the item in the map that corresponds to the given key if it exists

define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();

let key = map.insert((), "Hello!");

assert_eq!(Some(&"Hello!"), map.get(&key));

// Create a key that won't be in the map. This is non-ergonomic because
// it's not really a use case we expect,
let fake_key = TestKey::from(((), SlotMapKeyData::from(1u64)));

assert_eq!(None, map.get(&fake_key));

pub fn get_unbounded(&self, key: &impl Borrow<SlotMapKeyData>) -> Option<&T>[src]

Same as get method, but doesn't restrict input key to the type bound to this map. This method isn't unsafe; it just doesn't prevent you from getting data with a key of the wrong type

pub fn get_mut(&mut self, key: &K) -> Option<&mut T>[src]

Get a mutable reference to the item in the map that corresponds to the given key if it exists

define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();

let key = map.insert((), "Hello!");

{
    if let Some(item) = map.get_mut(&key) {
        *item = "World?";
    }
}
assert_eq!(Some(&"World?"), map.get(&key));

// Create a key that won't be in the map. This is non-ergonomic because
// it's not really a use case we expect,
let fake_key = TestKey::from(((), SlotMapKeyData::from(1u64)));

assert_eq!(None, map.get_mut(&fake_key));

pub fn get_mut_unbounded(
    &mut self,
    key: &impl Borrow<SlotMapKeyData>
) -> Option<&mut T>
[src]

Same as get_mut method, but doesn't restrict input key to the type bound to this map. This method isn't unsafe; it just doesn't prevent you from writing data with a key of the wrong type

pub fn remove(&mut self, key: &K) -> Option<&mut T>[src]

Remove the item at the given index and return a mutable ref to the item removed if there was one

let mut map = SlotMap::<TestKey,(),&'static str>::new();

let key = map.insert((), "Hello!");

assert!(map.get(&key).is_some());

assert_eq!(Some(&mut "Hello!"), map.remove(&key));

assert_eq!(None, map.get(&key));

pub fn remove_unbounded(
    &mut self,
    key: &impl Borrow<SlotMapKeyData>
) -> Option<&mut T>
[src]

Same as remove method, but doesn't restrict input key to the type bound to this map. This method isn't unsafe; it just doesn't prevent you from writing data with a key of the wrong type

pub fn contains_key(&self, key: &K) -> bool[src]

Check to see if the given key is still valid in this map

define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();

let key = map.insert((), "Hello!");

assert!(map.contains_key(&key));

// Create a key that won't be in the map. This is non-ergonomic because
// it's not really a use case we expect,
let fake_key = TestKey::from(((), SlotMapKeyData::from(1u64)));

assert!(!map.contains_key(&fake_key));

pub fn contains_key_unbounded(&self, key: &impl Borrow<SlotMapKeyData>) -> bool[src]

Same as contains_key method, but doesn't restrict input key to the type bound to this map. This method isn't unsafe; it just doesn't prevent you from getting data with a key of the wrong type

pub fn drain(&mut self) -> impl Iterator<Item = &mut T>[src]

Remove all items from this map and process them one-by-one

pub fn clear(&mut self)[src]

Clears all the values in the slot map. This can be a memory intensive operation because we will have to write information for every non-empty slot into the queue of slots that can now be used

pub fn iter(&self) -> impl Iterator<Item = &T>[src]

Create an iterator over all items in the items in the map

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>[src]

Construct an iterator over all the values in the slot map as mutable references

pub fn map<F, R>(&self, mapper: F) -> SlotMap<K, P, R> where
    F: FnMut(&T) -> R, 
[src]

Create a new map that has the same struture as this one, but with the values mapped with the given closure

Trait Implementations

impl<K, P, T> Clone for SlotMap<K, P, T> where
    K: SlotMapKey<P>,
    T: Clone
[src]

impl<K, P, T> Debug for SlotMap<K, P, T> where
    T: Debug,
    K: SlotMapKey<P>, 
[src]

impl<K, P, T> Default for SlotMap<K, P, T> where
    K: SlotMapKey<P>, 
[src]

Auto Trait Implementations

impl<K, P, T> RefUnwindSafe for SlotMap<K, P, T> where
    K: RefUnwindSafe,
    P: RefUnwindSafe,
    T: RefUnwindSafe

impl<K, P, T> Send for SlotMap<K, P, T> where
    K: Send,
    P: Send,
    T: Send

impl<K, P, T> Sync for SlotMap<K, P, T> where
    K: Sync,
    P: Sync,
    T: Sync

impl<K, P, T> Unpin for SlotMap<K, P, T> where
    K: Unpin,
    P: Unpin

impl<K, P, T> UnwindSafe for SlotMap<K, P, T> where
    K: UnwindSafe,
    P: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

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

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

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

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

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<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.