anymap2/
raw.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! The raw form of a `Map`, allowing untyped access.
//!
//! All relevant details are in the `RawMap` struct.

#[cfg(test)]
use std::mem;
use std::{
    any::TypeId,
    borrow::Borrow,
    collections::hash_map::{self, HashMap},
    hash::{BuildHasherDefault, Hash, Hasher},
    ops::{Index, IndexMut},
};

use crate::any::{Any, UncheckedAnyExt};

#[derive(Default)]
struct TypeIdHasher {
    value: u64,
}

impl Hasher for TypeIdHasher {
    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        // This expects to receive one and exactly one 64-bit value
        assert!(bytes.len() == 8);
        self.value = u64::from(bytes[0])
            | u64::from(bytes[1]) << 8
            | u64::from(bytes[2]) << 16
            | u64::from(bytes[3]) << 24
            | u64::from(bytes[4]) << 32
            | u64::from(bytes[5]) << 40
            | u64::from(bytes[6]) << 48
            | u64::from(bytes[7]) << 56;
    }

    #[inline]
    fn finish(&self) -> u64 {
        self.value
    }
}

#[test]
fn type_id_hasher() {
    fn verify_hashing_with(type_id: TypeId) {
        let mut hasher = TypeIdHasher::default();
        type_id.hash(&mut hasher);
        assert_eq!(hasher.finish(), unsafe {
            mem::transmute::<TypeId, u64>(type_id)
        });
    }
    // Pick a variety of types, just to demonstrate it’s all sane. Normal,
    // zero-sized, unsized, &c.
    verify_hashing_with(TypeId::of::<usize>());
    verify_hashing_with(TypeId::of::<()>());
    verify_hashing_with(TypeId::of::<str>());
    verify_hashing_with(TypeId::of::<&str>());
    verify_hashing_with(TypeId::of::<Vec<u8>>());
}

/// The raw, underlying form of a `Map`.
///
/// At its essence, this is a wrapper around `HashMap<TypeId, Box<Any>>`, with
/// the portions that would be memory-unsafe removed or marked unsafe. Normal
/// people are expected to use the safe `Map` interface instead, but there is
/// the occasional use for this such as iteration over the contents of an `Map`.
/// However, because you will then be dealing with `Any` trait objects, it
/// doesn’t tend to be so very useful. Still, if you need it, it’s here.
#[derive(Debug)]
pub struct RawMap<A: ?Sized + UncheckedAnyExt = dyn Any> {
    inner: HashMap<TypeId, Box<A>, BuildHasherDefault<TypeIdHasher>>,
}

// #[derive(Clone)] would want A to implement Clone, but in reality it’s only
// Box<A> that can.
impl<A: ?Sized + UncheckedAnyExt> Clone for RawMap<A>
where
    Box<A>: Clone,
{
    #[inline]
    fn clone(&self) -> RawMap<A> {
        RawMap {
            inner: self.inner.clone(),
        }
    }
}

impl_common_methods! {
    field: RawMap.inner;
    new() => HashMap::with_hasher(Default::default());
    with_capacity(capacity) => HashMap::with_capacity_and_hasher(capacity, Default::default());
}

/// `RawMap` iterator.
#[derive(Clone)]
pub struct Iter<'a, A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::Iter<'a, TypeId, Box<A>>,
}
impl<'a, A: ?Sized + UncheckedAnyExt> Iterator for Iter<'a, A> {
    type Item = &'a A;

    #[inline]
    fn next(&mut self) -> Option<&'a A> {
        self.inner.next().map(|x| &**x.1)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}
impl<'a, A: ?Sized + UncheckedAnyExt> ExactSizeIterator for Iter<'a, A> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}

/// `RawMap` mutable iterator.
pub struct IterMut<'a, A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::IterMut<'a, TypeId, Box<A>>,
}
impl<'a, A: ?Sized + UncheckedAnyExt> Iterator for IterMut<'a, A> {
    type Item = &'a mut A;

    #[inline]
    fn next(&mut self) -> Option<&'a mut A> {
        self.inner.next().map(|x| &mut **x.1)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}
impl<'a, A: ?Sized + UncheckedAnyExt> ExactSizeIterator for IterMut<'a, A> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}

/// `RawMap` move iterator.
pub struct IntoIter<A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::IntoIter<TypeId, Box<A>>,
}
impl<A: ?Sized + UncheckedAnyExt> Iterator for IntoIter<A> {
    type Item = Box<A>;

    #[inline]
    fn next(&mut self) -> Option<Box<A>> {
        self.inner.next().map(|x| x.1)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}
impl<A: ?Sized + UncheckedAnyExt> ExactSizeIterator for IntoIter<A> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}

/// `RawMap` drain iterator.
pub struct Drain<'a, A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::Drain<'a, TypeId, Box<A>>,
}
impl<'a, A: ?Sized + UncheckedAnyExt> Iterator for Drain<'a, A> {
    type Item = Box<A>;

    #[inline]
    fn next(&mut self) -> Option<Box<A>> {
        self.inner.next().map(|x| x.1)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}
impl<'a, A: ?Sized + UncheckedAnyExt> ExactSizeIterator for Drain<'a, A> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
    /// An iterator visiting all entries in arbitrary order.
    ///
    /// Iterator element type is `&Any`.
    #[inline]
    pub fn iter(&self) -> Iter<A> {
        Iter {
            inner: self.inner.iter(),
        }
    }

    /// An iterator visiting all entries in arbitrary order.
    ///
    /// Iterator element type is `&mut Any`.
    #[inline]
    pub fn iter_mut(&mut self) -> IterMut<A> {
        IterMut {
            inner: self.inner.iter_mut(),
        }
    }

    /// Clears the map, returning all items as an iterator.
    ///
    /// Iterator element type is `Box<Any>`.
    ///
    /// Keeps the allocated memory for reuse.
    #[inline]
    pub fn drain(&mut self) -> Drain<A> {
        Drain {
            inner: self.inner.drain(),
        }
    }

    /// Gets the entry for the given type in the collection for in-place
    /// manipulation.
    #[inline]
    pub fn entry(&mut self, key: TypeId) -> Entry<A> {
        match self.inner.entry(key) {
            hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry { inner: e }),
            hash_map::Entry::Vacant(e) => Entry::Vacant(VacantEntry { inner: e }),
        }
    }

    /// Returns a reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the map's key type, but `Hash` and
    /// `Eq` on the borrowed form *must* match those for the key type.
    #[inline]
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&A>
    where
        TypeId: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.inner.get(k).map(|x| &**x)
    }

    /// Returns true if the map contains a value for the specified key.
    ///
    /// The key may be any borrowed form of the map's key type, but `Hash` and
    /// `Eq` on the borrowed form *must* match those for the key type.
    #[inline]
    pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
    where
        TypeId: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.inner.contains_key(k)
    }

    /// Returns a mutable reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the map's key type, but `Hash` and
    /// `Eq` on the borrowed form *must* match those for the key type.
    #[inline]
    pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut A>
    where
        TypeId: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.inner.get_mut(k).map(|x| &mut **x)
    }

    /// Inserts a key-value pair from the map. If the key already had a value
    /// present in the map, that value is returned. Otherwise, None is
    /// returned.
    ///
    /// # Safety
    ///
    /// It is the caller’s responsibility to ensure that the key corresponds
    /// with the type ID of the value. If they do not, memory safety may be
    /// violated.
    #[inline]
    pub unsafe fn insert(&mut self, key: TypeId, value: Box<A>) -> Option<Box<A>> {
        self.inner.insert(key, value)
    }

    /// Removes a key from the map, returning the value at the key if the key
    /// was previously in the map.
    ///
    /// The key may be any borrowed form of the map's key type, but `Hash` and
    /// `Eq` on the borrowed form *must* match those for the key type.
    #[inline]
    pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<Box<A>>
    where
        TypeId: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.inner.remove(k)
    }
}

impl<A: ?Sized + UncheckedAnyExt, Q> Index<Q> for RawMap<A>
where
    TypeId: Borrow<Q>,
    Q: Eq + Hash,
{
    type Output = A;

    #[inline]
    fn index(&self, index: Q) -> &A {
        self.get(&index).expect("no entry found for key")
    }
}

impl<A: ?Sized + UncheckedAnyExt, Q> IndexMut<Q> for RawMap<A>
where
    TypeId: Borrow<Q>,
    Q: Eq + Hash,
{
    #[inline]
    fn index_mut(&mut self, index: Q) -> &mut A {
        self.get_mut(&index).expect("no entry found for key")
    }
}

impl<A: ?Sized + UncheckedAnyExt> IntoIterator for RawMap<A> {
    type IntoIter = IntoIter<A>;
    type Item = Box<A>;

    #[inline]
    fn into_iter(self) -> IntoIter<A> {
        IntoIter {
            inner: self.inner.into_iter(),
        }
    }
}

/// A view into a single occupied location in a `RawMap`.
pub struct OccupiedEntry<'a, A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>>,
}

/// A view into a single empty location in a `RawMap`.
pub struct VacantEntry<'a, A: ?Sized + UncheckedAnyExt> {
    inner: hash_map::VacantEntry<'a, TypeId, Box<A>>,
}

/// A view into a single location in a `RawMap`, which may be vacant or
/// occupied.
pub enum Entry<'a, A: ?Sized + UncheckedAnyExt> {
    /// An occupied Entry
    Occupied(OccupiedEntry<'a, A>),
    /// A vacant Entry
    Vacant(VacantEntry<'a, A>),
}

impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> {
    /// Ensures a value is in the entry by inserting the default if empty, and
    /// returns a mutable reference to the value in the entry.
    ///
    /// # Safety
    ///
    /// It is the caller’s responsibility to ensure that the key of the entry
    /// corresponds with the type ID of `value`. If they do not, memory
    /// safety may be violated.
    #[inline]
    pub unsafe fn or_insert(self, default: Box<A>) -> &'a mut A {
        match self {
            Entry::Occupied(inner) => inner.into_mut(),
            Entry::Vacant(inner) => inner.insert(default),
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default
    /// function if empty, and returns a mutable reference to the value in
    /// the entry.
    ///
    /// # Safety
    ///
    /// It is the caller’s responsibility to ensure that the key of the entry
    /// corresponds with the type ID of `value`. If they do not, memory
    /// safety may be violated.
    #[inline]
    pub unsafe fn or_insert_with<F: FnOnce() -> Box<A>>(self, default: F) -> &'a mut A {
        match self {
            Entry::Occupied(inner) => inner.into_mut(),
            Entry::Vacant(inner) => inner.insert(default()),
        }
    }
}

impl<'a, A: ?Sized + UncheckedAnyExt> OccupiedEntry<'a, A> {
    /// Gets a reference to the value in the entry.
    #[inline]
    pub fn get(&self) -> &A {
        &**self.inner.get()
    }

    /// Gets a mutable reference to the value in the entry.
    #[inline]
    pub fn get_mut(&mut self) -> &mut A {
        &mut **self.inner.get_mut()
    }

    /// Converts the OccupiedEntry into a mutable reference to the value in the
    /// entry with a lifetime bound to the collection itself.
    #[inline]
    pub fn into_mut(self) -> &'a mut A {
        &mut **self.inner.into_mut()
    }

    /// Sets the value of the entry, and returns the entry's old value.
    ///
    /// # Safety
    ///
    /// It is the caller’s responsibility to ensure that the key of the entry
    /// corresponds with the type ID of `value`. If they do not, memory
    /// safety may be violated.
    #[inline]
    pub unsafe fn insert(&mut self, value: Box<A>) -> Box<A> {
        self.inner.insert(value)
    }

    /// Takes the value out of the entry, and returns it.
    #[inline]
    pub fn remove(self) -> Box<A> {
        self.inner.remove()
    }
}

impl<'a, A: ?Sized + UncheckedAnyExt> VacantEntry<'a, A> {
    /// Sets the value of the entry with the VacantEntry's key,
    /// and returns a mutable reference to it
    ///
    /// # Safety
    ///
    /// It is the caller’s responsibility to ensure that the key of the entry
    /// corresponds with the type ID of `value`. If they do not, memory
    /// safety may be violated.
    #[inline]
    pub unsafe fn insert(self, value: Box<A>) -> &'a mut A {
        &mut **self.inner.insert(value)
    }
}