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::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)]
pub(crate) struct TypeMapBase(pub(crate) HashMap<TypeId, MultitraitObject>);

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

@ -1,10 +1,14 @@
/// Macro to create a new trait bound typemap
#[doc(hidden)]
#[macro_export]
macro_rules! impl_typemap {
($map:ident, $key:ty) => {
($( #[$outer:meta] )*
$map:ident, $key:ident, $($trt:ident), +) => {
$( #[$outer] )*
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;
#[inline]
@ -13,37 +17,60 @@ macro_rules! impl_typemap {
}
#[inline]
fn insert<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>(
fn insert<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self,
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);
}
#[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())
}
#[inline]
fn get_mut<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>(
fn get_mut<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self,
) -> Option<&mut T::Value> {
self.0.get_mut::<T>().and_then(|v| v.downcast_mut())
}
#[inline]
fn remove<T: $crate::typemap_trait::TypedKeyTrait<Self::Key>>(
fn remove<T: $crate::TypedKeyTrait<Self::Key>>(
&mut self,
) -> Option<T::Value> {
self.0.remove::<T>().and_then(|v| v.downcast())
}
#[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>()
}
}
};
}
#[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::{impl_typemap, TypeMapKey, TypedKeyMto, TypedKeyTrait};
use multi_trait_object::{create_object, MultitraitObject, RawClone, TryClone};
use crate::impl_typemap;
use multi_trait_object::{RawClone, TryClone};
pub struct CloneTypeMapKey;
impl<T> TypedKeyTrait<CloneTypeMapKey> for T
where
T: TypeMapKey,
<T as TypeMapKey>::Value: TypedKeyMto<CloneTypeMapKey> + Clone,
{
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_typemap!(
/// A typemap that can be cloned restricting all inner
/// types to implement [std::clone::Clone] as well.
CloneTypeMap,
CloneTypeMapKey,
RawClone
);
impl Clone for CloneTypeMap {
fn clone(&self) -> Self {

@ -1,22 +1,9 @@
use crate::{impl_typemap, TypeMapKey, TypedKeyMto, TypedKeyTrait};
use multi_trait_object::{create_object, MultitraitObject};
use crate::impl_typemap;
use std::any::Any;
#[derive(Eq, PartialEq, Hash)]
pub struct AnyTypeMapKey;
impl<T> TypedKeyTrait<AnyTypeMapKey> for T
where
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);
impl_typemap!(
/// A typemap that can store any type (implementing [std::any::Any]).
TypeMap,
AnyTypeMapKey,
Any
);

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

Loading…
Cancel
Save