key

Macro key 

Source
macro_rules! key {
    ($schema:ty { }) => { ... };
    ($schema:ty { $vis:vis $id:ident: $type:ty }) => { ... };
    ($schema:ty { $vis:vis mut $id:ident: $type:ty }) => { ... };
    ($schema:ty { $vis:vis $id:ident: $type:ty = $init:expr }) => { ... };
    ($schema:ty { $vis:vis mut $id:ident: $type:ty = $init:expr }) => { ... };
    ($schema:ty { $vis:vis $id:ident: $type:ty, $($tt:tt)* }) => { ... };
    ($schema:ty { $vis:vis mut $id:ident: $type:ty, $($tt:tt)* }) => { ... };
    ($schema:ty { $vis:vis $id:ident: $type:ty = $init:expr, $($tt:tt)* }) => { ... };
    ($schema:ty { $vis:vis mut $id:ident: $type:ty = $init:expr, $($tt:tt)* }) => { ... };
}
Expand description

Define a key for CtxMap.

ยงExample

ctxmap::schema!(S);
ctxmap::key!(S { KEY_1: u8 });
ctxmap::key!(S { KEY_2: str });

You can define multiple keys at once.

ctxmap::schema!(S);
ctxmap::key!(S {
    KEY_1: u8,
    KEY_2: str,
});

You can specify a default value.

The default value can be an expression that, when applied with & operator, becomes a reference to the type of the key.

For example, &"abc" and &String::new() can be &str, so "abc" and String::new() can be used as default values for keys of type str.

use std::fmt::Display;

ctxmap::schema!(S);
ctxmap::key!(S {
    KEY_1: u8 = 10,
    KEY_2: str = "abc",
    KEY_3: str = String::new(),
    KEY_4: dyn Display = 10,
    KEY_5: dyn Display = "xyz",
});

You can specify mutability.

Keys with mut can be used in with_mut, get_mut and index_mut.

ctxmap::schema!(S);
ctxmap::key!(S {
    mut KEY_1: u8,
    mut KEY_2: String,
});

You can specify visibility.

ctxmap::schema!(pub S);
ctxmap::key!(S { KEY_A: u8 });
ctxmap::key!(S { pub KEY_B: u8 });
ctxmap::key!(S { pub(crate) KEY_C: u8 });