097b6bd5324efa9e729dfde8655f71887279f221
1 //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
3 #![feature(core, std_misc, hash)]
4 #![cfg_attr(test, feature(test))]
5 #![warn(unused_qualifications, non_upper_case_globals,
6 variant_size_differences
, unused_typecasts
,
7 missing_docs
, unused_results
)]
12 use std
::any
::{Any
, TypeId
};
13 use std
::marker
::PhantomData
;
16 use unchecked_any
::UncheckedAnyExt
;
18 macro_rules
! impl_common_methods
{
20 field
: $t
:ident
.$field
:ident
;
22 with_capacity($with_capacity_arg
:ident
) => $with_capacity
:expr
;
25 /// Create an empty collection.
33 /// Creates an empty collection with the given initial capacity.
35 pub fn with_capacity($with_capacity_arg
: usize) -> $t
{
37 $field
: $with_capacity
,
41 /// Returns the number of elements the collection can hold without reallocating.
43 pub fn capacity(&self) -> usize {
44 self.$field
.capacity()
47 /// Reserves capacity for at least `additional` more elements to be inserted
48 /// in the collection. The collection may reserve more space to avoid
49 /// frequent reallocations.
53 /// Panics if the new allocation size overflows `usize`.
55 pub fn reserve(&mut self, additional
: usize) {
56 self.$field
.reserve(additional
)
59 /// Shrinks the capacity of the collection as much as possible. It will drop
60 /// down as much as possible while maintaining the internal rules
61 /// and possibly leaving some space in accordance with the resize policy.
63 pub fn shrink_to_fit(&mut self) {
64 self.$field
.shrink_to_fit()
67 /// Returns the number of items in the collection.
69 pub fn len(&self) -> usize {
73 /// Returns true if there are no items in the collection.
75 pub fn is_empty(&self) -> bool
{
76 self.$field
.is_empty()
79 /// Removes all items from the collection. Keeps the allocated memory for reuse.
81 pub fn clear(&mut self) {
91 /// A collection containing zero or one values for any given type and allowing convenient,
92 /// type-safe access to those values.
95 /// # use anymap::AnyMap;
96 /// let mut data = AnyMap::new();
97 /// assert_eq!(data.get(), None::<&i32>);
98 /// data.insert(42i32);
99 /// assert_eq!(data.get(), Some(&42i32));
100 /// data.remove::<i32>();
101 /// assert_eq!(data.get::<i32>(), None);
103 /// #[derive(PartialEq, Debug)]
108 /// assert_eq!(data.get::<Foo>(), None);
109 /// data.insert(Foo { str: format!("foo") });
110 /// assert_eq!(data.get(), Some(&Foo { str: format!("foo") }));
111 /// data.get_mut::<Foo>().map(|foo| foo.str.push('t'));
112 /// assert_eq!(&*data.get::<Foo>().unwrap().str, "foot");
115 /// Values containing non-static references are not permitted.
121 impl_common_methods
! {
123 new() => RawAnyMap
::new();
124 with_capacity(capacity
) => RawAnyMap
::with_capacity(capacity
);
128 /// Returns a reference to the value stored in the collection for the type `T`, if it exists.
129 pub fn get
<T
: Any
>(&self) -> Option
<&T
> {
130 self.raw
.get(&TypeId
::of
::<T
>())
131 .map(|any
| unsafe { any
.downcast_ref_unchecked
::<T
>() })
134 /// Returns a mutable reference to the value stored in the collection for the type `T`,
136 pub fn get_mut
<T
: Any
>(&mut self) -> Option
<&mut T
> {
137 self.raw
.get_mut(&TypeId
::of
::<T
>())
138 .map(|any
| unsafe { any
.downcast_mut_unchecked
::<T
>() })
141 /// Sets the value stored in the collection for the type `T`.
142 /// If the collection already had a value of type `T`, that value is returned.
143 /// Otherwise, `None` is returned.
144 pub fn insert
<T
: Any
>(&mut self, value
: T
) -> Option
<T
> {
146 self.raw
.insert(TypeId
::of
::<T
>(), Box
::new(value
))
147 .map(|any
| *any
.downcast_unchecked
::<T
>())
151 /// Removes the `T` value from the collection,
152 /// returning it if there was one or `None` if there was not.
153 pub fn remove
<T
: Any
>(&mut self) -> Option
<T
> {
154 self.raw
.remove(&TypeId
::of
::<T
>())
155 .map(|any
| *unsafe { any
.downcast_unchecked
::<T
>() })
158 /// Returns true if the collection contains a value of type `T`.
160 pub fn contains
<T
: Any
>(&self) -> bool
{
161 self.raw
.contains_key(&TypeId
::of
::<T
>())
164 /// Gets the entry for the given type in the collection for in-place manipulation
165 pub fn entry
<T
: Any
>(&mut self) -> Entry
<T
> {
166 match self.raw
.entry(TypeId
::of
::<T
>()) {
167 raw
::Entry
::Occupied(e
) => Entry
::Occupied(OccupiedEntry
{
171 raw
::Entry
::Vacant(e
) => Entry
::Vacant(VacantEntry
{
178 /// Get a reference to the raw untyped map underlying the `AnyMap`.
180 /// Normal users will not need to use this, but generic libraries working with an `AnyMap` may
181 /// just find a use for it occasionally.
183 pub fn as_raw(&self) -> &RawAnyMap
{
187 /// Get a mutable reference to the raw untyped map underlying the `AnyMap`.
189 /// Normal users will not need to use this, but generic libraries working with an `AnyMap` may
190 /// just find a use for it occasionally.
192 pub fn as_raw_mut(&mut self) -> &mut RawAnyMap
{
196 /// Convert the `AnyMap` into the raw untyped map that underlyies it.
198 /// Normal users will not need to use this, but generic libraries working with an `AnyMap` may
199 /// just find a use for it occasionally.
201 pub fn into_raw(self) -> RawAnyMap
{
205 /// Convert a raw untyped map into an `AnyMap`.
207 /// Normal users will not need to use this, but generic libraries working with an `AnyMap` may
208 /// just find a use for it occasionally.
210 pub fn from_raw(raw
: RawAnyMap
) -> AnyMap
{
217 /// A view into a single occupied location in an `AnyMap`.
218 pub struct OccupiedEntry
<'a
, V
: 'a
> {
219 inner
: raw
::OccupiedEntry
<'a
>,
220 type_
: PhantomData
<V
>,
223 /// A view into a single empty location in an `AnyMap`.
224 pub struct VacantEntry
<'a
, V
: 'a
> {
225 inner
: raw
::VacantEntry
<'a
>,
226 type_
: PhantomData
<V
>,
229 /// A view into a single location in an `AnyMap`, which may be vacant or occupied.
230 pub enum Entry
<'a
, V
: 'a
> {
231 /// An occupied Entry
232 Occupied(OccupiedEntry
<'a
, V
>),
234 Vacant(VacantEntry
<'a
, V
>),
237 impl<'a
, V
: Any
+ Clone
> Entry
<'a
, V
> {
238 /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
239 pub fn get(self) -> Result
<&'a
mut V
, VacantEntry
<'a
, V
>> {
241 Entry
::Occupied(inner
) => Ok(inner
.into_mut()),
242 Entry
::Vacant(inner
) => Err(inner
),
247 impl<'a
, V
: Any
> OccupiedEntry
<'a
, V
> {
248 /// Gets a reference to the value in the entry
249 pub fn get(&self) -> &V
{
250 unsafe { self.inner
.get().downcast_ref_unchecked() }
253 /// Gets a mutable reference to the value in the entry
254 pub fn get_mut(&mut self) -> &mut V
{
255 unsafe { self.inner
.get_mut().downcast_mut_unchecked() }
258 /// Converts the OccupiedEntry into a mutable reference to the value in the entry
259 /// with a lifetime bound to the collection itself
260 pub fn into_mut(self) -> &'a
mut V
{
261 unsafe { self.inner
.into_mut().downcast_mut_unchecked() }
264 /// Sets the value of the entry, and returns the entry's old value
265 pub fn insert(&mut self, value
: V
) -> V
{
266 unsafe { *self.inner
.insert(Box
::new(value
)).downcast_unchecked() }
269 /// Takes the value out of the entry, and returns it
270 pub fn remove(self) -> V
{
271 unsafe { *self.inner
.remove().downcast_unchecked() }
275 impl<'a
, V
: Any
> VacantEntry
<'a
, V
> {
276 /// Sets the value of the entry with the VacantEntry's key,
277 /// and returns a mutable reference to it
278 pub fn insert(self, value
: V
) -> &'a
mut V
{
279 unsafe { self.inner
.insert(Box
::new(value
)).downcast_mut_unchecked() }
284 fn bench_insertion(b
: &mut ::test
::Bencher
) {
286 let mut data
= AnyMap
::new();
288 let _
= data
.insert(42);
294 fn bench_get_missing(b
: &mut ::test
::Bencher
) {
296 let data
= AnyMap
::new();
298 assert_eq!(data
.get(), None
::<&i32>);
304 fn bench_get_present(b
: &mut ::test
::Bencher
) {
306 let mut data
= AnyMap
::new();
307 let _
= data
.insert(42);
308 // These inner loops are a feeble attempt to drown the other factors.
310 assert_eq!(data
.get(), Some(&42));
317 #[derive(Debug, PartialEq)] struct A(i32);
318 #[derive(Debug, PartialEq)] struct B(i32);
319 #[derive(Debug, PartialEq)] struct C(i32);
320 #[derive(Debug, PartialEq)] struct D(i32);
321 #[derive(Debug, PartialEq)] struct E(i32);
322 #[derive(Debug, PartialEq)] struct F(i32);
323 #[derive(Debug, PartialEq)] struct J(i32);
325 let mut map
: AnyMap
= AnyMap
::new();
326 assert_eq!(map
.insert(A(10)), None
);
327 assert_eq!(map
.insert(B(20)), None
);
328 assert_eq!(map
.insert(C(30)), None
);
329 assert_eq!(map
.insert(D(40)), None
);
330 assert_eq!(map
.insert(E(50)), None
);
331 assert_eq!(map
.insert(F(60)), None
);
333 // Existing key (insert)
334 match map
.entry
::<A
>() {
335 Entry
::Vacant(_
) => unreachable!(),
336 Entry
::Occupied(mut view
) => {
337 assert_eq!(view
.get(), &A(10));
338 assert_eq!(view
.insert(A(100)), A(10));
341 assert_eq!(map
.get
::<A
>().unwrap(), &A(100));
342 assert_eq!(map
.len(), 6);
345 // Existing key (update)
346 match map
.entry
::<B
>() {
347 Entry
::Vacant(_
) => unreachable!(),
348 Entry
::Occupied(mut view
) => {
349 let v
= view
.get_mut();
350 let new_v
= B(v
.0 * 10);
354 assert_eq!(map
.get().unwrap(), &B(200));
355 assert_eq!(map
.len(), 6);
358 // Existing key (remove)
359 match map
.entry
::<C
>() {
360 Entry
::Vacant(_
) => unreachable!(),
361 Entry
::Occupied(view
) => {
362 assert_eq!(view
.remove(), C(30));
365 assert_eq!(map
.get
::<C
>(), None
);
366 assert_eq!(map
.len(), 5);
369 // Inexistent key (insert)
370 match map
.entry
::<J
>() {
371 Entry
::Occupied(_
) => unreachable!(),
372 Entry
::Vacant(view
) => {
373 assert_eq!(*view
.insert(J(1000)), J(1000));
376 assert_eq!(map
.get
::<J
>().unwrap(), &J(1000));
377 assert_eq!(map
.len(), 6);