rand_map

Struct RandMap

Source
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 or BTreeMap .

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

Source

pub fn new() -> Self

Creates an empty map.

Source

pub fn as_hash_map( &self, ) -> &HashMap<Handle<V>, V, BuildHasherDefault<PassThroughHasher>>

Borrow the contained HashMap .

Source

pub fn clear(&mut self)

Clears the map.

Source

pub fn get(&self, handle: Handle<V>) -> Option<&V>

Retrieves a reference to a V using the handle created by insert() .

Source

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

Source

pub fn insert(&mut self, value: V) -> Handle<V>

Insert a V and get a handle for retrieval.

Source

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.

Source

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)

Source

pub fn iter_mut(&mut self) -> IterMut<'_, V>

The iterator element type is (Handle<V>, &mut V).

Source

pub fn len(&self) -> usize

Source

pub fn remove(&mut self, handle: Handle<V>) -> Option<V>

Remove and return the value with handle handle, or None if not found.

Trait Implementations§

Source§

impl<V: Clone> Clone for RandMap<V>

Source§

fn clone(&self) -> RandMap<V>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Debug> Debug for RandMap<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, V> IntoIterator for &'a RandMap<V>

The implementation uses [iter()(struct.RandMap.html#method.iter)

Source§

type Item = (Handle<V>, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V> PartialEq for RandMap<V>
where V: PartialEq,

Source§

fn eq(&self, other: &RandMap<V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<V> Freeze for RandMap<V>

§

impl<V> RefUnwindSafe for RandMap<V>
where V: RefUnwindSafe,

§

impl<V> !Send for RandMap<V>

§

impl<V> !Sync for RandMap<V>

§

impl<V> Unpin for RandMap<V>
where V: Unpin,

§

impl<V> UnwindSafe for RandMap<V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V