+/// 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>>,
+ type_: PhantomData<V>,
+}
+
+/// 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>>,
+ type_: PhantomData<V>,
+}
+
+/// A view into a single location in an AnyMap, 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 + Clone> Entry<'a, V> {
+ /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
+ pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> {
+ match self {
+ Entry::Occupied(entry) => Ok(entry.into_mut()),
+ Entry::Vacant(entry) => Err(entry),
+ }
+ }
+}
+
+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 collection 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 insert(&mut self, value: V) -> V {
+ unsafe { *self.entry.insert(Box::new(value) as Box<Any + 'static>).downcast_unchecked() }
+ }
+
+ /// Takes the value out of the entry, and returns it
+ pub fn remove(self) -> V {
+ unsafe { *self.entry.remove().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 insert(self, value: V) -> &'a mut V {
+ unsafe { self.entry.insert(Box::new(value) as Box<Any + 'static>).downcast_mut_unchecked() }
+ }
+}
+
+/// `AnyMap` iterator.
+#[derive(Clone)]
+pub struct Iter<'a> {
+ inner: hash_map::Iter<'a, TypeId, Box<Any + 'static>>,
+}
+
+/// `AnyMap` mutable references iterator.
+pub struct IterMut<'a> {
+ inner: hash_map::IterMut<'a, TypeId, Box<Any + 'static>>,
+}
+
+/// `AnyMap` draining iterator.
+pub struct Drain<'a> {
+ inner: hash_map::Drain<'a, TypeId, Box<Any + 'static>>,
+}
+
+/// `AnyMap` move iterator.
+pub struct IntoIter {
+ inner: hash_map::IntoIter<TypeId, Box<Any + 'static>>,
+}
+
+impl<'a> Iterator for Iter<'a> {
+ type Item = &'a Any;
+
+ #[inline]
+ fn next(&mut self) -> Option<&'a Any> {
+ self.inner.next().map(|item| &**item.1)
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+}
+
+impl<'a> Iterator for IterMut<'a> {
+ type Item = &'a mut Any;
+
+ #[inline]
+ fn next(&mut self) -> Option<&'a mut Any> {
+ self.inner.next().map(|item| &mut **item.1)
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+}
+
+impl<'a> Iterator for Drain<'a> {
+ type Item = Box<Any + 'static>;
+
+ #[inline]
+ fn next(&mut self) -> Option<Box<Any + 'static>> {
+ self.inner.next().map(|item| item.1)
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+}
+
+impl Iterator for IntoIter {
+ type Item = Box<Any + 'static>;
+
+ #[inline]
+ fn next(&mut self) -> Option<Box<Any + 'static>> {
+ self.inner.next().map(|item| item.1)
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+}
+