Add comments to impl_typemap macro

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 14e369449e
commit b7d7b83b0d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -2,6 +2,10 @@ use multi_trait_object::MultitraitObject;
use std::any::TypeId; use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
/// Base typemap used for implementation but not elsewhere
/// Each other typemap is just a newtype of this base type with
/// additional implementation.
#[doc(hidden)]
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(crate) struct TypeMapBase(pub(crate) HashMap<TypeId, MultitraitObject>); pub(crate) struct TypeMapBase(pub(crate) HashMap<TypeId, MultitraitObject>);

@ -1,7 +1,7 @@
#![doc=include_str!("../README.md")] #![doc=include_str!("../README.md")]
mod base; mod base;
pub(crate) mod macros; mod macros;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod trait_maps; mod trait_maps;

@ -1,10 +1,14 @@
/// Macro to create a new trait bound typemap
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[macro_export]
macro_rules! impl_typemap { macro_rules! impl_typemap {
($map:ident, $key:ty) => { ($( #[$outer:meta] )*
$map:ident, $key:ident, $($trt:ident), +) => {
$( #[$outer] )*
pub struct $map($crate::base::TypeMapBase); pub struct $map($crate::base::TypeMapBase);
$crate::impl_typekey!($key, $( $trt )+);
impl $crate::typemap_trait::TypeMapTrait for $map { impl $crate::TypeMapTrait for $map {
type Key = $key; type Key = $key;
#[inline] #[inline]
@ -13,37 +17,60 @@ macro_rules! impl_typemap {
} }
#[inline] #[inline]
fn insert<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>( fn insert<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self, &mut self,
value: T::Value, value: T::Value,
) { ) {
let mto = value.into_mto(); let mto = <T::Value as $crate::TypedKeyMto<Self::Key>>::into_mto(value);
self.0.insert::<T>(mto); self.0.insert::<T>(mto);
} }
#[inline] #[inline]
fn get<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>(&self) -> Option<&T::Value> { fn get<T: $crate::TypedKeyTrait<Self::Key>>(&self) -> Option<&T::Value> {
self.0.get::<T>().and_then(|v| v.downcast_ref()) self.0.get::<T>().and_then(|v| v.downcast_ref())
} }
#[inline] #[inline]
fn get_mut<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>( fn get_mut<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self, &mut self,
) -> Option<&mut T::Value> { ) -> Option<&mut T::Value> {
self.0.get_mut::<T>().and_then(|v| v.downcast_mut()) self.0.get_mut::<T>().and_then(|v| v.downcast_mut())
} }
#[inline] #[inline]
fn remove<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>( fn remove<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self, &mut self,
) -> Option<T::Value> { ) -> Option<T::Value> {
self.0.remove::<T>().and_then(|v| v.downcast()) self.0.remove::<T>().and_then(|v| v.downcast())
} }
#[inline] #[inline]
fn contains_key<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>(&self) -> bool { fn contains_key<T: $crate::TypedKeyTrait<Self::Key>>(&self) -> bool {
self.0.contains_key::<T>() self.0.contains_key::<T>()
} }
} }
}; };
} }
#[doc(hidden)]
#[macro_export]
macro_rules! impl_typekey {
($key:ident, $($trt:ident), +) => {
#[doc(hidden)]
pub struct $key;
impl<T> $crate::TypedKeyTrait<$key> for T
where
T: $crate::TypeMapKey,
<T as $crate::TypeMapKey>::Value: $crate::TypedKeyMto<$key> $(+ $trt )+,
{
type Value = T::Value;
}
impl<T: 'static $(+ $trt )+> $crate::TypedKeyMto<$key> for T {
fn into_mto(self) -> multi_trait_object::MultitraitObject {
multi_trait_object::create_object!(self $(, dyn $trt )+)
}
}
}
}

@ -1,24 +1,14 @@
use crate::base::TypeMapBase; use crate::base::TypeMapBase;
use crate::{impl_typemap, TypeMapKey, TypedKeyMto, TypedKeyTrait}; use crate::impl_typemap;
use multi_trait_object::{create_object, MultitraitObject, RawClone, TryClone}; use multi_trait_object::{RawClone, TryClone};
pub struct CloneTypeMapKey; impl_typemap!(
/// A typemap that can be cloned restricting all inner
impl<T> TypedKeyTrait<CloneTypeMapKey> for T /// types to implement [std::clone::Clone] as well.
where CloneTypeMap,
T: TypeMapKey, CloneTypeMapKey,
<T as TypeMapKey>::Value: TypedKeyMto<CloneTypeMapKey> + Clone, RawClone
{ );
type Value = T::Value;
}
impl<T: 'static + Clone> TypedKeyMto<CloneTypeMapKey> for T {
fn into_mto(self) -> MultitraitObject {
create_object!(self, dyn RawClone)
}
}
impl_typemap!(CloneTypeMap, CloneTypeMapKey);
impl Clone for CloneTypeMap { impl Clone for CloneTypeMap {
fn clone(&self) -> Self { fn clone(&self) -> Self {

@ -1,22 +1,9 @@
use crate::{impl_typemap, TypeMapKey, TypedKeyMto, TypedKeyTrait}; use crate::impl_typemap;
use multi_trait_object::{create_object, MultitraitObject};
use std::any::Any; use std::any::Any;
#[derive(Eq, PartialEq, Hash)] impl_typemap!(
pub struct AnyTypeMapKey; /// A typemap that can store any type (implementing [std::any::Any]).
TypeMap,
impl<T> TypedKeyTrait<AnyTypeMapKey> for T AnyTypeMapKey,
where Any
T: TypeMapKey, );
<T as TypeMapKey>::Value: Any,
{
type Value = T::Value;
}
impl<T: 'static> TypedKeyMto<AnyTypeMapKey> for T {
fn into_mto(self) -> MultitraitObject {
create_object!(self, dyn Any)
}
}
impl_typemap!(TypeMap, AnyTypeMapKey);

@ -9,12 +9,14 @@ pub trait TypeMapKey: 'static {
/// A trait used for restricting values inserted in a type map /// A trait used for restricting values inserted in a type map
/// using type checking /// using type checking
#[doc(hidden)]
pub trait TypedKeyTrait<T>: 'static { pub trait TypedKeyTrait<T>: 'static {
type Value: TypedKeyMto<T>; type Value: TypedKeyMto<T>;
} }
/// A trait used to create a multitrait-object from a given /// A trait used to create a multitrait-object from a given
/// value with the given guaranteed trait implementations /// value with the given guaranteed trait implementations
#[doc(hidden)]
pub trait TypedKeyMto<T> { pub trait TypedKeyMto<T> {
fn into_mto(self) -> MultitraitObject; fn into_mto(self) -> MultitraitObject;
} }

Loading…
Cancel
Save