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
//! # 🦀 `StaticTypeMap`
//!
//! _A map where the key is the type of the value._
//!
//! ---
//!
//! You may be looking for:
//!
//! - [Git repository](https://github.com/malobre/static_type_map)
//! - [Crates.io](https://crates.io/crates/static_type_map)
//!
//! ## Example
//!
//! ```
//! use static_type_map::TypeMap;
//!
//! let mut type_map = TypeMap::new();
//! type_map.insert(10u8);
//! type_map.insert(20u16);
//! type_map.insert(true);
//! type_map.insert("a");
//!
//! assert!(type_map.contains::<bool>());
//!
//! assert_eq!(type_map.get::<&str>(), Some(&"a"));
//!
//! if let Some(previous_value) = type_map.insert(50u8) {
//!     assert_eq!(previous_value, 10u8);
//! }
//!
//! type_map.remove::<u16>();
//!
//! assert_eq!(type_map.len(), 3);
//! ```
//!
//! ## Features
//!
//! | name        | default ? | description              |
//! | ----------- | --------- | ------------------------ |
//! | `send`      | yes       | Enables [`SendTypeMap`]  |
//! | `sync`      | yes       | Enables [`SyncTypeMap`]  |
//! | `hashbrown` | no        | Enables `no_std` support |

#![cfg_attr(feature = "hashbrown", no_std)]

#[cfg(feature = "hashbrown")]
extern crate alloc;
#[cfg(feature = "hashbrown")]
extern crate core;
#[cfg(feature = "hashbrown")]
use alloc::boxed::Box;
#[cfg(feature = "hashbrown")]
use hashbrown::HashMap;

#[cfg(not(feature = "hashbrown"))]
use std::collections::HashMap;

use core::any::{Any, TypeId};

macro_rules! impl_type_map {
    (
        $(#[$attr:meta])*
        $vis:vis struct $name:ident: Any $(+ $bounds:tt)*;
    ) => {
        $(#[$attr])*
        $vis struct $name(HashMap<TypeId, Box<dyn Any $(+ $bounds)*>>);

        #[allow(rustdoc::private_doc_tests)]
        impl $name {
            #[doc = concat!("Creates an empty [`", stringify!($name), "`].")]
            ///
            /// The map is initially created with a capacity of 0, so it will not allocate
            /// until it is first inserted into.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let type_map = ", stringify!($name), "::new();")]
            /// ```
            #[must_use]
            pub fn new() -> Self {
                Self { ..Self::default() }
            }

            #[doc = concat!("Creates an empty [`", stringify!($name), "`] with the specified capacity.")]
            ///
            /// The map will be able to hold at least `capacity` types without reallocating.
            /// If `capacity` is 0, the static type map will not allocate.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let type_map = ", stringify!($name), "::with_capacity(10);")]
            /// ```
            #[must_use]
            pub fn with_capacity(capacity: usize) -> Self {
                Self(HashMap::with_capacity(capacity))
            }

            /// Returns the number of types the map can hold without reallocating.
            ///
            /// This number is a lower bound; the map might be able to hold more, but it is
            /// guaranteed to be able to hold at least so many.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let type_map = ", stringify!($name), "::with_capacity(100);")]
            /// assert!(type_map.capacity() >= 100);
            /// ```
            #[must_use]
            pub fn capacity(&self) -> usize {
                self.0.capacity()
            }

            /// Returns `true` if the map contains no instances of any type.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let type_map = ", stringify!($name), "::new();")]
            /// assert!(type_map.is_empty());
            /// ```
            #[must_use]
            pub fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            /// Returns the number of types in the map.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// assert_eq!(type_map.len(), 0);
            /// type_map.insert("a");
            /// assert_eq!(type_map.len(), 1);
            /// ```
            #[must_use]
            pub fn len(&self) -> usize {
                self.0.len()
            }

            /// Clears the map. Keep allocated memory for reuse.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// type_map.insert("a");
            /// type_map.clear();
            /// assert!(type_map.is_empty());
            /// ```
            pub fn clear(&mut self) {
                self.0.clear();
            }

            /// Reserves capacity for at least `additional` more types to be inserted in the map. The
            /// collection may reserve more space to avoid frequent reallocations.
            ///
            /// # Panics
            ///
            /// Panics if the new allocation size overflows [`usize`].
            ///
            /// # Examples
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// assert_eq!(type_map.capacity(), 0);
            /// type_map.reserve(10);
            /// assert!(type_map.capacity() >= 10);
            /// ```
            pub fn reserve(&mut self, additional: usize) {
                self.0.reserve(additional);
            }

            /// Shrinks the capacity of the map as much as possible. It will drop down as much as possible
            /// while mainting the internal rules and possibly leaving some space in accordance with the
            /// resize policy.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::with_capacity(100);")]
            /// assert!(type_map.capacity() >= 0);
            /// type_map.insert("a");
            /// type_map.insert(true);
            /// assert!(type_map.capacity() >= 2);
            /// ```
            pub fn shrink_to_fit(&mut self) {
                self.0.shrink_to_fit();
            }

            /// Returns `true` if the map contains an instance of `T`.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// type_map.insert("a");
            /// assert!(type_map.contains::<&str>());
            /// ```
            #[must_use]
            pub fn contains<T>(&self) -> bool
            where
                T: Any,
            {
                self.0.contains_key(&TypeId::of::<T>())
            }

            /// Returns a reference to an instance of `T`.
            ///
            /// If the map does not have an instance of `T`, [`None`] is returned.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// type_map.insert("a");
            /// assert_eq!(type_map.get::<&str>(), Some(&"a"));
            /// assert_eq!(type_map.get::<bool>(), None);
            /// ```
            #[must_use]
            pub fn get<T>(&self) -> Option<&T>
            where
                T: Any $(+ $bounds)*,
            {
                self.0
                    .get(&TypeId::of::<T>())
                    .map(|boxed_any: &Box<dyn Any $(+ $bounds)*>| {
                        let any_ref: &dyn Any = boxed_any.as_ref();

                        // Sanity check
                        debug_assert!(any_ref.is::<T>());

                        unsafe { &*(any_ref as *const dyn Any as *const T) }
                    })
            }

            /// Returns a mutable reference to an instance of `T`.
            ///
            /// If the map does not have an instance of `T`, [`None`] is returned.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// type_map.insert("a");
            /// if let Some(x) = type_map.get_mut::<&str>() {
            ///     *x = "b";
            /// }
            /// assert_eq!(type_map.get::<&str>(), Some(&"b"));
            /// ```
            #[must_use]
            pub fn get_mut<T>(&mut self) -> Option<&mut T>
            where
                T: Any $(+ $bounds)*,
            {
                self.0
                    .get_mut(&TypeId::of::<T>())
                    .map(|boxed_any: &mut Box<dyn Any $(+ $bounds)*>| {
                        let any_mut: &mut dyn Any = boxed_any.as_mut();

                        // Sanity check
                        debug_assert!(any_mut.is::<T>());

                        unsafe { &mut *(any_mut as *mut dyn Any as *mut T) }
                    })
            }

            /// Insert an instance of type `T` into the map.
            ///
            /// If the map did not have this type present, [`None`] is returned.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// assert_eq!(type_map.insert("a"), None);
            /// assert_eq!(type_map.insert("b"), Some("a"));
            /// ```
            pub fn insert<T>(&mut self, value: T) -> Option<T>
            where
                T: Any $(+ $bounds)*,
            {
                self.0
                    .insert(TypeId::of::<T>(), Box::new(value))
                    .map(|boxed_any: Box<dyn Any $(+ $bounds)*>| {
                        // Sanity check
                        debug_assert!((&*boxed_any).is::<T>());

                        let ptr: *mut dyn Any = Box::into_raw(boxed_any);

                        unsafe { *Box::from_raw(ptr as *mut T) }
                    })
            }

            /// Remove and return an instance of type `T` from the map.
            ///
            /// If the map did not have this type present, [`None`] is returned.
            ///
            /// # Examples
            ///
            /// ```
            #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
            ///
            #[doc = concat!("let mut type_map = ", stringify!($name), "::new();")]
            /// type_map.insert("a");
            /// assert_eq!(type_map.remove::<&str>(), Some("a"));
            /// ```
            pub fn remove<T>(&mut self) -> Option<T>
            where
                T: Any $(+ $bounds)*,
            {
                self.0
                    .remove(&TypeId::of::<T>())
                    .map(|boxed_any: Box<dyn Any $(+ $bounds)*>| {
                        // Sanity check
                        debug_assert!((&*boxed_any).is::<T>());

                        let ptr: *mut dyn Any = Box::into_raw(boxed_any);

                        unsafe { *Box::from_raw(ptr as *mut T) }
                    })
            }
        }
    }
}

impl_type_map! {
    /// A map where the key is the type of the value.
    #[derive(Debug, Default)]
    pub struct TypeMap: Any;
}

#[cfg(feature = "send")]
impl_type_map! {
    /// Like [`TypeMap`] but with a [`Send`] bound.
    #[derive(Debug, Default)]
    pub struct SendTypeMap: Any + Send;
}

#[cfg(feature = "sync")]
impl_type_map! {
    /// Like [`TypeMap`] but with a [`Send`] + [`Sync`] bound.
    #[derive(Debug, Default)]
    pub struct SyncTypeMap: Any + Send + Sync;
}