From 68102e4b8ea1cecf6f02fcdb029480a32845f510 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 16 Mar 2022 21:05:07 +0100 Subject: [PATCH] Add KeyCanExtend and MapCanExtend marker traits Signed-off-by: trivernis --- Cargo.toml | 2 +- src/macros.rs | 6 ++++-- src/typemap_trait.rs | 12 +++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a53e0be..f05dcea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trait-bound-typemap" -version = "0.3.1" +version = "0.3.2" edition = "2021" license = "Apache-2.0" readme = "README.md" diff --git a/src/macros.rs b/src/macros.rs index d97caa7..4037241 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -70,7 +70,9 @@ macro_rules! impl_typemap { } } - impl, M: 'static $(+ $trt )+> FromIterator<$crate::TypeMapEntry> for $map { + impl $crate::MapCanExtend<$map> for T {} + + impl> FromIterator<$crate::TypeMapEntry> for $map { fn from_iter>>(iter: T2) -> Self { let map = iter .into_iter() @@ -81,7 +83,7 @@ macro_rules! impl_typemap { } } - impl, M: 'static $(+ $trt )+> Extend<$crate::TypeMapEntry> for $map { + impl> Extend<$crate::TypeMapEntry> for $map { fn extend>>(&mut self, iter: T) { let iter = iter .into_iter() diff --git a/src/typemap_trait.rs b/src/typemap_trait.rs index 7023f32..5f33274 100644 --- a/src/typemap_trait.rs +++ b/src/typemap_trait.rs @@ -30,7 +30,7 @@ pub trait MapKey { /// A trait implemented by all typemaps that provides /// all basic typemap functions pub trait TypeMap { - type Key; + type Key: MapKey; /// Creates a new typemap fn new() -> Self; @@ -50,3 +50,13 @@ pub trait TypeMap { /// Returns if the map contains a given key fn contains_key>(&self) -> bool; } + +/// Marker trait to signify that a given map can extend a different map (M) +/// or construct it +pub trait MapCanExtend {} + +/// Marker trait to signify that the given key associated with a map can extend +/// or construct a given map (M) +pub trait KeyCanExtend {} + +impl, M: MapCanExtend, M2> KeyCanExtend for T {}