Struct PolyMap

Source
pub struct PolyMap<K: Eq + Hash, S = RandomState> { /* private fields */ }
Expand description

A key-value map that can contain varying types of values.

A single PolyMap instance can map a given key to varying types of values. Successive operations on this key must use the correct type or the operation will panic.

If you would like to store only one value of each type, use TypeMap.

§Example

use polymap::PolyMap;

let mut map = PolyMap::new();

// Maps `&str` to `&str`.
map.insert("foo", "Hello, world!");

// Maps `&str` to `i32`.
map.insert("bar", 123);

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

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

Implementations§

Source§

impl<K: Eq + Hash> PolyMap<K, RandomState>

Source

pub fn new() -> PolyMap<K>

Constructs a new PolyMap.

Source

pub fn with_capacity(n: usize) -> PolyMap<K>

Constructs a new PolyMap with space reserved for n elements.

Source§

impl<K: Eq + Hash, S: BuildHasher> PolyMap<K, S>

Source

pub fn with_hasher(hash_builder: S) -> PolyMap<K, S>

Creates an empty PolyMap which will use the given hash builder to hash keys.

Source

pub fn clear(&mut self)

Removes all key-value pairs from the map.

Source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Returns whether the map contains a value corresponding to the given key. Does not make any assertions about the type of the value.

Source

pub fn contains_key_of<Q, T: Any>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Returns whether the map contains a value corresponding to the given key whose type is the same as the given type.

§Example
use polymap::PolyMap;

let mut map = PolyMap::new();

map.insert("foo", 1);
assert_eq!(false, map.contains_key_of::<_, &str>("foo"));
assert_eq!(true, map.contains_key_of::<_, i32>("foo"));
Source

pub fn capacity(&self) -> usize

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

Source

pub fn entry<T: Any>(&mut self, key: K) -> Entry<'_, K, T>

Returns the key’s corresponding entry in the map for in-place manipulation.

Whether the entry is occupied or vacant, the type of value that can be inserted into the returned entry is constrained to T.

§Panics

If the entry exists, but the type of value differs from the one requested.

Source

pub fn get<Q, T: Any>(&self, k: &Q) -> Option<&T>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Returns a reference to the value corresponding to the given key.

If the key is not contained within the map, None will be returned.

§Panics

If the key exists, but the type of value differs from the one requested.

Source

pub fn get_mut<Q, T: Any>(&mut self, k: &Q) -> Option<&mut T>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Returns a mutable reference to the value corresponding to the given key.

If the key is not contained within the map, None will be returned.

§Panics

If the key exists, but the type of value differs from the one requested.

Source

pub fn insert<T: Any>(&mut self, k: K, t: T) -> Option<T>

Inserts a key-value pair into the map. If the key is already present, that value is returned. Otherwise, None is returned.

§Panics

If the key exists, but has a value of a different type than the one given.

Source

pub fn keys(&self) -> Keys<'_, K>

Returns an iterator visiting all keys in arbitrary order. Iterator element type is &K.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns whether the map is empty.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional additional elements.

Source

pub fn remove<Q, T: Any>(&mut self, k: &Q) -> Option<T>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes a key from the map, returning the value if one existed.

§Panics

If the key exists, but the type of value differs from the one requested.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible.

Trait Implementations§

Source§

impl<K: Eq + Hash + Debug, S: BuildHasher> Debug for PolyMap<K, S>

Source§

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

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

impl<K: Eq + Hash, S: BuildHasher + Default> Default for PolyMap<K, S>

Source§

fn default() -> PolyMap<K, S>

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

Auto Trait Implementations§

§

impl<K, S> Freeze for PolyMap<K, S>
where S: Freeze,

§

impl<K, S = RandomState> !RefUnwindSafe for PolyMap<K, S>

§

impl<K, S = RandomState> !Send for PolyMap<K, S>

§

impl<K, S = RandomState> !Sync for PolyMap<K, S>

§

impl<K, S> Unpin for PolyMap<K, S>
where S: Unpin, K: Unpin,

§

impl<K, S = RandomState> !UnwindSafe for PolyMap<K, S>

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> 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, 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.