//! 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,
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T;
}
-impl<'a> UncheckedAnyRefExt<'a> for &'a Any {
+impl<'a> UncheckedAnyRefExt<'a> for &'a Any + 'a {
#[inline]
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T {
// Get the raw representation of the trait object
unsafe fn as_mut_unchecked<T: 'static>(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<T: 'static>(self) -> &'a mut T {
// Get the raw representation of the trait object
/// assert_eq!(data.find::<Foo>(), None);
/// data.insert(Foo { str: "foo".to_string() });
/// assert_eq!(data.find(), Some(&Foo { str: "foo".to_string() }));
-/// data.find_mut::<Foo>().map(|foo| foo.str.push_char('t'));
+/// data.find_mut::<Foo>().map(|foo| foo.str.push('t'));
/// assert_eq!(data.find::<Foo>().unwrap().str.as_slice(), "foot");
/// ```
///
/// Values containing non-static references are not permitted.
pub struct AnyMap {
- data: HashMap<TypeId, Box<Any>, TypeIdHasher>,
+ data: HashMap<TypeId, Box<Any + 'static>, TypeIdHasher>,
}
impl AnyMap {
pub fn remove<T: 'static>(&mut self) {
self.data.remove(&TypeId::of::<T>());
}
+
+ /// Does a value of type `T` exist?
+ pub fn contains<T: 'static>(&self) -> bool {
+ self.data.contains_key(&TypeId::of::<T>())
+ }
}
impl Collection 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);
}
})
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>);
}
})
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));
}
})