[−][src]Trait map_trait::map::Map
A generic Map trait
Examples
This is a toy example of a map which reexposes an inner map and stores the most recent key and value to be inserted. Because the LastInsertMap implements Map, it can be seamlessly used as a replacement for other maps.
#![allow(incomplete_features)] #![feature(generic_associated_types)] use std::borrow::Borrow; use std::hash::Hash; use std::collections::HashMap; use map_trait::map::Map; struct LastInsertMap<M, K, V> { inner_map: M, last_key: K, last_value: V, } impl<'m, K, V, M> LastInsertMap<M, K, V> where K: Copy, V: 'm + Copy, M: Map<'m, K, V>, { fn new(mut map: M, key: K, value: V) -> Self { map.insert(key, value); LastInsertMap { inner_map: map, last_key: key, last_value: value, } } fn get_last_insert(&self) -> (&K, &V) { (&self.last_key, &self.last_value) } } impl<'m, K, V, M> Map<'m, K, V> for LastInsertMap<M, K, V> where K: Copy, V: 'm + Copy, M: Map<'m, K, V>, { type GetGuard<'a> where 'm: 'a = M::GetGuard<'a>; #[inline] fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard<'a>> where K: Borrow<Q>, Q: Hash + Eq + Ord, { self.inner_map.get(k) } #[inline] fn insert(&mut self, k: K, v: V) -> Option<V> { self.last_key = k; self.last_value = v; self.inner_map.insert(k, v) } } let mut map = LastInsertMap::new(HashMap::new(), 0, 1); assert_eq!(map.get_last_insert(), (&0, &1)); assert_eq!(map.get(&0), Some(&1)); assert_eq!(map.insert(1, 2), None); assert_eq!(map.get(&1), Some(&2)); assert_eq!(map.get_last_insert(), (&1, &2));
Associated Types
Loading content...Required methods
fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard> where
K: Borrow<Q>,
Q: Hash + Eq + Ord,
K: Borrow<Q>,
Q: Hash + Eq + Ord,
fn insert(&mut self, k: K, v: V) -> Option<V>
Implementations on Foreign Types
impl<'m, K, V, S> Map<'m, K, V> for HashMap<K, V, S> where
K: Hash + Eq,
V: 'm,
S: BuildHasher, [src]
K: Hash + Eq,
V: 'm,
S: BuildHasher,
type GetGuard = &'a V
fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard> where
K: Borrow<Q>,
Q: Hash + Eq, [src]
K: Borrow<Q>,
Q: Hash + Eq,
fn insert(&mut self, k: K, v: V) -> Option<V>[src]
impl<'m, K, V> Map<'m, K, V> for BTreeMap<K, V> where
K: Ord,
V: 'm, [src]
K: Ord,
V: 'm,
type GetGuard = &'a V
fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard> where
K: Borrow<Q>,
Q: Ord, [src]
K: Borrow<Q>,
Q: Ord,