You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
3 years ago
|
use std::any::TypeId;
|
||
|
use std::collections::HashMap;
|
||
|
|
||
3 years ago
|
/// Base typemap used for implementation but not elsewhere
|
||
|
/// Each other typemap is just a newtype of this base type with
|
||
|
/// additional implementation.
|
||
|
#[doc(hidden)]
|
||
3 years ago
|
#[derive(Debug)]
|
||
|
pub(crate) struct TypeIndexedMap<T>(pub(crate) HashMap<TypeId, T>);
|
||
3 years ago
|
|
||
3 years ago
|
impl<T> Default for TypeIndexedMap<T> {
|
||
|
fn default() -> Self {
|
||
|
Self(HashMap::default())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T> TypeIndexedMap<T> {
|
||
3 years ago
|
#[inline]
|
||
|
pub fn new() -> Self {
|
||
|
Self::default()
|
||
|
}
|
||
|
|
||
|
#[inline]
|
||
3 years ago
|
pub fn insert<K: 'static>(&mut self, value: T) {
|
||
3 years ago
|
self.0.insert(TypeId::of::<K>(), value);
|
||
|
}
|
||
|
|
||
|
#[inline]
|
||
3 years ago
|
pub fn get<K: 'static>(&self) -> Option<&T> {
|
||
3 years ago
|
self.0.get(&TypeId::of::<K>())
|
||
|
}
|
||
|
|
||
|
#[inline]
|
||
3 years ago
|
pub fn get_mut<K: 'static>(&mut self) -> Option<&mut T> {
|
||
3 years ago
|
self.0.get_mut(&TypeId::of::<K>())
|
||
|
}
|
||
|
|
||
|
#[inline]
|
||
3 years ago
|
pub fn remove<K: 'static>(&mut self) -> Option<T> {
|
||
3 years ago
|
self.0.remove(&TypeId::of::<K>())
|
||
|
}
|
||
|
|
||
|
#[inline]
|
||
|
pub fn contains_key<K: 'static>(&self) -> bool {
|
||
|
self.0.contains_key(&TypeId::of::<K>())
|
||
|
}
|
||
|
}
|