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
use std::{
    any::{Any, TypeId},
    collections::hash_map::Entry as MapEntry,
    marker::PhantomData,
};

use rustc_hash::FxHashMapRand;

pub(crate) type AnyObject = Box<dyn Any + Send + Sync>;

pub struct Entry<'a, K: 'a, V: 'a> {
    inner: MapEntry<'a, K, AnyObject>,
    _marker: PhantomData<V>,
}

impl<'a, K, V> Entry<'a, K, V> {
    #[inline]
    pub fn or_insert(self, default: V) -> &'a mut V
    where
        V: Send + Sync + 'static,
    {
        let v = self.inner.or_insert_with(|| Box::new(default));
        v.downcast_mut().unwrap()
    }

    #[inline]
    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V
    where
        V: Send + Sync + 'static,
    {
        let v = self.inner.or_insert_with(|| Box::new(default()));
        v.downcast_mut().unwrap()
    }

    #[inline]
    pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V
    where
        V: Send + Sync + 'static,
    {
        let v = self.inner.or_insert_with_key(|key| Box::new(default(key)));
        v.downcast_mut().unwrap()
    }

    #[inline]
    pub fn and_modify<F: FnOnce(&mut V)>(self, f: F) -> Self
    where
        V: Send + Sync + 'static,
    {
        Entry {
            inner: self.inner.and_modify(|v| {
                f(v.downcast_mut().unwrap());
            }),
            _marker: PhantomData,
        }
    }

    #[allow(clippy::unwrap_or_default)]
    #[inline]
    pub fn or_default(self) -> &'a mut V
    where
        V: Default + Send + Sync + 'static,
    {
        self.or_insert_with(V::default)
    }
}

#[derive(Debug, Default)]
pub struct TypeMap {
    inner: FxHashMapRand<TypeId, AnyObject>,
}

impl TypeMap {
    #[inline]
    pub fn new() -> Self {
        TypeMap {
            inner: FxHashMapRand::default(),
        }
    }

    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        TypeMap {
            inner: FxHashMapRand::with_capacity_and_hasher(capacity, Default::default()),
        }
    }

    #[inline]
    pub fn insert<T: Send + Sync + 'static>(&mut self, t: T) {
        self.inner.insert(TypeId::of::<T>(), Box::new(t));
    }

    #[inline]
    pub fn get<T: 'static>(&self) -> Option<&T> {
        self.inner
            .get(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast_ref())
    }

    #[inline]
    pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.inner
            .get_mut(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast_mut())
    }

    #[inline]
    pub fn contains<T: 'static>(&self) -> bool {
        self.inner.contains_key(&TypeId::of::<T>())
    }

    #[inline]
    pub fn remove<T: 'static>(&mut self) -> Option<T> {
        self.inner
            .remove(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast().ok().map(|boxed| *boxed))
    }

    #[inline]
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    #[inline]
    pub fn extend(&mut self, other: TypeMap) {
        self.inner.extend(other.inner)
    }

    #[inline]
    pub fn iter(&self) -> ::std::collections::hash_map::Iter<'_, TypeId, AnyObject> {
        self.inner.iter()
    }

    #[inline]
    pub fn entry<T: 'static>(&mut self) -> Entry<'_, TypeId, T> {
        Entry {
            inner: self.inner.entry(TypeId::of::<T>()),
            _marker: PhantomData,
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    #[inline]
    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }
}