trait_bound_typemap/typemap_trait.rs
1use multi_trait_object::MultitraitObject;
2use std::any::Any;
3
4/// A trait that allows using the object implementing it
5/// to be used as a type key.
6pub trait TypeMapKey: 'static {
7 type Value: Any;
8}
9
10/// A trait used for restricting values inserted in a type map
11/// using type checking
12#[doc(hidden)]
13pub trait TypedKeyTrait<T>: 'static {
14 type Value: TypedKeyMto<T>;
15}
16
17/// A trait used to create a multitrait-object from a given
18/// value with the given guaranteed trait implementations
19#[doc(hidden)]
20pub trait TypedKeyMto<T> {
21 fn into_mto(self) -> MultitraitObject;
22}
23
24/// A trait to map the key to the map it describes
25#[doc(hidden)]
26pub trait MapKey {
27 type Map: TypeMap<Key = Self>;
28}
29
30/// A trait implemented by all typemaps that provides
31/// all basic typemap functions
32pub trait TypeMap {
33 type Key: MapKey<Map = Self>;
34
35 /// Creates a new typemap
36 fn new() -> Self;
37
38 /// Inserts a value into the typemap with the given key
39 fn insert<T: TypedKeyTrait<Self::Key>>(&mut self, value: T::Value);
40
41 /// Returns a reference to a value from the type map with the given provided key
42 fn get<T: TypedKeyTrait<Self::Key>>(&self) -> Option<&T::Value>;
43
44 /// Returns a mutable reference to a value from the type map with the given provided key
45 fn get_mut<T: TypedKeyTrait<Self::Key>>(&mut self) -> Option<&mut T::Value>;
46
47 /// Removes a value from the map for the given key
48 fn remove<T: TypedKeyTrait<Self::Key>>(&mut self) -> Option<T::Value>;
49
50 /// Returns if the map contains a given key
51 fn contains_key<T: TypedKeyTrait<Self::Key>>(&self) -> bool;
52}
53
54/// Marker trait to signify that a given map can extend a different map (M)
55/// or construct it
56pub trait MapCanExtend<M> {}
57
58/// Marker trait to signify that the given key associated with a map can extend
59/// or construct a given map (M)
60pub trait KeyCanExtend<M> {}
61
62impl<T: MapKey<Map = M>, M: MapCanExtend<M2>, M2> KeyCanExtend<M2> for T {}