1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::borrow::Borrow;
use std::hash::Hash;
use std::ops::Deref;

/// 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)
///     }
/// }
///
/// # fn main() {
///     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));
/// # }
/// ```
pub trait Map<'m, K, V: 'm> {
    type GetGuard<'a>: Deref<Target = V> where 'm: 'a;

    fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard<'a>>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + Ord;
    fn insert(&mut self, k: K, v: V) -> Option<V>;
}

impl<'m, K, V, S> Map<'m, K, V> for std::collections::HashMap<K, V, S>
where
    K: Hash + Eq,
    V: 'm,
    S: std::hash::BuildHasher,
{
    type GetGuard<'a> where 'm: 'a = &'a V;

    #[inline]
    fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard<'a>>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        std::collections::HashMap::get(self, k)
    }

    #[inline]
    fn insert(&mut self, k: K, v: V) -> Option<V> {
        std::collections::HashMap::insert(self, k, v)
    }
}

impl<'m, K, V> Map<'m, K, V> for std::collections::BTreeMap<K, V>
where
    K: Ord,
    V: 'm,
{
    type GetGuard<'a> where 'm: 'a = &'a V;

    #[inline]
    fn get<'a, Q: ?Sized>(&'a self, k: &Q) -> Option<Self::GetGuard<'a>>
    where
        K: Borrow<Q>,
        Q: Ord,
    {
        std::collections::BTreeMap::get(self, k)
    }

    #[inline]
    fn insert(&mut self, k: K, v: V) -> Option<V> {
        std::collections::BTreeMap::insert(self, k, v)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fmt::Debug;

    fn assert_map_get<'m, K, V>(map: &impl Map<'m, K, V>, k: K, v: V)
    where
        K: Hash + Eq + Ord,
        V: 'm + Clone + Eq + Debug,
    {
        assert_eq!(map.get(&k).unwrap().clone(), v);
    }

    fn assert_map_insert<'m, K, V>(map: &mut impl Map<'m, K, V>, k: K, v: V, o: Option<V>)
    where
        K: Hash + Eq,
        V: 'm + Eq + Debug,
    {
        assert_eq!(map.insert(k, v), o);
    }

    #[test]
    fn test_hash_map() {
        let mut map = std::collections::HashMap::new();

        assert_map_insert(&mut map, 1, 2, None);
        assert_map_get(&map, 1, 2);
    }

    #[test]
    fn test_btree_map() {
        let mut map = std::collections::BTreeMap::new();

        assert_map_insert(&mut map, 1, 2, None);
        assert_map_get(&map, 1, 2);
    }
}