[−][src]Struct slotmap::hop::HopSlotMap
Hop slot map, storage with stable unique keys.
See crate documentation for more details.
Methods
impl<V: Slottable> HopSlotMap<DefaultKey, V>
[src]
impl<V: Slottable> HopSlotMap<DefaultKey, V>
pub fn new() -> Self
[src]
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
[src]
pub fn with_capacity(capacity: usize) -> Self
Creates an empty HopSlotMap
with the given capacity.
The slot map will not reallocate until it holds at least capacity
elements.
Examples
let mut sm: HopSlotMap<_, i32> = HopSlotMap::with_capacity(10);
impl<K: Key, V: Slottable> HopSlotMap<K, V>
[src]
impl<K: Key, V: Slottable> HopSlotMap<K, V>
pub fn with_key() -> Self
[src]
pub fn with_key() -> Self
Constructs a new, empty HopSlotMap
with a custom key type.
Examples
new_key_type! { struct PositionKey; } let mut positions: HopSlotMap<PositionKey, i32> = HopSlotMap::with_key();
pub fn with_capacity_and_key(capacity: usize) -> Self
[src]
pub fn with_capacity_and_key(capacity: usize) -> Self
Creates an empty HopSlotMap
with the given capacity and a custom key
type.
The slot map will not reallocate until it holds at least capacity
elements.
Examples
new_key_type! { struct MessageKey; } let mut messages = HopSlotMap::with_capacity_and_key(3); let welcome: MessageKey = messages.insert("Welcome"); let good_day = messages.insert("Good day"); let hello = messages.insert("Hello");
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the number of elements in the slot map.
Examples
let mut sm = HopSlotMap::with_capacity(10); sm.insert("len() counts actual elements, not capacity"); let key = sm.insert("removed elements don't count either"); sm.remove(key); assert_eq!(sm.len(), 1);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns if the slot map is empty.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert("dummy"); assert_eq!(sm.is_empty(), false); sm.remove(key); assert_eq!(sm.is_empty(), true);
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
Returns the number of elements the HopSlotMap
can hold without
reallocating.
Examples
let sm: HopSlotMap<_, f64> = HopSlotMap::with_capacity(10); assert_eq!(sm.capacity(), 10);
pub fn reserve(&mut self, additional: usize)
[src]
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the HopSlotMap
. The collection may reserve more space to
avoid frequent reallocations.
Panics
Panics if the new allocation size overflows usize
.
Examples
let mut sm = HopSlotMap::new(); sm.insert("foo"); sm.reserve(32); assert!(sm.capacity() >= 33);
pub fn contains_key(&self, key: K) -> bool
[src]
pub fn contains_key(&self, key: K) -> bool
Returns true
if the slot map contains key
.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert(42); assert_eq!(sm.contains_key(key), true); sm.remove(key); assert_eq!(sm.contains_key(key), false);
pub fn insert(&mut self, value: V) -> K
[src]
pub fn insert(&mut self, value: V) -> K
Inserts a value into the slot map. Returns a unique key that can be used to access this value.
Panics
Panics if the number of elements in the slot map equals 232 - 2.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert(42); assert_eq!(sm[key], 42);
pub fn insert_with_key<F>(&mut self, f: F) -> K where
F: FnOnce(K) -> V,
[src]
pub fn insert_with_key<F>(&mut self, f: F) -> K where
F: FnOnce(K) -> V,
Inserts a value given by f
into the slot map. The key where the
value will be stored is passed into f
. This is useful to store values
that contain their own key.
Panics
Panics if the number of elements in the slot map equals 232 - 2.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert_with_key(|k| (k, 20)); assert_eq!(sm[key], (key, 20));
pub fn remove(&mut self, key: K) -> Option<V>
[src]
pub fn remove(&mut self, key: K) -> Option<V>
Removes a key from the slot map, returning the value at the key if the key was not previously removed.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert(42); assert_eq!(sm.remove(key), Some(42)); assert_eq!(sm.remove(key), None);
pub fn retain<F>(&mut self, f: F) where
F: FnMut(K, &mut V) -> bool,
[src]
pub fn retain<F>(&mut self, f: F) where
F: FnMut(K, &mut V) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all key-value pairs (k, v)
such that
f(k, &mut v)
returns false. This method invalidates any removed keys.
Examples
let mut sm = HopSlotMap::new(); let k1 = sm.insert(0); let k2 = sm.insert(1); let k3 = sm.insert(2); sm.retain(|key, val| key == k1 || *val == 1); assert!(sm.contains_key(k1)); assert!(sm.contains_key(k2)); assert!(!sm.contains_key(k3)); assert_eq!(2, sm.len());
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Clears the slot map. Keeps the allocated memory for reuse.
Examples
let mut sm = HopSlotMap::new(); for i in 0..10 { sm.insert(i); } assert_eq!(sm.len(), 10); sm.clear(); assert_eq!(sm.len(), 0);
ⓘImportant traits for Drain<'a, K, V>pub fn drain(&mut self) -> Drain<K, V>
[src]
pub fn drain(&mut self) -> Drain<K, V>
Clears the slot map, returning all key-value pairs in arbitrary order as an iterator. Keeps the allocated memory for reuse.
Even if the iterator is not (fully) consumed, when it goes out of scope all remaining elements are cleared.
Examples
let mut sm = HopSlotMap::new(); let k = sm.insert(0); let v: Vec<_> = sm.drain().collect(); assert_eq!(sm.len(), 0); assert_eq!(v, vec![(k, 0)]);
pub fn get(&self, key: K) -> Option<&V>
[src]
pub fn get(&self, key: K) -> Option<&V>
Returns a reference to the value corresponding to the key.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert("bar"); assert_eq!(sm.get(key), Some(&"bar")); sm.remove(key); assert_eq!(sm.get(key), None);
pub unsafe fn get_unchecked(&self, key: K) -> &V
[src]
pub unsafe fn get_unchecked(&self, key: K) -> &V
Returns a reference to the value corresponding to the key without version or bounds checking.
Safety
This should only be used if contains_key(key)
is true. Otherwise it is
dangerous undefined behavior.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert("bar"); assert_eq!(unsafe { sm.get_unchecked(key) }, &"bar"); sm.remove(key); // sm.get_unchecked(key) is now dangerous!
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
[src]
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert(3.5); if let Some(x) = sm.get_mut(key) { *x += 3.0; } assert_eq!(sm[key], 6.5);
pub unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V
[src]
pub unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V
Returns a mutable reference to the value corresponding to the key without version or bounds checking.
Safety
This should only be used if contains_key(key)
is true. Otherwise it is
dangerous undefined behavior.
Examples
let mut sm = HopSlotMap::new(); let key = sm.insert("foo"); unsafe { *sm.get_unchecked_mut(key) = "bar" }; assert_eq!(sm[key], "bar"); sm.remove(key); // sm.get_unchecked_mut(key) is now dangerous!
ⓘImportant traits for Iter<'a, K, V>pub fn iter(&self) -> Iter<K, V>
[src]
pub fn iter(&self) -> Iter<K, V>
An iterator visiting all key-value pairs in arbitrary order. The
iterator element type is (K, &'a V)
.
Examples
let mut sm = HopSlotMap::new(); let k0 = sm.insert(0); let k1 = sm.insert(1); let k2 = sm.insert(2); for (k, v) in sm.iter() { println!("key: {:?}, val: {}", k, v); }
ⓘImportant traits for IterMut<'a, K, V>pub fn iter_mut(&mut self) -> IterMut<K, V>
[src]
pub fn iter_mut(&mut self) -> IterMut<K, V>
An iterator visiting all key-value pairs in arbitrary order, with
mutable references to the values. The iterator element type is
(K, &'a mut V)
.
Examples
let mut sm = HopSlotMap::new(); let k0 = sm.insert(10); let k1 = sm.insert(20); let k2 = sm.insert(30); for (k, v) in sm.iter_mut() { if k != k1 { *v *= -1; } } assert_eq!(sm[k0], -10); assert_eq!(sm[k1], 20); assert_eq!(sm[k2], -30);
ⓘImportant traits for Keys<'a, K, V>pub fn keys(&self) -> Keys<K, V>
[src]
pub fn keys(&self) -> Keys<K, V>
An iterator visiting all keys in arbitrary order. The iterator element
type is K
.
Examples
let mut sm = HopSlotMap::new(); let k0 = sm.insert(10); let k1 = sm.insert(20); let k2 = sm.insert(30); let keys: HashSet<_> = sm.keys().collect(); let check: HashSet<_> = vec![k0, k1, k2].into_iter().collect(); assert_eq!(keys, check);
ⓘImportant traits for Values<'a, K, V>pub fn values(&self) -> Values<K, V>
[src]
pub fn values(&self) -> Values<K, V>
An iterator visiting all values in arbitrary order. The iterator element
type is &'a V
.
Examples
let mut sm = HopSlotMap::new(); let k0 = sm.insert(10); let k1 = sm.insert(20); let k2 = sm.insert(30); let values: HashSet<_> = sm.values().collect(); let check: HashSet<_> = vec![&10, &20, &30].into_iter().collect(); assert_eq!(values, check);
ⓘImportant traits for ValuesMut<'a, K, V>pub fn values_mut(&mut self) -> ValuesMut<K, V>
[src]
pub fn values_mut(&mut self) -> ValuesMut<K, V>
An iterator visiting all values mutably in arbitrary order. The iterator
element type is &'a mut V
.
Examples
let mut sm = HopSlotMap::new(); sm.insert(1); sm.insert(2); sm.insert(3); sm.values_mut().for_each(|n| { *n *= 3 }); let values: HashSet<_> = sm.into_iter().map(|(_k, v)| v).collect(); let check: HashSet<_> = vec![3, 6, 9].into_iter().collect(); assert_eq!(values, check);
Trait Implementations
impl<'a, K: Key, V: Slottable> IntoIterator for &'a HopSlotMap<K, V>
[src]
impl<'a, K: Key, V: Slottable> IntoIterator for &'a HopSlotMap<K, V>
type Item = (K, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more
impl<'a, K: Key, V: Slottable> IntoIterator for &'a mut HopSlotMap<K, V>
[src]
impl<'a, K: Key, V: Slottable> IntoIterator for &'a mut HopSlotMap<K, V>
type Item = (K, &'a mut V)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more
impl<K: Key, V: Slottable> IntoIterator for HopSlotMap<K, V>
[src]
impl<K: Key, V: Slottable> IntoIterator for HopSlotMap<K, V>
type Item = (K, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more
impl<K: Clone + Key, V: Clone + Slottable> Clone for HopSlotMap<K, V>
[src]
impl<K: Clone + Key, V: Clone + Slottable> Clone for HopSlotMap<K, V>
fn clone(&self) -> HopSlotMap<K, V>
[src]
fn clone(&self) -> HopSlotMap<K, V>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<K: Key, V: Slottable> Default for HopSlotMap<K, V>
[src]
impl<K: Key, V: Slottable> Default for HopSlotMap<K, V>
impl<K: Debug + Key, V: Debug + Slottable> Debug for HopSlotMap<K, V>
[src]
impl<K: Debug + Key, V: Debug + Slottable> Debug for HopSlotMap<K, V>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<K: Key, V: Slottable> Index<K> for HopSlotMap<K, V>
[src]
impl<K: Key, V: Slottable> Index<K> for HopSlotMap<K, V>
type Output = V
The returned type after indexing.
fn index(&self, key: K) -> &V
[src]
fn index(&self, key: K) -> &V
Performs the indexing (container[index]
) operation.
impl<K: Key, V: Slottable> IndexMut<K> for HopSlotMap<K, V>
[src]
impl<K: Key, V: Slottable> IndexMut<K> for HopSlotMap<K, V>
Auto Trait Implementations
impl<K, V> Send for HopSlotMap<K, V> where
V: Send,
impl<K, V> Send for HopSlotMap<K, V> where
V: Send,
impl<K, V> Sync for HopSlotMap<K, V> where
V: Sync,
impl<K, V> Sync for HopSlotMap<K, V> where
V: Sync,
Blanket Implementations
impl<I> IntoIterator for I where
I: Iterator,
[src]
impl<I> IntoIterator for I where
I: Iterator,
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?
fn into_iter(self) -> I
[src]
fn into_iter(self) -> I
Creates an iterator from a value. Read more
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
[src]
fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)
[src]
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more