Struct anymap::AnyMap [] [src]

pub struct AnyMap {
    // some fields omitted
}

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

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 AnyMap
[src]

fn new() -> AnyMap

Create an empty collection.

fn with_capacity(capacity: usize) -> AnyMap

Creates an empty collection with the given initial capacity.

fn capacity(&self) -> usize

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

fn reserve(&mut self, additional: usize)

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.

fn shrink_to_fit(&mut self)

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.

fn len(&self) -> usize

Returns the number of items in the collection.

fn is_empty(&self) -> bool

Returns true if there are no items in the collection.

fn clear(&mut self)

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

impl AnyMap
[src]

fn get<T: Any>(&self) -> Option<&T>

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

fn get_mut<T: Any>(&mut self) -> Option<&mut T>

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

fn insert<T: Any>(&mut self, value: T) -> Option<T>

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.

fn remove<T: Any>(&mut self) -> Option<T>

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

fn contains<T: Any>(&self) -> bool

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

fn entry<T: Any>(&mut self) -> Entry<T>

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

Trait Implementations

impl Debug for AnyMap
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl AsRef<RawAnyMap> for AnyMap
[src]

fn as_ref(&self) -> &RawAnyMap

Performs the conversion.

impl AsMut<RawAnyMap> for AnyMap
[src]

fn as_mut(&mut self) -> &mut RawAnyMap

Performs the conversion.

impl Into<RawAnyMap> for AnyMap
[src]

fn into(self) -> RawAnyMap

Performs the conversion.