pub struct RandMap<V>(/* private fields */);
Expand description
A map that creates a random handle on insertion to use when retrieving.
The raison d’être for this map is:
-
You want to put something in a map, but you have no key. Means you do not want to use a
HashMap
orBTreeMap
. -
You want to forget the details of what you put in to later retrieve it with a simple handle, and you are not interested in how many equal objects you insert. Means you do not want to use a
HashSet
or multiset. -
You want a persistent handle to refer to the item you put in the map. Means you do not want to use a
Vec
.
The implementation uses a HashMap
that does not actually hash. The
contained HashMap
can be borrowed, so all
nonmuting HashMap
functions
are at your disposal.
§Example:
use rand_map::{Handle, RandMap};
let mut map: RandMap<String> = RandMap::new();
let foo = map.insert("foo".to_string());
let baz = Handle::<String>::from_u64(4711);
map.insert_key_value(baz, "baz".to_string());
assert_eq!(baz.as_u64(), 4711);
map.remove(baz);
let bar = map.insert("bar".to_string());
assert_ne!(foo, bar);
map.clear();
assert!(map.as_hash_map().is_empty());
assert!(map.get(foo).is_none());
let foo = map.insert("foo".to_string());
let bar = map.insert("bar".to_string());
assert_eq!(map.len(), 2);
assert_eq!(map.get(foo), Some(&"foo".to_string()));
assert_eq!(map.get(bar).unwrap(), "bar");
for (key, value) in &map {
if key == bar {
assert_eq!(value, "bar");
}
}
for (key, mut value) in map.iter_mut() {
assert!(vec![foo, bar].contains(&key));
value.push_str("_more");
}
let mutref = map.get_mut(bar);
assert!(mutref.is_some());
mutref.unwrap().push_str("_and_more");
assert_eq!(map.remove(foo).unwrap(), "foo_more");
assert_eq!(map.get(bar).unwrap(), "bar_more_and_more");
// Note that as_hash_map() returns a HashMap<Handle, _> the methods of
// which generally take a key parameter that is &Handle.
assert!(map.as_hash_map().contains_key(&bar));
assert!(map == map.clone());
Implementations§
Source§impl<V> RandMap<V>
impl<V> RandMap<V>
Sourcepub fn as_hash_map(
&self,
) -> &HashMap<Handle<V>, V, BuildHasherDefault<PassThroughHasher>>
pub fn as_hash_map( &self, ) -> &HashMap<Handle<V>, V, BuildHasherDefault<PassThroughHasher>>
Borrow the contained HashMap
.
Sourcepub fn get(&self, handle: Handle<V>) -> Option<&V>
pub fn get(&self, handle: Handle<V>) -> Option<&V>
Retrieves a reference to a V
using the handle created by insert()
.
Sourcepub fn get_mut(&mut self, handle: Handle<V>) -> Option<&mut V>
pub fn get_mut(&mut self, handle: Handle<V>) -> Option<&mut V>
Retrieves a mutable reference to a V
using the handle created by
insert()
.
Sourcepub fn insert_key_value(&mut self, key: Handle<V>, value: V)
pub fn insert_key_value(&mut self, key: Handle<V>, value: V)
Insert a key-value pair. Does not return the old value for key
.
Sourcepub fn iter(&self) -> Iter<'_, V> ⓘ
pub fn iter(&self) -> Iter<'_, V> ⓘ
Almost equivalent to as_hash_map().iter()
, but the iterator element
type is (Handle<V>, &V)
rather than (&Handle<V>, &V)
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, V> ⓘ
The iterator element type is (Handle<V>, &mut V)
.
pub fn len(&self) -> usize
Trait Implementations§
Source§impl<'a, V> IntoIterator for &'a RandMap<V>
The implementation uses [iter()
(struct.RandMap.html#method.iter)
impl<'a, V> IntoIterator for &'a RandMap<V>
The implementation uses [iter()
(struct.RandMap.html#method.iter)