3d1be50333c1951af280cbeac78356cab0b6f3c9
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
};
8 #
[cfg(feature
= "unstable")]
9 use std
::collections
::hash_state
::HashState
;
11 #
[cfg(feature
= "unstable")]
12 use std
::hash
::Hasher
;
13 #
[cfg(feature
= "unstable")]
15 use std
::ops
::{Index
, IndexMut
};
16 #
[cfg(feature
= "unstable")]
19 use any
::{Any
, UncheckedAnyExt
};
21 #
[cfg(feature
= "unstable")]
27 #
[cfg(feature
= "unstable")]
30 #
[cfg(feature
= "unstable")]
31 impl HashState
for TypeIdState
{
32 type Hasher
= TypeIdHasher
;
34 fn hasher(&self) -> TypeIdHasher
{
35 TypeIdHasher
{ value
: 0 }
39 #
[cfg(feature
= "unstable")]
40 impl Hasher
for TypeIdHasher
{
42 fn write(&mut self, bytes
: &[u8]) {
43 // This expects to receive one and exactly one 64-bit value
44 debug_assert!(bytes
.len() == 8);
46 ptr
::copy_nonoverlapping(&mut self.value
, mem
::transmute(&bytes
[0]), 1)
51 fn finish(&self) -> u64 { self.value
}
55 /// The raw, underlying form of a `Map`.
57 /// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with the portions that
58 /// would be memory-unsafe removed or marked unsafe. Normal people are expected to use the safe
59 /// `Map` interface instead, but there is the occasional use for this such as iteration over the
60 /// contents of an `Map`. However, because you will then be dealing with `Any` trait objects, it
61 /// doesn’t tend to be so very useful. Still, if you need it, it’s here.
63 pub struct RawMap
<A
: ?Sized
+ UncheckedAnyExt
= Any
> {
64 #
[cfg(feature
= "unstable")]
65 inner
: HashMap
<TypeId
, Box
<A
>, TypeIdState
>,
67 #
[cfg(not(feature
= "unstable"))]
68 inner
: HashMap
<TypeId
, Box
<A
>>,
71 // #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box<A> that can.
72 impl<A
: ?Sized
+ UncheckedAnyExt
> Clone
for RawMap
<A
> where Box
<A
>: Clone
{
73 fn clone(&self) -> RawMap
<A
> {
75 inner
: self.inner
.clone(),
80 impl<A
: ?Sized
+ UncheckedAnyExt
> Default
for RawMap
<A
> {
81 fn default() -> RawMap
<A
> {
86 #
[cfg(feature
= "unstable")]
87 impl_common_methods
! {
89 new() => HashMap
::with_hash_state(TypeIdState
);
90 with_capacity(capacity
) => HashMap
::with_capacity_and_hash_state(capacity
, TypeIdState
);
93 #
[cfg(not(feature
= "unstable"))]
94 impl_common_methods
! {
96 new() => HashMap
::new();
97 with_capacity(capacity
) => HashMap
::with_capacity(capacity
);
102 pub struct Iter
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
103 inner
: hash_map
::Iter
<'a
, TypeId
, Box
<A
>>,
105 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for Iter
<'a
, A
> {
107 #
[inline
] fn next(&mut self) -> Option
<&'a A
> { self.inner
.next().map(|x
| &**x
.1) }
108 #
[inline
] fn size_hint(&self) -> (usize, Option
<usize>) { self.inner
.size_hint() }
110 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for Iter
<'a
, A
> {
111 #
[inline
] fn len(&self) -> usize { self.inner
.len() }
114 /// RawMap mutable iterator.
115 pub struct IterMut
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
116 inner
: hash_map
::IterMut
<'a
, TypeId
, Box
<A
>>,
118 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for IterMut
<'a
, A
> {
119 type Item
= &'a
mut A
;
120 #
[inline
] fn next(&mut self) -> Option
<&'a
mut A
> { self.inner
.next().map(|x
| &mut **x
.1) }
121 #
[inline
] fn size_hint(&self) -> (usize, Option
<usize>) { self.inner
.size_hint() }
123 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for IterMut
<'a
, A
> {
124 #
[inline
] fn len(&self) -> usize { self.inner
.len() }
127 /// RawMap move iterator.
128 pub struct IntoIter
<A
: ?Sized
+ UncheckedAnyExt
> {
129 inner
: hash_map
::IntoIter
<TypeId
, Box
<A
>>,
131 impl<A
: ?Sized
+ UncheckedAnyExt
> Iterator
for IntoIter
<A
> {
133 #
[inline
] fn next(&mut self) -> Option
<Box
<A
>> { self.inner
.next().map(|x
| x
.1) }
134 #
[inline
] fn size_hint(&self) -> (usize, Option
<usize>) { self.inner
.size_hint() }
136 impl<A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for IntoIter
<A
> {
137 #
[inline
] fn len(&self) -> usize { self.inner
.len() }
140 /// RawMap drain iterator.
141 #
[cfg(feature
= "unstable")]
142 pub struct Drain
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
143 inner
: hash_map
::Drain
<'a
, TypeId
, Box
<A
>>,
145 #
[cfg(feature
= "unstable")]
146 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Iterator
for Drain
<'a
, A
> {
148 #
[inline
] fn next(&mut self) -> Option
<Box
<A
>> { self.inner
.next().map(|x
| x
.1) }
149 #
[inline
] fn size_hint(&self) -> (usize, Option
<usize>) { self.inner
.size_hint() }
151 #
[cfg(feature
= "unstable")]
152 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> ExactSizeIterator
for Drain
<'a
, A
> {
153 #
[inline
] fn len(&self) -> usize { self.inner
.len() }
156 impl<A
: ?Sized
+ UncheckedAnyExt
> RawMap
<A
> {
157 /// An iterator visiting all entries in arbitrary order.
159 /// Iterator element type is `&Any`.
161 pub fn iter(&self) -> Iter
<A
> {
163 inner
: self.inner
.iter(),
167 /// An iterator visiting all entries in arbitrary order.
169 /// Iterator element type is `&mut Any`.
171 pub fn iter_mut(&mut self) -> IterMut
<A
> {
173 inner
: self.inner
.iter_mut(),
177 /// Clears the map, returning all items as an iterator.
179 /// Iterator element type is `Box<Any>`.
181 /// Keeps the allocated memory for reuse.
183 #
[cfg(feature
= "unstable")]
184 pub fn drain(&mut self) -> Drain
<A
> {
186 inner
: self.inner
.drain(),
190 /// Gets the entry for the given type in the collection for in-place manipulation.
191 pub fn entry(&mut self, key
: TypeId
) -> Entry
<A
> {
192 match self.inner
.entry(key
) {
193 hash_map
::Entry
::Occupied(e
) => Entry
::Occupied(OccupiedEntry
{
196 hash_map
::Entry
::Vacant(e
) => Entry
::Vacant(VacantEntry
{
202 /// Returns a reference to the value corresponding to the key.
204 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
205 /// form *must* match those for the key type.
206 pub fn get
<Q
: ?Sized
>(&self, k
: &Q
) -> Option
<&A
>
207 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
208 self.inner
.get(k
).map(|x
| &**x
)
211 /// Returns true if the map contains a value for the specified key.
213 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
214 /// form *must* match those for the key type.
215 pub fn contains_key
<Q
: ?Sized
>(&self, k
: &Q
) -> bool
216 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
217 self.inner
.contains_key(k
)
220 /// Returns a mutable reference to the value corresponding to the key.
222 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
223 /// form *must* match those for the key type.
224 pub fn get_mut
<Q
: ?Sized
>(&mut self, k
: &Q
) -> Option
<&mut A
>
225 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
226 self.inner
.get_mut(k
).map(|x
| &mut **x
)
229 /// Inserts a key-value pair from the map. If the key already had a value present in the map,
230 /// that value is returned. Otherwise, None is returned.
232 /// It is the caller’s responsibility to ensure that the key corresponds with the type ID of
233 /// the value. If they do not, memory safety may be violated.
234 pub unsafe fn insert(&mut self, key
: TypeId
, value
: Box
<A
>) -> Option
<Box
<A
>> {
235 self.inner
.insert(key
, value
)
238 /// Removes a key from the map, returning the value at the key if the key was previously in the
241 /// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
242 /// form *must* match those for the key type.
243 pub fn remove
<Q
: ?Sized
>(&mut self, k
: &Q
) -> Option
<Box
<A
>>
244 where TypeId
: Borrow
<Q
>, Q
: Hash
+ Eq
{
250 impl<A
: ?Sized
+ UncheckedAnyExt
, Q
> Index
<Q
> for RawMap
<A
> where TypeId
: Borrow
<Q
>, Q
: Eq
+ Hash
{
253 fn index
<'a
>(&'a
self, index
: Q
) -> &'a A
{
254 self.get(&index
).expect("no entry found for key")
258 impl<A
: ?Sized
+ UncheckedAnyExt
, Q
> IndexMut
<Q
> for RawMap
<A
> where TypeId
: Borrow
<Q
>, Q
: Eq
+ Hash
{
259 fn index_mut
<'a
>(&'a
mut self, index
: Q
) -> &'a
mut A
{
260 self.get_mut(&index
).expect("no entry found for key")
264 impl<A
: ?Sized
+ UncheckedAnyExt
> IntoIterator
for RawMap
<A
> {
266 type IntoIter
= IntoIter
<A
>;
268 fn into_iter(self) -> IntoIter
<A
> {
270 inner
: self.inner
.into_iter(),
275 /// A view into a single occupied location in a `RawMap`.
276 pub struct OccupiedEntry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
277 inner
: hash_map
::OccupiedEntry
<'a
, TypeId
, Box
<A
>>,
280 /// A view into a single empty location in a `RawMap`.
281 pub struct VacantEntry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
282 inner
: hash_map
::VacantEntry
<'a
, TypeId
, Box
<A
>>,
285 /// A view into a single location in a `RawMap`, which may be vacant or occupied.
286 pub enum Entry
<'a
, A
: ?Sized
+ UncheckedAnyExt
> {
287 /// An occupied Entry
288 Occupied(OccupiedEntry
<'a
, A
>),
290 Vacant(VacantEntry
<'a
, A
>),
293 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> Entry
<'a
, A
> {
294 /// Ensures a value is in the entry by inserting the default if empty, and returns
295 /// a mutable reference to the value in the entry.
297 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
298 /// the type ID of `value`. If they do not, memory safety may be violated.
299 pub unsafe fn or_insert(self, default: Box
<A
>) -> &'a
mut A
{
301 Entry
::Occupied(inner
) => inner
.into_mut(),
302 Entry
::Vacant(inner
) => inner
.insert(default),
306 /// Ensures a value is in the entry by inserting the result of the default function if empty,
307 /// and returns a mutable reference to the value in the entry.
309 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
310 /// the type ID of `value`. If they do not, memory safety may be violated.
311 pub unsafe fn or_insert_with
<F
: FnOnce() -> Box
<A
>>(self, default: F
) -> &'a
mut A
{
313 Entry
::Occupied(inner
) => inner
.into_mut(),
314 Entry
::Vacant(inner
) => inner
.insert(default()),
319 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> OccupiedEntry
<'a
, A
> {
320 /// Gets a reference to the value in the entry.
321 pub fn get(&self) -> &A
{
325 /// Gets a mutable reference to the value in the entry.
326 pub fn get_mut(&mut self) -> &mut A
{
327 &mut **self.inner
.get_mut()
330 /// Converts the OccupiedEntry into a mutable reference to the value in the entry
331 /// with a lifetime bound to the collection itself.
332 pub fn into_mut(self) -> &'a
mut A
{
333 &mut **self.inner
.into_mut()
336 /// Sets the value of the entry, and returns the entry's old value.
338 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
339 /// the type ID of `value`. If they do not, memory safety may be violated.
340 pub unsafe fn insert(&mut self, value
: Box
<A
>) -> Box
<A
> {
341 self.inner
.insert(value
)
344 /// Takes the value out of the entry, and returns it.
345 pub fn remove(self) -> Box
<A
> {
350 impl<'a
, A
: ?Sized
+ UncheckedAnyExt
> VacantEntry
<'a
, A
> {
351 /// Sets the value of the entry with the VacantEntry's key,
352 /// and returns a mutable reference to it
354 /// It is the caller’s responsibility to ensure that the key of the entry corresponds with
355 /// the type ID of `value`. If they do not, memory safety may be violated.
356 pub unsafe fn insert(self, value
: Box
<A
>) -> &'a
mut A
{
357 &mut **self.inner
.insert(value
)