#lifetime #reference #map #scope

ctxmap

A collection that can store references of different types and lifetimes

3 releases (breaking)

0.5.0 Jun 16, 2022
0.4.0 May 31, 2022
0.3.0 May 27, 2022
0.2.0 Apr 4, 2022
0.1.0 Sep 20, 2021

#1270 in Data structures

MIT/Apache

20KB
371 lines

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.

Dependencies

~49KB