X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/b3811cf0d1bdab6154534eda1903c930885749ec..764038fe6e3f5a28270a874bce2561924a316a20:/src/lib.rs?ds=inline diff --git a/src/lib.rs b/src/lib.rs index bcd5c23..a53a2d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,11 +2,12 @@ #![warn(missing_docs, unused_results)] -use std::any::TypeId; +use std::any::{Any, TypeId}; use std::marker::PhantomData; use raw::RawMap; -use any::{UncheckedAnyExt, IntoBox, Any}; +use any::{UncheckedAnyExt, IntoBox}; +pub use any::CloneAny; macro_rules! impl_common_methods { ( @@ -75,20 +76,27 @@ 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, /// type-safe access to those values. /// /// The type parameter `A` allows you to use a different value type; normally you will want it to -/// be `anymap::any::Any`, but there are other choices: +/// be `std::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` or `+ Send + 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; @@ -304,8 +312,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox> VacantEntry<'a, A, V> { #[cfg(test)] mod tests { - use {Map, AnyMap, Entry}; - use any::{Any, CloneAny}; + use super::*; #[derive(Clone, Debug, PartialEq)] struct A(i32); #[derive(Clone, Debug, PartialEq)] struct B(i32); @@ -387,11 +394,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 +428,20 @@ 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_debug::>(); + assert_debug::>(); + assert_debug::>(); + assert_send::>(); + assert_send::>(); + assert_sync::>(); + assert_clone::>(); + assert_clone::>(); + assert_clone::>(); + assert_debug::>(); + assert_debug::>(); + assert_debug::>(); } }