X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/036d7b0231a968f1d5ae14df49c5aa670423999e..63e4c18ed7748325476eaed3a11e1496b9bcc932:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 4c0e9da..e766913 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,18 @@ //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type. -#![crate_id = "anymap#0.9.0"] -#![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, - missing_doc, unused_result)] +#![warn(unused_qualifications, non_upper_case_globals, + variant_size_differences, unused_typecasts, + missing_docs, unused_results)] #[cfg(test)] extern crate test; use std::any::Any; use std::intrinsics::TypeId; -use std::collections::{Collection, HashMap, Mutable}; +use std::collections::HashMap; use std::hash::{Hash, Hasher, Writer}; use std::mem::{transmute, transmute_copy}; use std::raw::TraitObject; @@ -38,7 +37,7 @@ impl Writer for TypeIdState { } impl Hasher for TypeIdHasher { - fn hash>(&self, value: &T) -> u64 { + fn hash>(&self, value: &T) -> u64 { let mut state = TypeIdState { value: 0, }; @@ -54,7 +53,7 @@ trait UncheckedAnyRefExt<'a> { unsafe fn as_ref_unchecked(self) -> &'a T; } -impl<'a> UncheckedAnyRefExt<'a> for &'a Any { +impl<'a> UncheckedAnyRefExt<'a> for &'a Any + 'a { #[inline] unsafe fn as_ref_unchecked(self) -> &'a T { // Get the raw representation of the trait object @@ -72,7 +71,7 @@ trait UncheckedAnyMutRefExt<'a> { unsafe fn as_mut_unchecked(self) -> &'a mut T; } -impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any { +impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any + 'a { #[inline] unsafe fn as_mut_unchecked(self) -> &'a mut T { // Get the raw representation of the trait object @@ -103,13 +102,13 @@ impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any { /// assert_eq!(data.find::(), None); /// data.insert(Foo { str: "foo".to_string() }); /// assert_eq!(data.find(), Some(&Foo { str: "foo".to_string() })); -/// data.find_mut::().map(|foo| foo.str.push_char('t')); +/// data.find_mut::().map(|foo| foo.str.push('t')); /// assert_eq!(data.find::().unwrap().str.as_slice(), "foot"); /// ``` /// /// Values containing non-static references are not permitted. pub struct AnyMap { - data: HashMap, TypeIdHasher>, + data: HashMap, TypeIdHasher>, } impl AnyMap { @@ -123,12 +122,12 @@ impl AnyMap { impl AnyMap { /// Retrieve the value stored in the map for the type `T`, if it exists. - pub fn find<'a, T: 'static>(&'a self) -> Option<&'a T> { + pub fn find(&self) -> Option<&T> { self.data.find(&TypeId::of::()).map(|any| unsafe { any.as_ref_unchecked::() }) } /// Retrieve a mutable reference to the value stored in the map for the type `T`, if it exists. - pub fn find_mut<'a, T: 'static>(&'a mut self) -> Option<&'a mut T> { + pub fn find_mut(&mut self) -> Option<&mut T> { self.data.find_mut(&TypeId::of::()).map(|any| unsafe { any.as_mut_unchecked::() }) } @@ -142,20 +141,24 @@ impl AnyMap { pub fn remove(&mut self) { self.data.remove(&TypeId::of::()); } -} -impl Collection for AnyMap { - fn len(&self) -> uint { + /// Does a value of type `T` exist? + pub fn contains(&self) -> bool { + self.data.contains_key(&TypeId::of::()) + } + + /// Returns the number of items in the collection. + pub fn len(&self) -> uint { self.data.len() } - fn is_empty(&self) -> bool { + /// Returns true if there are no items in the collection. + pub fn is_empty(&self) -> bool { self.data.is_empty() } -} -impl Mutable for AnyMap { - fn clear(&mut self) { + /// Removes all items from the collection. + pub fn clear(&mut self) { self.data.clear(); } } @@ -164,7 +167,7 @@ impl Mutable for AnyMap { 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); } }) @@ -174,7 +177,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>); } }) @@ -186,7 +189,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)); } })