Crates.io | trait-bound-typemap |
lib.rs | trait-bound-typemap |
version | 0.3.3 |
source | src |
created_at | 2022-03-14 20:36:12.266371 |
updated_at | 2022-03-16 20:10:00.984937 |
description | A crate to create typemaps with additional trait restrictions and implementations |
homepage | |
repository | https://github.com/Trivernis/trait-bound-typemap |
max_upload_size | |
id | 550162 |
size | 28,213 |
This crate offers typemaps that restrict a given type in their
trait and therefore offer additional trait implementations such as Clone
and PartialEq
.
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.
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>());
}
Apache-2.0