[][src]Macro dyn_context::context

macro_rules! context {
    (
        $(#[$attr:meta])*
        $vis:vis struct $name:ident $($body:tt)*
    ) => { ... };
    (
        $(#[$attr:meta])*
        $vis:vis dyn struct $name:ident $($body:tt)*
    ) => { ... };
}

Creates structure, allowing to pack several references into a one reference to a 'static type, optionally implementing the Context trait.

In Rust, lifetimes are intrusive, and sometimes it can lead to an inadequately complex code. Moreover, in some cases it can lead to an impossible code, means code so complex, so it can not make to compiles, even it is logically meaningful. (Such situations could occur because Rust does not support existential types with infinite parameters list.)

The context macro allows to "compress" several lifetimes into a one.

For example, using context you can pack together two str references and use them with a code, requiring a 'static type:

context! {
    struct DoubleStr {
        str_1: ref str,
        str_2: ref str
    }
}

fn call_back<T: 'static, R>(t: &T, callback: impl FnOnce(&T) -> R) -> R {
    callback(t)
}

let s_1 = String::from("str1");
let s_2 = String::from("str2");
let r = DoubleStr::call(&s_1[1..], &s_2[2..], |double_str| call_back(double_str, |double_str| {
    format!("{}{}", double_str.str_1(), double_str.str_2())
}));
assert_eq!(r, "tr1r2");

The context macro also allows automatically implement the Context trait.