[][src]Macro reax::computed

macro_rules! computed {
    { output! $n:ident = $e:expr; context! $c:ident $(: $ct:ty)?; $($x:tt)* } => { ... };
    { output! $n:ident = $e:expr; $($x:tt)* } => { ... };
    { context! $c:ident$(: $ct:ty)?; $($x:tt)* } => { ... };
    { $($x:tt)* } => { ... };
}

A macro for conveniently creating ComputedVars.

let a = Var::new(3);
let b = Var::new(4);

// Return the computed value.
let sum = computed! {
    a.get_copy() + b.get_copy()
};

// Pre-allocate the computed value and mutate it.
let debug = computed! {
    output! text = String::new();
    text.clear();

    write!(
        text,
        "{} + {} = {}",
        a.get_copy(),
        b.get_copy(),
        sum.get_copy(),
    ).unwrap();
};

// Use a context.
let with_ctx = computed! {
    context! ctx: Vec<i32>;

    ctx.push(sum.get_copy());
};

// And it works!
let mut list = Vec::new();
assert_eq!(debug.get().as_str(), "3 + 4 = 7");
with_ctx.get_contextual(&mut list);
assert_eq!(list, &[7]);

a.set(5);

assert_eq!(debug.get().as_str(), "5 + 4 = 9");
with_ctx.get_contextual(&mut list);
assert_eq!(list, &[7, 9]);