Struct polymap::typemap::TypeMap [] [src]

pub struct TypeMap { /* fields omitted */ }

A container for values of varying types.

Values contained in a TypeMap are stored uniquely according to their type. This means that, for each possible type, only one value may be stored in a single TypeMap instance.

If you would like to store multiple values of a given type, mapped to individual keys, use PolyMap.

Example

use polymap::TypeMap;

let mut map = TypeMap::new();

// Stores a `&str` value
map.insert("Hello, world!");

// Stores an `i32` value
map.insert(123);

// Gets a reference to the stored value
let &foo: &&str = map.get().unwrap();
assert_eq!(foo, "Hello, world!");

let &bar: &i32 = map.get().unwrap();
assert_eq!(bar, 123);

Notes

Values are stored in an internal buffer that is reallocated when filled. Methods reserve_data, reserve_data_exact, and constructor with_capacity can be used to reserve a larger buffer ahead of time to prevent expensive reallocation and move operations.

Methods

impl TypeMap
[src]

Constructs a new TypeMap.

Constructs a new PolyMap with space reserved for n fields and size bytes of data.

Removes all key-value pairs from the map, calling any destructors on stored values.

Returns whether the map contains a value of the given type.

Returns the capacity, in bytes, of the internal data buffer.

Returns the size, in bytes, of the internal data buffer.

Returns a reference to the value of the given type.

If no value of the given type exists, None is returned.

Returns a mutable reference to the value of the given type.

If no value of the given type exists, None is returned.

Inserts a value into the map. If a value of the same type is already present, that value is returned. Otherwise, None is returned.

Returns the number of elements in the map.

Removes a value of the given type from the map, returning it. If no value of the type exists, None is returned.

Reserves capacity for at least additional additional bytes of storage space within the internal data buffer.

Reserves space for at least n bytes in the internal data buffer. Does nothing if the capacity is already sufficient.

Reserves capacity for at least additional additional fields.

Reserves space for at least n fields. Does nothing if the capacity is already sufficient.

Shrinks the internal data buffer as close as possible to the size of the currently contained elements.

Trait Implementations

impl Default for TypeMap
[src]

Returns the "default value" for a type. Read more