Struct anymap::Map

source · []
pub struct Map<A: ?Sized + UncheckedAnyExt = dyn Any> { /* private fields */ }
Expand description

A collection containing zero or one values for any given type and allowing convenient, type-safe access to those values.

The type parameter A allows you to use a different value type; normally you will want it to be core::any::Any (also known as std::any::Any), but there are other choices:

  • If you want the entire map to be cloneable, use CloneAny instead of Any; with that, you can only add types that implement Clone to the map.
  • You can add on + Send or + Send + Sync (e.g. Map<dyn Any + Send>) to add those auto traits.

Cumulatively, there are thus six forms of map:

Example

(Here using the AnyMap convenience alias; the first line could use anymap::Map::<core::any::Any>::new() instead if desired.)

let mut data = anymap::AnyMap::new();
assert_eq!(data.get(), None::<&i32>);
data.insert(42i32);
assert_eq!(data.get(), Some(&42i32));
data.remove::<i32>();
assert_eq!(data.get::<i32>(), None);

#[derive(Clone, PartialEq, Debug)]
struct Foo {
    str: String,
}

assert_eq!(data.get::<Foo>(), None);
data.insert(Foo { str: format!("foo") });
assert_eq!(data.get(), Some(&Foo { str: format!("foo") }));
data.get_mut::<Foo>().map(|foo| foo.str.push('t'));
assert_eq!(&*data.get::<Foo>().unwrap().str, "foot");

Values containing non-static references are not permitted.

Implementations

Create an empty collection.

Creates an empty collection with the given initial capacity.

Returns the number of elements the collection can hold without reallocating.

Reserves capacity for at least additional more elements to be inserted in the collection. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new allocation size overflows usize.

Shrinks the capacity of the collection as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

Returns the number of items in the collection.

Returns true if there are no items in the collection.

Removes all items from the collection. Keeps the allocated memory for reuse.

Returns a reference to the value stored in the collection for the type T, if it exists.

Returns a mutable reference to the value stored in the collection for the type T, if it exists.

Sets the value stored in the collection for the type T. If the collection already had a value of type T, that value is returned. Otherwise, None is returned.

Removes the T value from the collection, returning it if there was one or None if there was not.

Returns true if the collection contains a value of type T.

Gets the entry for the given type in the collection for in-place manipulation

Get access to the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection, and this lets you do that.

To improve compatibility with Cargo features, interact with this map through the names anymap::RawMap and anymap::raw_hash_map, rather than through std::collections::{HashMap, hash_map} or hashbrown::{HashMap, hash_map}, for anything beyond self methods. Otherwise, if you use std and another crate in the tree enables hashbrown, your code will break.

Get mutable access to the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to iterate over all the items in the collection mutably, or drain or something, or possibly even batch insert, and this lets you do that.

To improve compatibility with Cargo features, interact with this map through the names anymap::RawMap and anymap::raw_hash_map, rather than through std::collections::{HashMap, hash_map} or hashbrown::{HashMap, hash_map}, for anything beyond self methods. Otherwise, if you use std and another crate in the tree enables hashbrown, your code will break.

Safety

If you insert any values to the raw map, the key (a TypeId) must match the value’s type, or undefined behaviour will occur when you access those values.

(Removing entries is perfectly safe.)

Convert this into the raw hash map that backs this.

This will seldom be useful, but it’s conceivable that you could wish to consume all the items in the collection and do something with some or all of them, and this lets you do that, without the unsafe that .as_raw_mut().drain() would require.

To improve compatibility with Cargo features, interact with this map through the names anymap::RawMap and anymap::raw_hash_map, rather than through std::collections::{HashMap, hash_map} or hashbrown::{HashMap, hash_map}, for anything beyond self methods. Otherwise, if you use std and another crate in the tree enables hashbrown, your code will break.

Construct a map from a collection of raw values.

You know what? I can’t immediately think of any legitimate use for this, especially because of the requirement of the BuildHasherDefault<TypeIdHasher> generic in the map.

Perhaps this will be most practical as unsafe { Map::from_raw(iter.collect()) }, iter being an iterator over (TypeId, Box<A>) pairs. Eh, this method provides symmetry with into_raw, so I don’t care if literally no one ever uses it. I’m not even going to write a test for it, it’s so trivial.

To improve compatibility with Cargo features, interact with this map through the names anymap::RawMap and anymap::raw_hash_map, rather than through std::collections::{HashMap, hash_map} or hashbrown::{HashMap, hash_map}, for anything beyond self methods. Otherwise, if you use std and another crate in the tree enables hashbrown, your code will break.

Safety

For all entries in the raw map, the key (a TypeId) must match the value’s type, or undefined behaviour will occur when you access that entry.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.