Struct one_way_slot_map::SlotMap [−][src]
#[repr(transparent)]pub struct SlotMap<K, P, T> where
K: SlotMapKey<P>, { /* fields omitted */ }
Expand description
Implementation of a slot map that limits the restrictions on slotted keys and values by preventing retrieval of original values without explicit replacement
Implementations
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());
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());
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());
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));
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
define_key_type!(TestKey<()>);
define_key_type!(OtherKey<()> : Default);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let _ = map.insert((), "Hello!");
assert_eq!(Some(&"Hello!"), map.get_unbounded(&OtherKey::default()));
// 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 = OtherKey::from(((), SlotMapKeyData::from(1u64)));
assert_eq!(None, map.get_unbounded(&fake_key));
Similar to get_unbounded, but only requires to slotmap key data
define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let _ = map.insert((), "Hello!");
assert_eq!(Some(&"Hello!"), map.get_raw(&SlotMapKeyData::default()));
// Create key data that won't be in the map. This is non-ergonomic
// because it's not really a use case we expect,
let fake_key_data = SlotMapKeyData::from(1u64);
assert_eq!(None, map.get_raw(&fake_key_data));
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));
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
define_key_type!(TestKey<()>);
define_key_type!(OtherKey<()> : Default);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let key = map.insert((), "Hello!");
{
if let Some(item) = map.get_mut_unbounded(&OtherKey::default()) {
*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));
Similar to get_unbounded_mut, but only requires to slotmap key data
define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let key = map.insert((), "Hello!");
{
if let Some(item) = map.get_mut_raw(&SlotMapKeyData::default()) {
*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_data = SlotMapKeyData::from(1u64);
assert_eq!(None, map.get_mut_raw(&fake_key_data));
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));
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
define_key_type!(TestKey<()>);
define_key_type!(OtherKey<()> : Default);
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_unbounded(&OtherKey::default()));
assert_eq!(None, map.get(&key));
Similar to remove_unbounded but only requires the slot map key data
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_raw(&SlotMapKeyData::default()));
assert_eq!(None, map.get(&key));
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));
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
define_key_type!(TestKey<()>);
define_key_type!(OtherKey<()> : Default);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let key = map.insert((), "Hello!");
assert!(map.contains_key_unbounded(&OtherKey::default()));
// 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 = OtherKey::from(((), SlotMapKeyData::from(1u64)));
assert!(!map.contains_key_unbounded(&fake_key));
Similar to contains_key_unbounded but only requires slot map key data
define_key_type!(TestKey<()>);
let mut map = SlotMap::<TestKey,(),&'static str>::new();
let key = map.insert((), "Hello!");
assert!(map.contains_key_raw(&SlotMapKeyData::default()));
// 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_data = SlotMapKeyData::from(1u64);
assert!(!map.contains_key_raw(&fake_key_data));
Remove all items from this map and process them one-by-one
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
Get an iterator over keys and values given a way to get the pointer from the stored value.
Get an iterator over keys and mutable values given a way to get the pointer from the stored value.
Create an iterator over all raw key data and values for items present in the slot map
Create an iterator over all raw key data and mutable values for items present in the slot map
Create an iterator over all items in the items in the map
Construct an iterator over all the values in the slot map as mutable references
Trait Implementations
This is here because using *const K/P in the phantom data prevent the slot map type from being automatically marked as send and sync. Using K and P directly in the phantom data would solve this problem, but according to the rust book, you can save some drop check logic (whatever that is) if you use const pointers in your phantom data
This is here because using *const K/P in the phantom data prevent the slot map type from being automatically marked as send and sync. Using K and P directly in the phantom data would solve this problem, but according to the rust book, you can save some drop check logic (whatever that is) if you use const pointers in your phantom data
Auto Trait Implementations
impl<K, P, T> RefUnwindSafe for SlotMap<K, P, T> where
K: RefUnwindSafe,
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<K, P, T> UnwindSafe for SlotMap<K, P, T> where
K: RefUnwindSafe,
P: RefUnwindSafe,
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more