1 //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
3 #
![crate_id
= "anymap#0.9"]
4 #
![crate_type
= "rlib"]
5 #
![crate_type
= "dylib"]
6 #
![warn(unnecessary_qualification
, non_uppercase_statics
, unsafe_block
,
7 variant_size_difference
, managed_heap_memory
, unnecessary_typecast
,
8 missing_doc
, unused_result
, deprecated_owned_vector
)]
10 use std
::any
::{Any
, AnyRefExt
, AnyMutRefExt
};
11 use std
::intrinsics
::TypeId
;
12 use std
::collections
::HashMap
;
14 /// A map containing zero or one values for any given type and allowing convenient,
15 /// type-safe access to those values.
18 /// # use anymap::AnyMap;
19 /// let mut data = AnyMap::new();
20 /// assert_eq!(data.find(), None::<&int>);
22 /// assert_eq!(data.find(), Some(&42i));
23 /// data.remove::<int>();
24 /// assert_eq!(data.find::<int>(), None);
26 /// #[deriving(PartialEq, Show)]
31 /// assert_eq!(data.find::<Foo>(), None);
32 /// data.insert(Foo { str: "foo".to_string() });
33 /// assert_eq!(data.find(), Some(&Foo { str: "foo".to_string() }));
34 /// data.find_mut::<Foo>().map(|foo| foo.str.push_char('t'));
35 /// assert_eq!(data.find::<Foo>().unwrap().str.as_slice(), "foot");
38 /// Values containing non-static references are not permitted.
40 data
: HashMap
<TypeId
, Box
<Any
>:'
static>,
44 /// Construct a new `AnyMap`.
45 pub fn new() -> AnyMap
{
53 /// Retrieve the value stored in the map for the type `T`, if it exists.
54 pub fn find
<'a
, T
: '
static>(&'a
self) -> Option
<&'a T
> {
55 self.data
.find(&TypeId
::of
::<T
>()).and_then(|any
| any
.as_ref
::<T
>())
58 /// Retrieve a mutable reference to the value stored in the map for the type `T`, if it exists.
59 pub fn find_mut
<'a
, T
: '
static>(&'a
mut self) -> Option
<&'a
mut T
> {
60 self.data
.find_mut(&TypeId
::of
::<T
>()).and_then(|any
| any
.as_mut
::<T
>())
63 /// Set the value contained in the map for the type `T`.
64 /// This will override any previous value stored.
65 pub fn insert
<T
: '
static>(&mut self, value
: T
) {
66 self.data
.insert(TypeId
::of
::<T
>(), box value
as Box
<Any
>:'
static);
69 /// Remove the value for the type `T` if it existed.
70 pub fn remove
<T
: '
static>(&mut self) {
71 self.data
.remove(&TypeId
::of
::<T
>());