more inline is more inline

This commit is contained in:
Dominik Maier 2020-12-11 11:23:07 +01:00
parent 58cf357bf0
commit ca39bd97b4

View File

@ -5,12 +5,15 @@ use core::slice::{Iter, IterMut};
use hashbrown::hash_map::{Keys, Values, ValuesMut}; use hashbrown::hash_map::{Keys, Values, ValuesMut};
use hashbrown::HashMap; use hashbrown::HashMap;
/// A map, storing any trait object by TypeId
#[derive(Default)] #[derive(Default)]
pub struct MetaMap { pub struct MetaMap {
map: HashMap<TypeId, Box<dyn Any>>, map: HashMap<TypeId, Box<dyn Any>>,
} }
impl MetaMap { impl MetaMap {
#[inline]
pub fn get<T>(&self) -> Option<&T> pub fn get<T>(&self) -> Option<&T>
where where
T: Any, T: Any,
@ -20,6 +23,7 @@ impl MetaMap {
.map(|x| x.as_ref().downcast_ref::<T>().unwrap()) .map(|x| x.as_ref().downcast_ref::<T>().unwrap())
} }
#[inline]
pub fn get_mut<T>(&mut self) -> Option<&mut T> pub fn get_mut<T>(&mut self) -> Option<&mut T>
where where
T: Any, T: Any,
@ -29,6 +33,7 @@ impl MetaMap {
.map(|x| x.as_mut().downcast_mut::<T>().unwrap()) .map(|x| x.as_mut().downcast_mut::<T>().unwrap())
} }
#[inline]
pub fn insert<T>(&mut self, t: T) pub fn insert<T>(&mut self, t: T)
where where
T: Any, T: Any,
@ -36,10 +41,12 @@ impl MetaMap {
self.map.insert(TypeId::of::<T>(), Box::new(t)); self.map.insert(TypeId::of::<T>(), Box::new(t));
} }
#[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.map.len() self.map.len()
} }
#[inline]
pub fn contains<T>(&self) -> bool pub fn contains<T>(&self) -> bool
where where
T: Any, T: Any,
@ -54,11 +61,13 @@ impl MetaMap {
} }
} }
/// A map, allowing to store multiple elements of any given type, by TypeId.
pub struct MultiMetaMap { pub struct MultiMetaMap {
map: HashMap<TypeId, Vec<Box<dyn Any>>>, map: HashMap<TypeId, Vec<Box<dyn Any>>>,
} }
impl MultiMetaMap { impl MultiMetaMap {
#[inline]
pub fn get<T>(&self) -> Option<core::iter::Map<Iter<'_, Box<dyn Any>>, fn(&Box<dyn Any>) -> &T>> pub fn get<T>(&self) -> Option<core::iter::Map<Iter<'_, Box<dyn Any>>, fn(&Box<dyn Any>) -> &T>>
where where
T: Any, T: Any,
@ -69,6 +78,7 @@ impl MultiMetaMap {
} }
} }
#[inline]
pub fn get_mut<T>( pub fn get_mut<T>(
&mut self, &mut self,
) -> Option<core::iter::Map<IterMut<'_, Box<dyn Any>>, fn(&mut Box<dyn Any>) -> &mut T>> ) -> Option<core::iter::Map<IterMut<'_, Box<dyn Any>>, fn(&mut Box<dyn Any>) -> &mut T>>
@ -84,6 +94,7 @@ impl MultiMetaMap {
} }
} }
#[inline]
pub fn insert<T>(&mut self, t: T) pub fn insert<T>(&mut self, t: T)
where where
T: Any, T: Any,
@ -96,10 +107,12 @@ impl MultiMetaMap {
} }
} }
#[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.map.len() self.map.len()
} }
#[inline]
pub fn contains<T>(&self) -> bool pub fn contains<T>(&self) -> bool
where where
T: Any, T: Any,
@ -119,6 +132,8 @@ pub struct MetaInstanceMap {
} }
impl MetaInstanceMap { impl MetaInstanceMap {
#[inline]
pub fn get<T, U>(&self, instance: &U) -> Option<&T> pub fn get<T, U>(&self, instance: &U) -> Option<&T>
where where
T: Any, T: Any,
@ -138,6 +153,7 @@ impl MetaInstanceMap {
} }
} }
#[inline]
pub fn get_mut<T, U>(&mut self, instance: &U) -> Option<&mut T> pub fn get_mut<T, U>(&mut self, instance: &U) -> Option<&mut T>
where where
T: Any, T: Any,
@ -186,6 +202,7 @@ impl MetaInstanceMap {
} }
} }
#[inline]
pub fn insert<T, U>(&mut self, t: T, instance: &U) pub fn insert<T, U>(&mut self, t: T, instance: &U)
where where
T: Any, T: Any,
@ -207,10 +224,12 @@ impl MetaInstanceMap {
.insert(instance, Box::new(t)); .insert(instance, Box::new(t));
} }
#[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.map.len() self.map.len()
} }
#[inline]
pub fn contains_type<T>(&self) -> bool pub fn contains_type<T>(&self) -> bool
where where
T: Any, T: Any,
@ -218,6 +237,7 @@ impl MetaInstanceMap {
self.map.contains_key(&TypeId::of::<T>()) self.map.contains_key(&TypeId::of::<T>())
} }
#[inline]
pub fn contains<T, U>(&self, instance: &U) -> bool pub fn contains<T, U>(&self, instance: &U) -> bool
where where
T: Any, T: Any,
@ -247,6 +267,7 @@ pub trait AsAny {
fn as_any_mut(&mut self) -> &mut dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any;
} }
/// A map, allowing to store and get any object by type and name
pub struct NamedAnyMap<B> pub struct NamedAnyMap<B>
where where
B: ?Sized + Any + AsAny, B: ?Sized + Any + AsAny,
@ -344,6 +365,7 @@ where
} }
} }
#[inline]
pub fn all_typeids(&self) -> Keys<'_, TypeId, HashMap<&'static str, Box<B>>> { pub fn all_typeids(&self) -> Keys<'_, TypeId, HashMap<&'static str, Box<B>>> {
self.map.keys() self.map.keys()
} }
@ -356,10 +378,12 @@ where
self.map.get_mut(&typeid).unwrap().insert(name, val); self.map.get_mut(&typeid).unwrap().insert(name, val);
} }
#[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.map.len() self.map.len()
} }
#[inline]
pub fn contains_type<T>(&self) -> bool pub fn contains_type<T>(&self) -> bool
where where
T: Any, T: Any,
@ -367,6 +391,7 @@ where
self.map.contains_key(&TypeId::of::<T>()) self.map.contains_key(&TypeId::of::<T>())
} }
#[inline]
pub fn contains<T>(&self, name: &'static str) -> bool pub fn contains<T>(&self, name: &'static str) -> bool
where where
T: Any, T: Any,