observe

Macro observe 

Source
observe!() { /* proc-macro */ }
Available on crate feature derive only.
Expand description

Observe and collect mutations within a closure.

This macro wraps a closure’s operations to track all mutations that occur within it. The closure receives a mutable reference to the value, and any mutations made are automatically collected and returned.

§Syntax

observe!(binding => { /* mutations */ }).unwrap();
observe!(|binding: &mut String| { /* mutations */ });

§Example

use serde::Serialize;
use morphix::adapter::Json;
use morphix::{Observe, observe};

#[derive(Serialize, Observe)]
struct Point {
    x: f64,
    y: f64,
}

let mut point = Point { x: 1.0, y: 2.0 };

let Json(mutation) = observe!(point => {
    point.x += 1.0;
    point.y *= 2.0;
}).unwrap();

assert_eq!(point.x, 2.0);
assert_eq!(point.y, 4.0);