Add KeyCanExtend and MapCanExtend marker traits

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent 7938f6064d
commit 68102e4b8e
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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"

@ -70,7 +70,9 @@ macro_rules! impl_typemap {
}
}
impl<T: $crate::MapKey<Map = M>, M: 'static $(+ $trt )+> FromIterator<$crate::TypeMapEntry<T>> for $map {
impl<T: $crate::TypeMap $(+ $trt )+> $crate::MapCanExtend<$map> for T {}
impl<T: $crate::KeyCanExtend<$map>> FromIterator<$crate::TypeMapEntry<T>> for $map {
fn from_iter<T2: IntoIterator<Item = $crate::TypeMapEntry<T>>>(iter: T2) -> Self {
let map = iter
.into_iter()
@ -81,7 +83,7 @@ macro_rules! impl_typemap {
}
}
impl<K: $crate::MapKey<Map = M>, M: 'static $(+ $trt )+> Extend<$crate::TypeMapEntry<K>> for $map {
impl<K: $crate::KeyCanExtend<$map>> Extend<$crate::TypeMapEntry<K>> for $map {
fn extend<T: IntoIterator<Item = $crate::TypeMapEntry<K>>>(&mut self, iter: T) {
let iter = iter
.into_iter()

@ -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<Map = Self>;
/// 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<T: TypedKeyTrait<Self::Key>>(&self) -> bool;
}
/// Marker trait to signify that a given map can extend a different map (M)
/// or construct it
pub trait MapCanExtend<M> {}
/// Marker trait to signify that the given key associated with a map can extend
/// or construct a given map (M)
pub trait KeyCanExtend<M> {}
impl<T: MapKey<Map = M>, M: MapCanExtend<M2>, M2> KeyCanExtend<M2> for T {}

Loading…
Cancel
Save