Struct anymap::Map [] [src]

pub struct Map<A: ?Sized + UncheckedAnyExt = Any> { /* fields omitted */ }

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 anymap::any::Any, but there are other choices:

  • If you want the entire map to be cloneable, use CloneAny instead of Any.
  • You can add on + Send and/or + Sync (e.g. Map<Any + Send>) to add those bounds.
let mut data = 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.

Methods

impl<A: ?Sized + UncheckedAnyExt> Map<A>
[src]

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.

impl<A: ?Sized + UncheckedAnyExt> Map<A>
[src]

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

Trait Implementations

impl<A: Debug + ?Sized + UncheckedAnyExt> Debug for Map<A>
[src]

Formats the value using the given formatter.

impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where
    Box<A>: Clone
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A>
[src]

Performs the conversion.

impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A>
[src]

Performs the conversion.

impl<A: ?Sized + UncheckedAnyExt> Into<RawMap<A>> for Map<A>
[src]

Performs the conversion.