Observe

Derive Macro Observe 

Source
#[derive(Observe)]
{
    // Attributes available to this derive:
    #[morphix]
}
Available on crate feature derive only.
Expand description

Derive the Observe trait for structs to enable mutation tracking.

This macro automatically generates an Observe implementation for the struct, producing a default Observer type that wraps the struct and tracks mutations to each field according to that field’s own Observe implementation.

§Requirements

  • The struct must also derive or implement Serialize
  • Only named structs are supported (not tuple structs or enums)

§Customizing Behavior

If a field type T does not implement Observe, or you need an alternative observer implementation, you can customize this via the #[morphix(...)] field attribute inside a #[derive(Observe)] struct:

These attributes allow you to override the default Observer type that would otherwise come from the field’s Observe implementation.

§Example

use serde::Serialize;
use morphix::Observe;

#[derive(Serialize, Observe)]
struct User {
    name: String,         // StringObserver
    age: i32,             // SnapshotObserver<i32>

    #[morphix(noop)]
    cache: String,        // Not tracked

    #[morphix(shallow)]
    metadata: Metadata,   // ShallowObserver<Metadata>
}

#[derive(Serialize)]
struct Metadata {
    created_at: String,
    updated_at: String,
}