X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/8c1b4578cc1e307a7e7fff6152fc904c4e8d4371..330bc5aa1e4b1644a19b3751ee3b65c849447005:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 8340ae2..b677319 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,14 +6,14 @@ #![feature(default_type_params)] #![warn(unnecessary_qualification, non_uppercase_statics, variant_size_difference, managed_heap_memory, unnecessary_typecast, - missing_doc, unused_result, deprecated_owned_vector)] + missing_doc, unused_result)] #[cfg(test)] extern crate test; use std::any::Any; use std::intrinsics::TypeId; -use std::collections::HashMap; +use std::collections::{Collection, HashMap, Mutable}; use std::hash::{Hash, Hasher, Writer}; use std::mem::{transmute, transmute_copy}; use std::raw::TraitObject; @@ -109,7 +109,7 @@ impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any { /// /// Values containing non-static references are not permitted. pub struct AnyMap { - data: HashMap:'static, TypeIdHasher>, + data: HashMap, TypeIdHasher>, } impl AnyMap { @@ -135,7 +135,7 @@ impl AnyMap { /// Set the value contained in the map for the type `T`. /// This will override any previous value stored. pub fn insert(&mut self, value: T) { - self.data.insert(TypeId::of::(), box value as Box:'static); + self.data.insert(TypeId::of::(), box value as Box); } /// Remove the value for the type `T` if it existed. @@ -144,11 +144,29 @@ impl AnyMap { } } +impl Collection for AnyMap { + fn len(&self) -> uint { + self.data.len() + } + + fn is_empty(&self) -> bool { + self.data.is_empty() + } +} + +impl Mutable for AnyMap { + fn clear(&mut self) { + self.data.clear(); + } +} + #[bench] fn bench_insertion(b: &mut ::test::Bencher) { b.iter(|| { let mut data = AnyMap::new(); - data.insert(42i); + for _ in range(0, 100) { + data.insert(42i); + } }) } @@ -156,7 +174,9 @@ fn bench_insertion(b: &mut ::test::Bencher) { fn bench_find_missing(b: &mut ::test::Bencher) { b.iter(|| { let data = AnyMap::new(); - assert_eq!(data.find(), None::<&int>); + for _ in range(0, 100) { + assert_eq!(data.find(), None::<&int>); + } }) } @@ -165,6 +185,9 @@ fn bench_find_present(b: &mut ::test::Bencher) { b.iter(|| { let mut data = AnyMap::new(); data.insert(42i); - assert_eq!(data.find(), Some(&42i)); + // These inner loops are a feeble attempt to drown the other factors. + for _ in range(0, 100) { + assert_eq!(data.find(), Some(&42i)); + } }) }