Struct typedmap::hashmap::TypedKeyValueMutRef[][src]

pub struct TypedKeyValueMutRef<'a, Marker> { /* fields omitted */ }

Represents mutably borrowed pair of key and value.

Implementations

impl<'a, Marker> TypedKeyValueMutRef<'a, Marker>[src]

pub fn downcast_key_ref<K: 'static + TypedMapKey<Marker>>(
    &self
) -> Option<&'a K>
[src]

Downcast key to reference of K. Returns None if not possible.

pub fn downcast_value_mut<'b, V: 'static + Any>(
    &'b mut self
) -> Option<&'b mut V> where
    'a: 'b, 
[src]

Downcast value to type V. Returns None if not possible.

Note: this function borrows mutably self, and returns mutable reference with lifetime bounded by that borrow. If you need to obtain mutable reference with hashmap bounded lifetime (’a), then use downcast_value function.

pub fn downcast_value<V: 'static + Any>(self) -> Result<&'a mut V, Self>[src]

Tries to cast value to mutable reference of V consuming self. This allows to return mutable reference with ’a lifetime which may be useful when collecting mutable references.

Examples:

use typedmap::TypedMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq, Debug)]
struct Key(usize);
#[derive(Hash, PartialEq, Eq, Debug)]
struct SKey(&'static str);

impl TypedMapKey for Key {
    type Value = u32;
}

impl TypedMapKey for SKey {
    type Value = usize;
}

let mut map: TypedMap = TypedMap::new();
map.insert(Key(3), 3);
map.insert(SKey("four"), 4);

let v: Vec<&mut u32> = map
    .iter_mut()
    .flat_map(|kv| kv.downcast_value::<u32>().ok())
    .collect();
assert_eq!(*v[0], 3);
assert_eq!(v.len(), 1);

pub fn downcast_pair_mut<'b, K: 'static + TypedMapKey<Marker>>(
    &'b mut self
) -> Option<(&'b K, &'b mut K::Value)> where
    'a: 'b, 
[src]

Downcast key to type K and value to type K::Value. Returns None if not possible.

Note: this function borrows mutably self, and returns mutable reference with lifetime bounded by that borrow. If you need to obtain mutable reference with hashmap bounded lifetime (’a), then use downcast_pair function.

pub fn downcast_pair<K: 'static + TypedMapKey<Marker>>(
    self
) -> Result<(&'a K, &'a mut K::Value), Self>
[src]

Tries to cast self to key K and value K::Value consuming self. This allows to return references with ’a lifetime which may be useful when collecting references to Vec.

Examples:

use typedmap::TypedMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq, Debug)]
struct Key(usize);
#[derive(Hash, PartialEq, Eq, Debug)]
struct SKey(&'static str);

impl TypedMapKey for Key {
    type Value = u32;
}

impl TypedMapKey for SKey {
    type Value = usize;
}

let mut map: TypedMap = TypedMap::new();
map.insert(Key(3), 3);
map.insert(SKey("four"), 4);

let v: Vec<(&Key, &mut u32)> = map
    .iter_mut()
    .flat_map(|kv| kv.downcast_pair::<Key>().ok())
    .collect();
assert_eq!(*v[0].0, Key(3));
assert_eq!(*v[0].1, 3);
assert_eq!(v.len(), 1);

Trait Implementations

impl<M> Debug for TypedKeyValueMutRef<'_, M>[src]

Auto Trait Implementations

impl<'a, Marker> !RefUnwindSafe for TypedKeyValueMutRef<'a, Marker>

impl<'a, Marker> !Send for TypedKeyValueMutRef<'a, Marker>

impl<'a, Marker> !Sync for TypedKeyValueMutRef<'a, Marker>

impl<'a, Marker> Unpin for TypedKeyValueMutRef<'a, Marker> where
    Marker: Unpin

impl<'a, Marker> !UnwindSafe for TypedKeyValueMutRef<'a, Marker>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.