X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/6daea5338511debd17890e14082d6b8a4c00a368..3210c8082befc8d7a5a0626e0b06413ac832a56c:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 1fae987..0a1a692 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,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 @@ -71,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 @@ -102,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 { @@ -141,6 +141,11 @@ 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 {