Crate trait_bound_typemap

Source
Expand description

§Trait bound Typemap

This crate offers typemaps that restrict a given type in their trait and therefore offer additional trait implementations such as Clone and PartialEq.

§Safety

This crate relies on the multi-trait-object crate which provides a workaround for storing a type erased object with all associated traits until this feature is implemented in the language itself. This crate will likely break when the fat pointer used by trait objects changes which it hasn’t in a long time so far.

§Usage

use trait_bound_typemap::{CloneTypeMap, AnyTypeMap, TypeMap, TypeMapKey};

#[derive(Clone)]
pub struct MyStruct {
    a: u8,
    b: String,
}

pub struct MyStructKey;

impl TypeMapKey for MyStructKey {
    type Value = MyStruct;
}

fn main() {
    let mut map = CloneTypeMap::new();
    let value = MyStruct {a: 5, b: String::from("Hello World")};
    map.insert::<MyStructKey>(value);
    assert!(map.contains_key::<MyStructKey>());

    // can be cloned
    let map2 = map.clone();
    assert!(map.contains_key::<MyStructKey>());

    // less restrictive is always allowed
    let any_map = AnyTypeMap::from_iter(map2);
    assert!(map.contains_key::<MyStructKey>());
}

§License

Apache-2.0

Structs§

AnyTypeMap
A typemap that can store any type (implementing std::any::Any).
CloneSendSyncTypeMap
A typemap that implements Clone and is Send + Sync
CloneTypeMap
A typemap that can be cloned restricting all inner types to implement std::clone::Clone as well.
PartialEqTypeMap
A typemap that provides a PartialEq implementation
SendSyncTypeMap
A typemap that is Send and Sync
TypeMapEntry

Traits§

KeyCanExtend
Marker trait to signify that the given key associated with a map can extend or construct a given map (M)
MapCanExtend
Marker trait to signify that a given map can extend a different map (M) or construct it
TypeMap
A trait implemented by all typemaps that provides all basic typemap functions
TypeMapKey
A trait that allows using the object implementing it to be used as a type key.