Unravel the parbroken define! macro
[anymap] / src / lib.rs
index 5f21a947f05c663aaf6c7183444a8d98028d5efd..c9e027633b2e37fe06f828d1189ec20285757578 100644 (file)
@@ -1,11 +1,7 @@
 //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
 
-#![cfg_attr(all(feature = "bench", test), feature(test))]
 #![warn(missing_docs, unused_results)]
 
-#[cfg(all(feature = "bench", test))]
-extern crate test;
-
 use std::any::TypeId;
 use std::marker::PhantomData;
 
@@ -79,6 +75,13 @@ macro_rules! impl_common_methods {
                 self.$field.clear()
             }
         }
+
+        impl<A: ?Sized + UncheckedAnyExt> Default for $t<A> {
+            #[inline]
+            fn default() -> $t<A> {
+                $t::new()
+            }
+        }
     }
 }
 
@@ -92,7 +95,7 @@ pub mod raw;
 /// be `anymap::any::Any`, but there are other choices:
 ///
 /// - If you want the entire map to be cloneable, use `CloneAny` instead of `Any`.
-/// - You can add on `+ Send` and/or `+ Sync` (e.g. `Map<Any + Send>`) to add those bounds.
+/// - You can add on `+ Send` and/or `+ Sync` (e.g. `Map<dyn Any + Send>`) to add those bounds.
 ///
 /// ```rust
 /// # use anymap::AnyMap;
@@ -117,12 +120,13 @@ pub mod raw;
 ///
 /// Values containing non-static references are not permitted.
 #[derive(Debug)]
-pub struct Map<A: ?Sized + UncheckedAnyExt = Any> {
+pub struct Map<A: ?Sized + UncheckedAnyExt = dyn Any> {
     raw: RawMap<A>,
 }
 
 // #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box<A> that can.
 impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where Box<A>: Clone {
+    #[inline]
     fn clone(&self) -> Map<A> {
         Map {
             raw: self.raw.clone(),
@@ -135,7 +139,7 @@ impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where Box<A>: Clone {
 /// Why is this a separate type alias rather than a default value for `Map<A>`? `Map::new()`
 /// doesn’t seem to be happy to infer that it should go with the default value.
 /// It’s a bit sad, really. Ah well, I guess this approach will do.
-pub type AnyMap = Map<Any>;
+pub type AnyMap = Map<dyn Any>;
 
 impl_common_methods! {
     field: Map.raw;
@@ -145,6 +149,7 @@ impl_common_methods! {
 
 impl<A: ?Sized + UncheckedAnyExt> Map<A> {
     /// Returns a reference to the value stored in the collection for the type `T`, if it exists.
+    #[inline]
     pub fn get<T: IntoBox<A>>(&self) -> Option<&T> {
         self.raw.get(&TypeId::of::<T>())
             .map(|any| unsafe { any.downcast_ref_unchecked::<T>() })
@@ -152,6 +157,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
 
     /// Returns a mutable reference to the value stored in the collection for the type `T`,
     /// if it exists.
+    #[inline]
     pub fn get_mut<T: IntoBox<A>>(&mut self) -> Option<&mut T> {
         self.raw.get_mut(&TypeId::of::<T>())
             .map(|any| unsafe { any.downcast_mut_unchecked::<T>() })
@@ -160,6 +166,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
     /// Sets the value stored in the collection for the type `T`.
     /// If the collection already had a value of type `T`, that value is returned.
     /// Otherwise, `None` is returned.
+    #[inline]
     pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T> {
         unsafe {
             self.raw.insert(TypeId::of::<T>(), value.into_box())
@@ -169,6 +176,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
 
     /// Removes the `T` value from the collection,
     /// returning it if there was one or `None` if there was not.
+    #[inline]
     pub fn remove<T: IntoBox<A>>(&mut self) -> Option<T> {
         self.raw.remove(&TypeId::of::<T>())
             .map(|any| *unsafe { any.downcast_unchecked::<T>() })
@@ -181,6 +189,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
     }
 
     /// Gets the entry for the given type in the collection for in-place manipulation
+    #[inline]
     pub fn entry<T: IntoBox<A>>(&mut self) -> Entry<A, T> {
         match self.raw.entry(TypeId::of::<T>()) {
             raw::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry {
@@ -196,18 +205,21 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
 }
 
 impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A> {
+    #[inline]
     fn as_ref(&self) -> &RawMap<A> {
         &self.raw
     }
 }
 
 impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A> {
+    #[inline]
     fn as_mut(&mut self) -> &mut RawMap<A> {
         &mut self.raw
     }
 }
 
 impl<A: ?Sized + UncheckedAnyExt> Into<RawMap<A>> for Map<A> {
+    #[inline]
     fn into(self) -> RawMap<A> {
         self.raw
     }
@@ -236,6 +248,7 @@ pub enum Entry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> {
 impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> 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.
+    #[inline]
     pub fn or_insert(self, default: V) -> &'a mut V {
         match self {
             Entry::Occupied(inner) => inner.into_mut(),
@@ -245,6 +258,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> Entry<'a, A, V> {
 
     /// 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.
+    #[inline]
     pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
         match self {
             Entry::Occupied(inner) => inner.into_mut(),
@@ -255,27 +269,32 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> Entry<'a, A, V> {
 
 impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
     /// Gets a reference to the value in the entry
+    #[inline]
     pub fn get(&self) -> &V {
         unsafe { self.inner.get().downcast_ref_unchecked() }
     }
 
     /// Gets a mutable reference to the value in the entry
+    #[inline]
     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
+    #[inline]
     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
+    #[inline]
     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
+    #[inline]
     pub fn remove(self) -> V {
         unsafe { *self.inner.remove().downcast_unchecked() }
     }
@@ -284,46 +303,12 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
 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
+    #[inline]
     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};
@@ -409,11 +394,17 @@ mod tests {
     }
 
     test_entry!(test_entry_any, AnyMap);
-    test_entry!(test_entry_cloneany, Map<CloneAny>);
+    test_entry!(test_entry_cloneany, Map<dyn CloneAny>);
+
+    #[test]
+    fn test_default() {
+        let map: AnyMap = Default::default();
+        assert_eq!(map.len(), 0);
+    }
 
     #[test]
     fn test_clone() {
-        let mut map: Map<CloneAny> = Map::new();
+        let mut map: Map<dyn CloneAny> = Map::new();
         let _ = map.insert(A(1));
         let _ = map.insert(B(2));
         let _ = map.insert(D(3));
@@ -437,25 +428,25 @@ mod tests {
         fn assert_sync<T: Sync>() { }
         fn assert_clone<T: Clone>() { }
         fn assert_debug<T: ::std::fmt::Debug>() { }
-        assert_send::<Map<Any + Send>>();
-        assert_send::<Map<Any + Send + Sync>>();
-        assert_sync::<Map<Any + Sync>>();
-        assert_sync::<Map<Any + Send + Sync>>();
-        assert_debug::<Map<Any>>();
-        assert_debug::<Map<Any + Send>>();
-        assert_debug::<Map<Any + Sync>>();
-        assert_debug::<Map<Any + Send + Sync>>();
-        assert_send::<Map<CloneAny + Send>>();
-        assert_send::<Map<CloneAny + Send + Sync>>();
-        assert_sync::<Map<CloneAny + Sync>>();
-        assert_sync::<Map<CloneAny + Send + Sync>>();
-        assert_clone::<Map<CloneAny + Send>>();
-        assert_clone::<Map<CloneAny + Send + Sync>>();
-        assert_clone::<Map<CloneAny + Sync>>();
-        assert_clone::<Map<CloneAny + Send + Sync>>();
-        assert_debug::<Map<CloneAny>>();
-        assert_debug::<Map<CloneAny + Send>>();
-        assert_debug::<Map<CloneAny + Sync>>();
-        assert_debug::<Map<CloneAny + Send + Sync>>();
+        assert_send::<Map<dyn Any + Send>>();
+        assert_send::<Map<dyn Any + Send + Sync>>();
+        assert_sync::<Map<dyn Any + Sync>>();
+        assert_sync::<Map<dyn Any + Send + Sync>>();
+        assert_debug::<Map<dyn Any>>();
+        assert_debug::<Map<dyn Any + Send>>();
+        assert_debug::<Map<dyn Any + Sync>>();
+        assert_debug::<Map<dyn Any + Send + Sync>>();
+        assert_send::<Map<dyn CloneAny + Send>>();
+        assert_send::<Map<dyn CloneAny + Send + Sync>>();
+        assert_sync::<Map<dyn CloneAny + Sync>>();
+        assert_sync::<Map<dyn CloneAny + Send + Sync>>();
+        assert_clone::<Map<dyn CloneAny + Send>>();
+        assert_clone::<Map<dyn CloneAny + Send + Sync>>();
+        assert_clone::<Map<dyn CloneAny + Sync>>();
+        assert_clone::<Map<dyn CloneAny + Send + Sync>>();
+        assert_debug::<Map<dyn CloneAny>>();
+        assert_debug::<Map<dyn CloneAny + Send>>();
+        assert_debug::<Map<dyn CloneAny + Sync>>();
+        assert_debug::<Map<dyn CloneAny + Send + Sync>>();
     }
 }