|
| 1 | +use std::any::{Any, TypeId}; |
| 2 | +use std::collections::hash_map as map; |
| 3 | +use std::marker::{PhantomData, Unsize}; |
| 4 | + |
| 5 | +use rustc_data_structures::fx::FxHashMap; |
| 6 | + |
| 7 | +/// Map that can store data for arbitrary types. |
| 8 | +pub struct AnyMap<U: ?Sized> { |
| 9 | + // This is basically `FxHashMap<TypeId, Box<dyn Any>>` |
| 10 | + // |
| 11 | + // The generic `U` is present to capture auto trait bounds. |
| 12 | + map: FxHashMap<TypeId, Box<U>>, |
| 13 | +} |
| 14 | + |
| 15 | +pub struct OccupiedEntry<'a, U: ?Sized, T> { |
| 16 | + entry: map::OccupiedEntry<'a, TypeId, Box<U>>, |
| 17 | + phantom: PhantomData<T>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a, U: Any + ?Sized + 'static, T: 'static> OccupiedEntry<'a, U, T> { |
| 21 | + pub fn into_mut(self) -> &'a mut T |
| 22 | + where |
| 23 | + T: Unsize<U>, |
| 24 | + { |
| 25 | + let any_ref = &mut **self.entry.into_mut(); |
| 26 | + debug_assert_eq!((*any_ref).type_id(), TypeId::of::<T>()); |
| 27 | + // SAFETY: by type invariant, `any_ref` is a `&mut T`. |
| 28 | + unsafe { &mut *(any_ref as *mut U as *mut T) } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub struct VacantEntry<'a, U: ?Sized, T> { |
| 33 | + entry: map::VacantEntry<'a, TypeId, Box<U>>, |
| 34 | + phantom: PhantomData<T>, |
| 35 | +} |
| 36 | + |
| 37 | +impl<'a, U: Any + ?Sized, T> VacantEntry<'a, U, T> { |
| 38 | + pub fn insert(self, value: T) -> &'a mut T |
| 39 | + where |
| 40 | + T: Unsize<U>, |
| 41 | + { |
| 42 | + let any_ref = &mut **self.entry.insert(Box::new(value) as _); |
| 43 | + // SAFETY: we just inserted it and we know the type is `Box<T>`. |
| 44 | + unsafe { &mut *(any_ref as *mut U as *mut T) } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +pub enum Entry<'a, U: ?Sized, T> { |
| 49 | + Occupied(OccupiedEntry<'a, U, T>), |
| 50 | + Vacant(VacantEntry<'a, U, T>), |
| 51 | +} |
| 52 | + |
| 53 | +impl<'a, U: Any + ?Sized, T: 'static> Entry<'a, U, T> { |
| 54 | + pub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T |
| 55 | + where |
| 56 | + T: Unsize<U>, |
| 57 | + { |
| 58 | + match self { |
| 59 | + Entry::Occupied(entry) => entry.into_mut(), |
| 60 | + Entry::Vacant(entry) => entry.insert(default()), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<U: ?Sized> Default for AnyMap<U> { |
| 66 | + fn default() -> Self { |
| 67 | + Self::new() |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<U: ?Sized> AnyMap<U> { |
| 72 | + pub fn new() -> Self { |
| 73 | + Self { |
| 74 | + map: Default::default(), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<U: Any + ?Sized> AnyMap<U> { |
| 80 | + pub fn entry<T: 'static>(&mut self) -> Entry<'_, U, T> { |
| 81 | + match self.map.entry(TypeId::of::<T>()) { |
| 82 | + map::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { |
| 83 | + entry, |
| 84 | + phantom: PhantomData, |
| 85 | + }), |
| 86 | + map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry { |
| 87 | + entry, |
| 88 | + phantom: PhantomData, |
| 89 | + }), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments