Change typemap base to be generic

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent 638665af06
commit 51dd8a5c09
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,10 +1,10 @@
#![doc=include_str!("../README.md")]
mod base;
mod macros;
#[cfg(test)]
mod tests;
mod trait_maps;
mod type_indexed;
mod typemap_trait;
pub use trait_maps::*;

@ -6,7 +6,7 @@ macro_rules! impl_typemap {
$map:ident, $key:ident, $($trt:ident), +) => {
$( #[$outer] )*
#[derive(Debug)]
pub struct $map($crate::base::TypeMapBase);
pub struct $map($crate::type_indexed::TypeIndexedMap<multi_trait_object::MultitraitObject>);
$crate::impl_typekey!($key, $( $trt )+);
impl $crate::TypeMapTrait for $map {
@ -14,7 +14,7 @@ macro_rules! impl_typemap {
#[inline]
fn new() -> Self {
Self($crate::base::TypeMapBase::new())
Self($crate::type_indexed::TypeIndexedMap::new())
}
#[inline]

@ -1,5 +1,5 @@
use crate::base::TypeMapBase;
use crate::impl_typemap;
use crate::type_indexed::TypeIndexedMap;
use multi_trait_object::{RawClone, TryClone};
impl_typemap!(
@ -18,6 +18,6 @@ impl Clone for CloneTypeMap {
.iter()
.map(|(t, o)| (t.clone(), o.try_clone().unwrap()))
.collect();
CloneTypeMap(TypeMapBase(map))
CloneTypeMap(TypeIndexedMap(map))
}
}

@ -1,4 +1,3 @@
use multi_trait_object::MultitraitObject;
use std::any::TypeId;
use std::collections::HashMap;
@ -6,32 +5,38 @@ use std::collections::HashMap;
/// Each other typemap is just a newtype of this base type with
/// additional implementation.
#[doc(hidden)]
#[derive(Debug, Default)]
pub(crate) struct TypeMapBase(pub(crate) HashMap<TypeId, MultitraitObject>);
#[derive(Debug)]
pub(crate) struct TypeIndexedMap<T>(pub(crate) HashMap<TypeId, T>);
impl TypeMapBase {
impl<T> Default for TypeIndexedMap<T> {
fn default() -> Self {
Self(HashMap::default())
}
}
impl<T> TypeIndexedMap<T> {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn insert<K: 'static>(&mut self, value: MultitraitObject) {
pub fn insert<K: 'static>(&mut self, value: T) {
self.0.insert(TypeId::of::<K>(), value);
}
#[inline]
pub fn get<K: 'static>(&self) -> Option<&MultitraitObject> {
pub fn get<K: 'static>(&self) -> Option<&T> {
self.0.get(&TypeId::of::<K>())
}
#[inline]
pub fn get_mut<K: 'static>(&mut self) -> Option<&mut MultitraitObject> {
pub fn get_mut<K: 'static>(&mut self) -> Option<&mut T> {
self.0.get_mut(&TypeId::of::<K>())
}
#[inline]
pub fn remove<K: 'static>(&mut self) -> Option<MultitraitObject> {
pub fn remove<K: 'static>(&mut self) -> Option<T> {
self.0.remove(&TypeId::of::<K>())
}
Loading…
Cancel
Save