+ /// Remove and return the value for the type `T` if it existed.
+ pub fn remove<T: Any + 'static>(&mut self) -> Option<T> {
+ self.data.remove(&TypeId::of::<T>())
+ .map(|any| *unsafe { any.downcast_unchecked::<T>() })
+ }
+
+ /// Does a value of type `T` exist?
+ pub fn contains<T: Any + 'static>(&self) -> bool {
+ self.data.contains_key(&TypeId::of::<T>())
+ }
+
+ /// Gets the given key's corresponding entry in the map for in-place manipulation
+ pub fn entry<T: Any + 'static>(&mut self) -> Entry<T> {
+ match self.data.entry(TypeId::of::<T>()) {
+ hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry { entry: e }),
+ hash_map::Entry::Vacant(e) => Entry::Vacant(VacantEntry { entry: e }),
+ }
+ }
+
+ /// Returns the number of items in the collection.
+ pub fn len(&self) -> uint {
+ self.data.len()
+ }
+
+ /// Returns true if there are no items in the collection.
+ pub fn is_empty(&self) -> bool {
+ self.data.is_empty()
+ }
+
+ /// Removes all items from the collection.
+ pub fn clear(&mut self) {
+ self.data.clear();
+ }
+}
+
+/// A view into a single occupied location in an AnyMap
+pub struct OccupiedEntry<'a, V: 'a> {
+ entry: hash_map::OccupiedEntry<'a, TypeId, Box<Any + 'static>>,
+}
+
+/// A view into a single empty location in an AnyMap
+pub struct VacantEntry<'a, V: 'a> {
+ entry: hash_map::VacantEntry<'a, TypeId, Box<Any + 'static>>,
+}
+
+/// A view into a single location in a map, which may be vacant or occupied
+pub enum Entry<'a, V: 'a> {
+ /// An occupied Entry
+ Occupied(OccupiedEntry<'a, V>),
+ /// A vacant Entry
+ Vacant(VacantEntry<'a, V>),
+}
+
+impl<'a, V: 'static> OccupiedEntry<'a, V> {
+ /// Gets a reference to the value in the entry
+ pub fn get(&self) -> &V {
+ unsafe { self.entry.get().downcast_ref_unchecked() }
+ }
+
+ /// Gets a mutable reference to the value in the entry
+ pub fn get_mut(&mut self) -> &mut V {
+ unsafe { self.entry.get_mut().downcast_mut_unchecked() }
+ }
+
+ /// Converts the OccupiedEntry into a mutable reference to the value in the entry
+ /// with a lifetime bound to the map itself
+ pub fn into_mut(self) -> &'a mut V {
+ unsafe { self.entry.into_mut().downcast_mut_unchecked() }
+ }
+
+ /// Sets the value of the entry, and returns the entry's old value
+ pub fn set(&mut self, value: V) -> V {
+ unsafe { *self.entry.set(box value as Box<Any + 'static>).downcast_unchecked() }
+ }
+
+ /// Takes the value out of the entry, and returns it
+ pub fn take(self) -> V {
+ unsafe { *self.entry.take().downcast_unchecked() }
+ }
+}
+
+impl<'a, V: 'static> VacantEntry<'a, V> {
+ /// Sets the value of the entry with the VacantEntry's key,
+ /// and returns a mutable reference to it
+ pub fn set(self, value: V) -> &'a mut V {
+ unsafe { self.entry.set(box value as Box<Any + 'static>).downcast_mut_unchecked() }