8bc9a178a054ed6fc43c36d5f9b83d38bd997310
[anymap] / src / lib.rs
1 //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
2
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)]
9
10 use std::any::{Any, AnyRefExt, AnyMutRefExt};
11 use std::intrinsics::TypeId;
12 use std::collections::HashMap;
13
14 /// A map containing zero or one values for any given type and allowing convenient,
15 /// type-safe access to those values.
16 ///
17 /// ```rust
18 /// # use anymap::AnyMap;
19 /// let mut data = AnyMap::new();
20 /// assert_eq!(data.find(), None::<&int>);
21 /// data.insert(42i);
22 /// assert_eq!(data.find(), Some(&42i));
23 /// data.remove::<int>();
24 /// assert_eq!(data.find::<int>(), None);
25 ///
26 /// #[deriving(PartialEq, Show)]
27 /// struct Foo {
28 /// str: String,
29 /// }
30 ///
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");
36 /// ```
37 ///
38 /// Values containing non-static references are not permitted.
39 pub struct AnyMap {
40 data: HashMap<TypeId, Box<Any>:'static>,
41 }
42
43 impl AnyMap {
44 /// Construct a new `AnyMap`.
45 pub fn new() -> AnyMap {
46 AnyMap {
47 data: HashMap::new(),
48 }
49 }
50 }
51
52 impl 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>())
56 }
57
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>())
61 }
62
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);
67 }
68
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>());
72 }
73 }