X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/b3811cf0d1bdab6154534eda1903c930885749ec..b07b62fd4d84fe1eedaba6c89c15990ced5089e6:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index bcd5c23..3f656e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,8 @@ use std::any::TypeId; use std::marker::PhantomData; use raw::RawMap; -use any::{UncheckedAnyExt, IntoBox, Any}; +use any::{UncheckedAnyExt, IntoBox}; +pub use any::{Any, CloneAny}; macro_rules! impl_common_methods { ( @@ -75,10 +76,17 @@ macro_rules! impl_common_methods { self.$field.clear() } } + + impl Default for $t { + #[inline] + fn default() -> $t { + $t::new() + } + } } } -pub mod any; +mod any; pub mod raw; /// A collection containing zero or one values for any given type and allowing convenient, @@ -88,7 +96,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`) to add those bounds. +/// - You can add on `+ Send` and/or `+ Sync` (e.g. `Map`) to add those bounds. /// /// ```rust /// # use anymap::AnyMap; @@ -113,7 +121,7 @@ pub mod raw; /// /// Values containing non-static references are not permitted. #[derive(Debug)] -pub struct Map { +pub struct Map { raw: RawMap, } @@ -132,7 +140,7 @@ impl Clone for Map where Box: Clone { /// Why is this a separate type alias rather than a default value for `Map`? `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; +pub type AnyMap = Map; impl_common_methods! { field: Map.raw; @@ -387,11 +395,17 @@ mod tests { } test_entry!(test_entry_any, AnyMap); - test_entry!(test_entry_cloneany, Map); + test_entry!(test_entry_cloneany, Map); + + #[test] + fn test_default() { + let map: AnyMap = Default::default(); + assert_eq!(map.len(), 0); + } #[test] fn test_clone() { - let mut map: Map = Map::new(); + let mut map: Map = Map::new(); let _ = map.insert(A(1)); let _ = map.insert(B(2)); let _ = map.insert(D(3)); @@ -415,25 +429,25 @@ mod tests { fn assert_sync() { } fn assert_clone() { } fn assert_debug() { } - assert_send::>(); - assert_send::>(); - assert_sync::>(); - assert_sync::>(); - assert_debug::>(); - assert_debug::>(); - assert_debug::>(); - assert_debug::>(); - assert_send::>(); - assert_send::>(); - assert_sync::>(); - assert_sync::>(); - assert_clone::>(); - assert_clone::>(); - assert_clone::>(); - assert_clone::>(); - assert_debug::>(); - assert_debug::>(); - assert_debug::>(); - assert_debug::>(); + assert_send::>(); + assert_send::>(); + assert_sync::>(); + assert_sync::>(); + assert_debug::>(); + assert_debug::>(); + assert_debug::>(); + assert_debug::>(); + assert_send::>(); + assert_send::>(); + assert_sync::>(); + assert_sync::>(); + assert_clone::>(); + assert_clone::>(); + assert_clone::>(); + assert_clone::>(); + assert_debug::>(); + assert_debug::>(); + assert_debug::>(); + assert_debug::>(); } }