+}
+
+/// A view into a single occupied location in an `Map`.
+pub struct OccupiedEntry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> {
+ inner: raw::OccupiedEntry<'a, A>,
+ type_: PhantomData<V>,
+}
+
+/// A view into a single empty location in an `Map`.
+pub struct VacantEntry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> {
+ inner: raw::VacantEntry<'a, A>,
+ type_: PhantomData<V>,
+}
+
+/// A view into a single location in an `Map`, which may be vacant or occupied.
+pub enum Entry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> {
+ /// An occupied Entry
+ Occupied(OccupiedEntry<'a, A, V>),
+ /// A vacant Entry
+ Vacant(VacantEntry<'a, A, V>),
+}
+
+impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A> + Clone> Entry<'a, A, V> {
+ /// Ensures a value is in the entry by inserting the default if empty, and returns
+ /// a mutable reference to the value in the entry.
+ pub fn or_insert(self, default: V) -> &'a mut V {
+ match self {
+ Entry::Occupied(inner) => inner.into_mut(),
+ Entry::Vacant(inner) => inner.insert(default),
+ }
+ }
+
+ /// Ensures a value is in the entry by inserting the result of the default function if empty,
+ /// and returns a mutable reference to the value in the entry.
+ pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
+ match self {
+ Entry::Occupied(inner) => inner.into_mut(),
+ Entry::Vacant(inner) => inner.insert(default()),
+ }
+ }
+}
+
+impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
+ /// Gets a reference to the value in the entry
+ pub fn get(&self) -> &V {
+ unsafe { self.inner.get().downcast_ref_unchecked() }
+ }
+
+ /// Gets a mutable reference to the value in the entry
+ pub fn get_mut(&mut self) -> &mut V {
+ unsafe { self.inner.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.inner.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.inner.insert(value.into_box()).downcast_unchecked() }
+ }
+
+ /// Takes the value out of the entry, and returns it
+ pub fn remove(self) -> V {
+ unsafe { *self.inner.remove().downcast_unchecked() }
+ }
+}
+
+impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> VacantEntry<'a, 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.inner.insert(value.into_box()).downcast_mut_unchecked() }
+ }
+}
+
+#[cfg(feature = "bench")]
+#[bench]
+fn bench_insertion(b: &mut ::test::Bencher) {
+ b.iter(|| {
+ let mut data = AnyMap::new();
+ for _ in 0..100 {
+ let _ = data.insert(42);
+ }
+ })
+}
+
+#[cfg(feature = "bench")]
+#[bench]
+fn bench_get_missing(b: &mut ::test::Bencher) {
+ b.iter(|| {
+ let data = AnyMap::new();
+ for _ in 0..100 {
+ assert_eq!(data.get(), None::<&i32>);
+ }
+ })
+}
+
+#[cfg(feature = "bench")]
+#[bench]
+fn bench_get_present(b: &mut ::test::Bencher) {
+ b.iter(|| {
+ let mut data = AnyMap::new();
+ let _ = data.insert(42);
+ // These inner loops are a feeble attempt to drown the other factors.
+ for _ in 0..100 {
+ assert_eq!(data.get(), Some(&42));
+ }
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use {Map, AnyMap, Entry};
+ use any::{Any, CloneAny};
+
+ #[derive(Clone, Debug, PartialEq)] struct A(i32);
+ #[derive(Clone, Debug, PartialEq)] struct B(i32);
+ #[derive(Clone, Debug, PartialEq)] struct C(i32);
+ #[derive(Clone, Debug, PartialEq)] struct D(i32);
+ #[derive(Clone, Debug, PartialEq)] struct E(i32);
+ #[derive(Clone, Debug, PartialEq)] struct F(i32);
+ #[derive(Clone, Debug, PartialEq)] struct J(i32);
+
+ #[test]
+ fn test_entry() {
+ let mut map: AnyMap = AnyMap::new();
+ assert_eq!(map.insert(A(10)), None);
+ assert_eq!(map.insert(B(20)), None);
+ assert_eq!(map.insert(C(30)), None);
+ assert_eq!(map.insert(D(40)), None);
+ assert_eq!(map.insert(E(50)), None);
+ assert_eq!(map.insert(F(60)), None);
+
+ // Existing key (insert)
+ match map.entry::<A>() {
+ Entry::Vacant(_) => unreachable!(),
+ Entry::Occupied(mut view) => {
+ assert_eq!(view.get(), &A(10));
+ assert_eq!(view.insert(A(100)), A(10));
+ }
+ }
+ assert_eq!(map.get::<A>().unwrap(), &A(100));
+ assert_eq!(map.len(), 6);
+
+
+ // Existing key (update)
+ match map.entry::<B>() {
+ Entry::Vacant(_) => unreachable!(),
+ Entry::Occupied(mut view) => {
+ let v = view.get_mut();
+ let new_v = B(v.0 * 10);
+ *v = new_v;
+ }
+ }
+ assert_eq!(map.get::<B>().unwrap(), &B(200));
+ assert_eq!(map.len(), 6);
+
+
+ // Existing key (remove)
+ match map.entry::<C>() {
+ Entry::Vacant(_) => unreachable!(),
+ Entry::Occupied(view) => {
+ assert_eq!(view.remove(), C(30));
+ }
+ }
+ assert_eq!(map.get::<C>(), None);
+ assert_eq!(map.len(), 5);
+
+
+ // Inexistent key (insert)
+ match map.entry::<J>() {
+ Entry::Occupied(_) => unreachable!(),
+ Entry::Vacant(view) => {
+ assert_eq!(*view.insert(J(1000)), J(1000));
+ }
+ }
+ assert_eq!(map.get::<J>().unwrap(), &J(1000));
+ assert_eq!(map.len(), 6);
+
+ // Entry.or_insert on existing key
+ map.entry::<B>().or_insert(B(71)).0 += 1;
+ assert_eq!(map.get::<B>().unwrap(), &B(201));
+ assert_eq!(map.len(), 6);