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
use std::any::{Any, TypeId}; use polymap::PolyMap; /// 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); /// ``` /// /// # Notes /// /// Values are stored in an internal buffer that is reallocated when filled. /// Methods `reserve_data`, `reserve_data_exact`, and constructor `with_capacity` /// can be used to reserve a larger buffer ahead of time to prevent expensive /// reallocation and move operations. #[derive(Default)] pub struct TypeMap { map: PolyMap<TypeId>, } impl TypeMap { /// Constructs a new `TypeMap`. pub fn new() -> TypeMap { TypeMap{map: PolyMap::new()} } /// Constructs a new `PolyMap` with space reserved for `n` fields /// and `size` bytes of data. pub fn with_capacity(n: usize, size: usize) -> TypeMap { TypeMap{map: PolyMap::with_capacity(n, size)} } /// Removes all key-value pairs from the map, calling any destructors on /// stored values. pub fn clear(&mut self) { self.map.clear(); } /// Returns whether the map contains a value of the given type. pub fn contains<T: Any>(&self) -> bool { self.map.contains_key(&TypeId::of::<T>()) } /// Returns the capacity, in bytes, of the internal data buffer. pub fn data_capacity(&self) -> usize { self.map.data_capacity() } /// Returns the size, in bytes, of the internal data buffer. pub fn data_size(&self) -> usize { self.map.data_size() } /// Returns a reference to the value of the given type. /// /// If no value of the given type exists, `None` is returned. pub fn get<T: Any>(&self) -> Option<&T> { self.map.get(&TypeId::of::<T>()) } /// Returns a mutable reference to the value of the given type. /// /// If no value of the given type exists, `None` is returned. pub fn get_mut<T: Any>(&mut self) -> Option<&mut T> { self.map.get_mut(&TypeId::of::<T>()) } /// Inserts a value into the map. If a value of the same type is /// already present, that value is returned. Otherwise, `None` is returned. pub fn insert<T: Any>(&mut self, t: T) -> Option<T> { self.map.insert(TypeId::of::<T>(), t) } /// Returns the number of elements in the map. pub fn len(&self) -> usize { self.map.len() } /// Removes a value of the given type from the map, returning it. /// If no value of the type exists, `None` is returned. pub fn remove<T: Any>(&mut self) -> Option<T> { self.map.remove(&TypeId::of::<T>()) } /// Reserves capacity for at least `additional` additional bytes of storage /// space within the internal data buffer. pub fn reserve_data(&mut self, additional: usize) { self.map.reserve_data(additional); } /// Reserves space for at least `n` bytes in the internal data buffer. /// Does nothing if the capacity is already sufficient. pub fn reserve_data_exact(&mut self, additional: usize) { self.map.reserve_data_exact(additional); } /// Reserves capacity for at least `additional` additional fields. pub fn reserve_fields(&mut self, additional: usize) { self.map.reserve_fields(additional); } /// Reserves space for at least `n` fields. /// Does nothing if the capacity is already sufficient. pub fn reserve_fields_exact(&mut self, additional: usize) { self.map.reserve_fields_exact(additional); } /// Shrinks the internal data buffer as close as possible to the size of /// the currently contained elements. pub fn shrink_data_to_fit(&mut self) { self.map.shrink_data_to_fit(); } } #[cfg(test)] mod test { use super::TypeMap; #[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})); } }