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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Provides the `TypeMap` implementation, mapping types to values

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::collections::hash_map::{self, RandomState};
use std::fmt;
use std::hash::BuildHasher;
use std::marker::PhantomData;

/// 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`](../polymap/struct.PolyMap.html).
///
/// # 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);
/// ```
pub struct TypeMap<S = RandomState> {
    map: HashMap<TypeId, Box<Any>, S>,
}

impl TypeMap<RandomState> {
    /// Constructs a new `TypeMap`.
    #[inline]
    pub fn new() -> TypeMap {
        TypeMap{
            map: HashMap::new(),
        }
    }

    /// Constructs a new `TypeMap` with space reserved for `n` elements.
    #[inline]
    pub fn with_capacity(n: usize) -> TypeMap {
        TypeMap{
            map: HashMap::with_capacity(n),
        }
    }
}

impl<S: BuildHasher> TypeMap<S> {
    /// Creates an empty `TypeMap` which will use the given hash builder to hash keys.
    #[inline]
    pub fn with_hasher(hash_builder: S) -> TypeMap<S> {
        TypeMap{
            map: HashMap::with_hasher(hash_builder),
        }
    }

    /// Removes all values from the map.
    #[inline]
    pub fn clear(&mut self) {
        self.map.clear();
    }

    /// Returns whether the map contains a value corresponding to the given type.
    #[inline]
    pub fn contains<T: Any>(&self) -> bool {
        self.map.contains_key(&TypeId::of::<T>())
    }

    /// Returns the number of elements the map can hold without reallocating.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.map.capacity()
    }

    /// Returns the type's corresponding entry in the map for in-place manipulation.
    #[inline]
    pub fn entry<T: Any>(&mut self) -> Entry<T> {
        match self.map.entry(TypeId::of::<T>()) {
            hash_map::Entry::Vacant(ent) => Entry::Vacant(VacantEntry::new(ent)),
            hash_map::Entry::Occupied(ent) => Entry::Occupied(OccupiedEntry::new(ent))
        }
    }

    /// Returns a reference to the value corresponding to the given type.
    ///
    /// If the type is not contained within the map, `None` will be returned.
    #[inline]
    pub fn get<T: Any>(&self) -> Option<&T> {
        if let Some(v) = self.map.get(&TypeId::of::<T>()) {
            Some(v.downcast_ref().expect("wrong type in TypeMap"))
        } else {
            None
        }
    }

    /// Returns a mutable reference to the value corresponding to the given type.
    ///
    /// If the type is not contained within the map, `None` will be returned.
    #[inline]
    pub fn get_mut<T: Any>(&mut self) -> Option<&mut T> {
        if let Some(v) = self.map.get_mut(&TypeId::of::<T>()) {
            Some(v.downcast_mut().expect("wrong type in TypeMap"))
        } else {
            None
        }
    }

    /// Inserts a value into the map. If a value of that type is already present,
    /// that value is returned. Otherwise, `None` is returned.
    #[inline]
    pub fn insert<T: Any>(&mut self, t: T) -> Option<T> {
        self.map.insert(TypeId::of::<T>(), Box::new(t))
            .map(|old| *old.downcast().expect("wrong type in TypeMap"))
    }

    /// Returns the number of elements in the map.
    #[inline]
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Returns whether the map is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Reserves capacity for at least `additional` additional elements.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.map.reserve(additional);
    }

    /// Removes a value from the map, returning the value if one existed.
    #[inline]
    pub fn remove<T: Any>(&mut self) -> Option<T> {
        self.map.remove(&TypeId::of::<T>())
            .map(|old| *old.downcast().expect("wrong type in TypeMap"))
    }

    /// Shrinks the capacity of the map as much as possible.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.map.shrink_to_fit();
    }
}

impl<S> fmt::Debug for TypeMap<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("TypeMap { .. }")
    }
}

impl<S: BuildHasher + Default> Default for TypeMap<S> {
    fn default() -> TypeMap<S> {
        TypeMap::with_hasher(S::default())
    }
}

/// A view into a single entry in a map, which may be either vacant or occupied.
///
/// This enum is returned from the [`entry`][entry] method on [`TypeMap`][typemap].
///
/// [typemap]: struct.TypeMap.html
/// [entry]: struct.TypeMap.html#method.entry
pub enum Entry<'a, V: Any> {
    /// An occupied entry
    Occupied(OccupiedEntry<'a, V>),
    /// A vacant entry
    Vacant(VacantEntry<'a, V>),
}

impl<'a, V: Any> Entry<'a, V> {
    /// Inserts a value if empty, then returns a mutable reference.
    #[inline]
    pub fn or_insert(self, default: V) -> &'a mut V {
        match self {
            Entry::Occupied(ent) => ent.into_mut(),
            Entry::Vacant(ent) => ent.insert(default)
        }
    }

    /// Inserts a value if empty using the given function,
    /// then returns a mutable reference.
    #[inline]
    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
        match self {
            Entry::Occupied(ent) => ent.into_mut(),
            Entry::Vacant(ent) => ent.insert(default())
        }
    }
}

impl<'a, V: Any + fmt::Debug> fmt::Debug for Entry<'a, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Entry::Occupied(ref ent) =>
                f.debug_tuple("Entry")
                    .field(ent)
                    .finish(),
            Entry::Vacant(ref ent) =>
                f.debug_tuple("Entry")
                    .field(ent)
                    .finish(),
        }
    }
}

/// A view into an occupied entry in a `TypeMap`.
pub struct OccupiedEntry<'a, V: Any> {
    entry: hash_map::OccupiedEntry<'a, TypeId, Box<Any>>,
    _data: PhantomData<V>,
}

impl<'a, V: Any> OccupiedEntry<'a, V> {
    fn new(entry: hash_map::OccupiedEntry<'a, TypeId, Box<Any>>) -> OccupiedEntry<'a, V> {
        OccupiedEntry{
            entry: entry,
            _data: PhantomData,
        }
    }

    /// Returns a reference to the entry value.
    #[inline]
    pub fn get(&self) -> &V {
        self.entry.get().downcast_ref().expect("wrong type in entry")
    }

    /// Returns a mutable reference to the entry value.
    #[inline]
    pub fn get_mut(&mut self) -> &mut V {
        self.entry.get_mut().downcast_mut().expect("wrong type in entry")
    }

    /// Consumes the entry and returns a mutable reference
    /// tied to the lifetime of the parent container.
    #[inline]
    pub fn into_mut(self) -> &'a mut V {
        self.entry.into_mut().downcast_mut().expect("wrong type in entry")
    }

    /// Inserts a value into the entry and returns the old value.
    #[inline]
    pub fn insert(&mut self, value: V) -> V {
        *self.entry.insert(Box::new(value)).downcast().expect("wrong type in entry")
    }

    /// Removes the entry and returns the value.
    #[inline]
    pub fn remove(self) -> V {
        *self.entry.remove().downcast().expect("wrong type in entry")
    }
}

impl<'a, V: Any + fmt::Debug> fmt::Debug for OccupiedEntry<'a, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("OccupiedEntry")
            .field(self.get())
            .finish()
    }
}

/// A view into a vacant entry in a `TypeMap`.
pub struct VacantEntry<'a, V: Any> {
    entry: hash_map::VacantEntry<'a, TypeId, Box<Any>>,
    _data: PhantomData<V>,
}

impl<'a, V: Any> VacantEntry<'a, V> {
    fn new(entry: hash_map::VacantEntry<'a, TypeId, Box<Any>>) -> VacantEntry<'a, V> {
        VacantEntry{
            entry: entry,
            _data: PhantomData,
        }
    }

    /// Consumes the entry, inserting a value and returning a mutable reference.
    #[inline]
    pub fn insert(self, value: V) -> &'a mut V {
        self.entry.insert(Box::new(value)).downcast_mut().expect("wrong type in entry")
    }
}

impl<'a, V: Any> fmt::Debug for VacantEntry<'a, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("VacantEntry { .. }")
    }
}

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

    #[test]
    fn test_debug() {
        let mut m = TypeMap::new();

        m.insert(123_i32);

        assert_eq!(format!("{:?}", m), "TypeMap { .. }");

        assert_eq!(format!("{:?}", m.entry::<i32>()),
            r#"Entry(OccupiedEntry(123))"#);
        assert_eq!(format!("{:?}", m.entry::<()>()),
            r#"Entry(VacantEntry { .. })"#);
    }

    #[test]
    fn test_entry() {
        let mut m = TypeMap::new();

        m.entry::<u32>().or_insert(123);
        m.entry::<String>().or_insert_with(String::new);

        assert_eq!(m.len(), 2);
        assert_eq!(m.get::<u32>(), Some(&123));
        assert_eq!(m.get::<String>(), Some(&String::new()));
    }

    #[derive(Debug, Eq, PartialEq)]
    struct Foo {
        a: i32,
        b: i32,
    }

    #[test]
    fn test_typemap() {
        let mut m = TypeMap::new();

        m.insert(123);
        m.insert(456_u32);
        m.insert("foo");
        m.insert(Foo{a: 1, b: 2});

        assert_eq!(m.get::<i32>(), Some(&123));
        assert_eq!(m.get::<u32>(), Some(&456));
        assert_eq!(m.get::<&str>(), Some(&"foo"));
        assert_eq!(m.get::<Foo>(), Some(&Foo{a: 1, b: 2}));
    }
}