X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/d51aff506409d7ffa2de275cd4e3029a88de1e2a..7dc36adf24d48394bcb9d0aa802746e3d4a1f564:/src/lib.rs?ds=inline diff --git a/src/lib.rs b/src/lib.rs index d0bce3c..0c71e36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,7 @@ //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type. -#![crate_id = "anymap#0.9"] -#![crate_type = "rlib"] -#![crate_type = "dylib"] +#![crate_name = "anymap"] +#![crate_type = "lib"] #![feature(default_type_params)] #![warn(unnecessary_qualification, non_uppercase_statics, variant_size_difference, managed_heap_memory, unnecessary_typecast, @@ -13,7 +12,7 @@ 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 +108,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 { @@ -142,13 +141,34 @@ impl AnyMap { pub fn remove(&mut self) { self.data.remove(&TypeId::of::()); } + + /// Does a value of type `T` exist? + pub fn contains(&self) -> bool { + self.data.contains_key(&TypeId::of::()) + } +} + +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(); - for _ in range(0, 100) { + for _ in range(0u, 100) { data.insert(42i); } }) @@ -158,7 +178,7 @@ fn bench_insertion(b: &mut ::test::Bencher) { fn bench_find_missing(b: &mut ::test::Bencher) { b.iter(|| { let data = AnyMap::new(); - for _ in range(0, 100) { + for _ in range(0u, 100) { assert_eq!(data.find(), None::<&int>); } }) @@ -170,7 +190,7 @@ fn bench_find_present(b: &mut ::test::Bencher) { let mut data = AnyMap::new(); data.insert(42i); // These inner loops are a feeble attempt to drown the other factors. - for _ in range(0, 100) { + for _ in range(0u, 100) { assert_eq!(data.find(), Some(&42i)); } })