[−][src]Struct one_way_slot_map::SlotMap
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]
K: SlotMapKey<P>,
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
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));
pub fn get_raw(&self, key_data: &SlotMapKeyData) -> Option<&T>
[src]
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));
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]
&mut self,
key: &impl Borrow<SlotMapKeyData>
) -> Option<&mut T>
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));
pub fn get_mut_raw(&mut self, key_data: &SlotMapKeyData) -> Option<&mut T>
[src]
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));
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]
&mut self,
key: &impl Borrow<SlotMapKeyData>
) -> Option<&mut T>
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));
pub fn remove_raw(&mut self, key_data: &SlotMapKeyData) -> Option<&mut T>
[src]
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));
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
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));
pub fn contains_key_raw(&self, key_data: &SlotMapKeyData) -> bool
[src]
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));
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<F>(&self, pointer_finder: F) -> impl Iterator<Item = (K, &T)> where
F: FnMut(&T) -> P,
[src]
F: FnMut(&T) -> P,
Get an iterator over keys and values given a way to get the pointer from the stored value.
pub fn iter_mut<F>(
&mut self,
pointer_finder: F
) -> impl Iterator<Item = (K, &mut T)> where
F: FnMut(&T) -> P,
[src]
&mut self,
pointer_finder: F
) -> impl Iterator<Item = (K, &mut T)> where
F: FnMut(&T) -> P,
Get an iterator over keys and mutable values given a way to get the pointer from the stored value.
pub fn iter_raw(&self) -> impl Iterator<Item = (SlotMapKeyData, &T)>
[src]
Create an iterator over all raw key data and values for items present in the slot map
pub fn iter_mut_raw(&mut self) -> impl Iterator<Item = (SlotMapKeyData, &mut T)>
[src]
Create an iterator over all raw key data and mutable values for items present in the slot map
pub fn values(&self) -> impl Iterator<Item = &T>
[src]
Create an iterator over all items in the items in the map
pub fn values_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]
F: FnMut(&T) -> R,
Create a new map that has the same structure 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]
K: SlotMapKey<P>,
T: Clone,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<K, P, T> Debug for SlotMap<K, P, T> where
T: Debug,
K: SlotMapKey<P>,
[src]
T: Debug,
K: SlotMapKey<P>,
impl<K, P, T> Default for SlotMap<K, P, T> where
K: SlotMapKey<P>,
[src]
K: SlotMapKey<P>,
impl<K, P, T> Send for SlotMap<K, P, T> where
K: Sync + SlotMapKey<P>,
P: Sync,
T: Sync,
[src]
K: Sync + SlotMapKey<P>,
P: Sync,
T: Sync,
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
impl<K, P, T> Sync for SlotMap<K, P, T> where
K: Sync + SlotMapKey<P>,
P: Sync,
T: Sync,
[src]
K: Sync + SlotMapKey<P>,
P: Sync,
T: Sync,
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,
K: RefUnwindSafe,
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<K, P, T> Unpin for SlotMap<K, P, T>
impl<K, P, T> UnwindSafe for SlotMap<K, P, T> where
K: RefUnwindSafe,
P: RefUnwindSafe,
T: UnwindSafe,
K: RefUnwindSafe,
P: RefUnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,