Expand description

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);

Macros

Define a key for CtxMap.

Define a type that implements Schema.

Structs

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

Mutable reference to CtxMap where the value has changed.

A key for CtxMap.

Traits

Key collection for CtxMap.

Type Definitions