1 //! The raw form of a `Map`, allowing untyped access.
3 //! All relevant details are in the `RawMap` struct.
6 use std
::borrow
::Borrow
;
7 use std
::collections
::hash_map
::{self, HashMap
};
9 use std
::hash
::{Hasher
, BuildHasherDefault
};
11 use std
::ops
::{Index
, IndexMut
};
14 use any
::{Any
, UncheckedAnyExt
};
21 impl Hasher
for TypeIdHasher
{
23 fn write(&mut self, bytes
: &[u8]) {
24 // This expects to receive one and exactly one 64-bit value
25 debug_assert!(bytes
.len() == 8);
27 ptr
::copy_nonoverlapping(&mut self.value
, mem
::transmute(&bytes
[0]), 1)
32 fn finish(&self) -> u64 { self.value
}
35 /// The raw, underlying form of a `Map`.
37 /// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that
38 /// would be memory-unsafe removed or marked unsafe. Normal people are expected to use the safe
39 /// `Map` interface instead, but there is the occasional use for this such as iteration over the
40 /// contents of an `Map`. However, because you will then be dealing with `Any` trait objects, it
41 /// doesn’t tend to be so very useful. Still, if you need it, it’s here.
43 pub struct RawMap
<A
: ?Sized
+ UncheckedAnyExt
= Any
> {
44 inner
: HashMap
<TypeId
, Box
<A
>, BuildHasherDefault
<TypeIdHasher
>>,
47 // #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box<A> that can.
48 impl<A
: ?Sized
+ UncheckedAnyExt
> Clone
for RawMap
<A
> where Box
<A
>: Clone
{
49 fn clone(&self) -> RawMap
<A
> {
51 inner
: self.inner
.clone(),
56 impl<A
: ?Sized
+ UncheckedAnyExt
> Default
for RawMap
<A
> {
57 fn default() -> RawMap
<A
> {
62 impl_common_methods
! {
64 new() => HashMap
::with_hasher(Default
::default());
65 with_capacity(capacity
) => HashMap
::with_capacity_and_hasher(capacity
, Default
::default());
70 pub struct Iter
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
71 inner
: hash_map
::Iter
<'a
, TypeId
, Box
<A
>>,
73 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for Iter
<'a
, A
> {
75 #[inline] fn next(&mut self) -> Option<&'a A> { self.inner.next().map(|x| &**x.1) }
76 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
78 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for Iter
<'a
, A
> {
79 #[inline] fn len(&self) -> usize { self.inner.len() }
82 /// RawMap mutable iterator.
83 pub struct IterMut
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
84 inner
: hash_map
::IterMut
<'a
, TypeId
, Box
<A
>>,
86 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for IterMut
<'a
, A
> {
87 type Item
= &'a
mut A
;
88 #[inline] fn next(&mut self) -> Option<&'a mut A> { self.inner.next().map(|x| &mut **x.1) }
89 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
91 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for IterMut
<'a
, A
> {
92 #[inline] fn len(&self) -> usize { self.inner.len() }
95 /// RawMap move iterator.
96 pub struct IntoIter
<A
: ?Sized
+ UncheckedAnyExt
> {
97 inner
: hash_map
::IntoIter
<TypeId
, Box
<A
>>,
99 impl<A
: ?Sized
+ UncheckedAnyExt
> Iterator
for IntoIter
<A
> {
101 #[inline] fn next(&mut self) -> Option<Box<A>> { self.inner.next().map(|x| x.1) }
102 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
104 impl<A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for IntoIter
<A
> {
105 #[inline] fn len(&self) -> usize { self.inner.len() }
108 /// RawMap drain iterator.
109 #[cfg(feature = "unstable")]
110 pub struct Drain
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
111 inner
: hash_map
::Drain
<'a
, TypeId
, Box
<A
>>,
113 #[cfg(feature = "unstable")]
114 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for Drain
<'a
, A
> {
116 #[inline] fn next(&mut self) -> Option<Box<A>> { self.inner.next().map(|x| x.1) }
117 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
119 #[cfg(feature = "unstable")]
120 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for Drain
<'a
, A
> {
121 #[inline] fn len(&self) -> usize { self.inner.len() }
124 impl<A
: ?Sized
+ UncheckedAnyExt
> RawMap
<A
> {
125 /// An iterator visiting all entries in arbitrary order.
127 /// Iterator element type is `&Any`.
129 pub fn iter(&self) -> Iter
<A
> {
131 inner
: self.inner
.iter(),
135 /// An iterator visiting all entries in arbitrary order.
137 /// Iterator element type is `&mut Any`.
139 pub fn iter_mut(&mut self) -> IterMut
<A
> {
141 inner
: self.inner
.iter_mut(),
145 /// Clears the map, returning all items as an iterator.
147 /// Iterator element type is `Box<Any>`.
149 /// Keeps the allocated memory for reuse.
151 #[cfg(feature = "unstable")]
152 pub fn drain(&mut self) -> Drain
<A
> {
154 inner
: self.inner
.drain(),
158 /// Gets the entry for the given type in the collection for in-place manipulation.
159 pub fn entry(&mut self, key
: TypeId
) -> Entry
<A
> {
160 match self.inner
.entry(key
) {
161 hash_map
::Entry
::Occupied(e
) => Entry
::Occupied(OccupiedEntry
{
164 hash_map
::Entry
::Vacant(e
) => Entry
::Vacant(VacantEntry
{
170 /// Returns a reference to the value corresponding to the key.
172 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
173 /// form *must* match those for the key type.
174 pub fn get
<Q
: ?Sized
>(&self, k
: &Q
) -> Option
<&A
>
175 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
176 self.inner
.get(k
).map(|x
| &**x
)
179 /// Returns true if the map contains a value for the specified key.
181 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
182 /// form *must* match those for the key type.
183 pub fn contains_key
<Q
: ?Sized
>(&self, k
: &Q
) -> bool
184 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
185 self.inner
.contains_key(k
)
188 /// Returns a mutable reference to the value corresponding to the key.
190 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
191 /// form *must* match those for the key type.
192 pub fn get_mut
<Q
: ?Sized
>(&mut self, k
: &Q
) -> Option
<&mut A
>
193 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
194 self.inner
.get_mut(k
).map(|x
| &mut **x
)
197 /// Inserts a key-value pair from the map. If the key already had a value present in the map,
198 /// that value is returned. Otherwise, None is returned.
200 /// It is the caller’s responsibility to ensure that the key corresponds with the type ID of
201 /// the value. If they do not, memory safety may be violated.
202 pub unsafe fn insert(&mut self, key
: TypeId
, value
: Box
<A
>) -> Option
<Box
<A
>> {
203 self.inner
.insert(key
, value
)
206 /// Removes a key from the map, returning the value at the key if the key was previously in the
209 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
210 /// form *must* match those for the key type.
211 pub fn remove
<Q
: ?Sized
>(&mut self, k
: &Q
) -> Option
<Box
<A
>>
212 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
218 impl<A
: ?Sized
+ UncheckedAnyExt
, Q
> Index
<Q
> for RawMap
<A
> where TypeId
: Borrow
<Q
>, Q
: Eq
+ Hash
{
221 fn index
<'a
>(&'a
self, index
: Q
) -> &'a A
{
222 self.get(&index
).expect("no entry found for key")
226 impl<A
: ?Sized
+ UncheckedAnyExt
, Q
> IndexMut
<Q
> for RawMap
<A
> where TypeId
: Borrow
<Q
>, Q
: Eq
+ Hash
{
227 fn index_mut
<'a
>(&'a
mut self, index
: Q
) -> &'a
mut A
{
228 self.get_mut(&index
).expect("no entry found for key")
232 impl<A
: ?Sized
+ UncheckedAnyExt
> IntoIterator
for RawMap
<A
> {
234 type IntoIter
= IntoIter
<A
>;
236 fn into_iter(self) -> IntoIter
<A
> {
238 inner
: self.inner
.into_iter(),
243 /// A view into a single occupied location in a `RawMap`.
244 pub struct OccupiedEntry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
245 inner
: hash_map
::OccupiedEntry
<'a
, TypeId
, Box
<A
>>,
248 /// A view into a single empty location in a `RawMap`.
249 pub struct VacantEntry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
250 inner
: hash_map
::VacantEntry
<'a
, TypeId
, Box
<A
>>,
253 /// A view into a single location in a `RawMap`, which may be vacant or occupied.
254 pub enum Entry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
255 /// An occupied Entry
256 Occupied(OccupiedEntry
<'a
, A
>),
258 Vacant(VacantEntry
<'a
, A
>),
261 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Entry
<'a
, A
> {
262 /// Ensures a value is in the entry by inserting the default if empty, and returns
263 /// a mutable reference to the value in the entry.
265 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
266 /// the type ID of `value`. If they do not, memory safety may be violated.
267 pub unsafe fn or_insert(self, default: Box
<A
>) -> &'a
mut A
{
269 Entry
::Occupied(inner
) => inner
.into_mut(),
270 Entry
::Vacant(inner
) => inner
.insert(default),
274 /// Ensures a value is in the entry by inserting the result of the default function if empty,
275 /// and returns a mutable reference to the value in the entry.
277 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
278 /// the type ID of `value`. If they do not, memory safety may be violated.
279 pub unsafe fn or_insert_with
<F
: FnOnce() -> Box
<A
>>(self, default: F
) -> &'a
mut A
{
281 Entry
::Occupied(inner
) => inner
.into_mut(),
282 Entry
::Vacant(inner
) => inner
.insert(default()),
287 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> OccupiedEntry
<'a
, A
> {
288 /// Gets a reference to the value in the entry.
289 pub fn get(&self) -> &A
{
293 /// Gets a mutable reference to the value in the entry.
294 pub fn get_mut(&mut self) -> &mut A
{
295 &mut **self.inner
.get_mut()
298 /// Converts the OccupiedEntry into a mutable reference to the value in the entry
299 /// with a lifetime bound to the collection itself.
300 pub fn into_mut(self) -> &'a
mut A
{
301 &mut **self.inner
.into_mut()
304 /// Sets the value of the entry, and returns the entry's old value.
306 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
307 /// the type ID of `value`. If they do not, memory safety may be violated.
308 pub unsafe fn insert(&mut self, value
: Box
<A
>) -> Box
<A
> {
309 self.inner
.insert(value
)
312 /// Takes the value out of the entry, and returns it.
313 pub fn remove(self) -> Box
<A
> {
318 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> VacantEntry
<'a
, A
> {
319 /// Sets the value of the entry with the VacantEntry's key,
320 /// and returns a mutable reference to it
322 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
323 /// the type ID of `value`. If they do not, memory safety may be violated.
324 pub unsafe fn insert(self, value
: Box
<A
>) -> &'a
mut A
{
325 &mut **self.inner
.insert(value
)