ctxmap

Crates.ioctxmap
lib.rsctxmap
version0.5.0
sourcesrc
created_at2021-09-20 15:36:18.660082
updated_at2022-06-16 06:32:34.932339
descriptionA collection that can store references of different types and lifetimes.
homepage
repositoryhttps://github.com/frozenlib/ctxmap
max_upload_size
id454058
size19,644
frozenlib (frozenlib)

documentation

https://docs.rs/ctxmap/

README

ctxmap

Crates.io Docs.rs Actions Status

A collection that can store references of different types and lifetimes.

Install

Add this to your Cargo.toml:

[dependencies]
ctxmap = "0.5.0"

Example

ctxmap::schema!(Schema);
ctxmap::key!(Schema {
    KEY_NO_DEFAULT: u32,
    KEY_INT: u32 = 10,
    KEY_DYN: dyn std::fmt::Display = 10,
    KEY_STR: str = "abc",
    KEY_STRING: str = format!("abc-{}", 10),
    mut KEY_MUT: u32 = 30,
});

let mut m = ctxmap::CtxMap::new();
assert_eq!(m.get(&KEY_NO_DEFAULT), None);
assert_eq!(m.get(&KEY_INT), Some(&10));
assert_eq!(m[&KEY_INT], 10);
assert_eq!(&m[&KEY_STR], "abc");

m.with(&KEY_INT, &20, |m| {
    assert_eq!(m[&KEY_INT], 20);
});
assert_eq!(m[&KEY_INT], 10);

assert_eq!(m[&KEY_MUT], 30);
m[&KEY_MUT] = 40;
assert_eq!(m[&KEY_MUT], 40);

m.with_mut(&KEY_MUT, &mut 50, |m| {
    assert_eq!(m[&KEY_MUT], 50);
    m[&KEY_MUT] = 60;
    assert_eq!(m[&KEY_MUT], 60);
});
assert_eq!(m[&KEY_MUT], 40);

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 83

cargo fmt