#[derive(Observe)]
{
// Attributes available to this derive:
#[observe]
}
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 #[observe(...)] field attribute inside a
#[derive(Observe)] struct:
#[observe(hash)]— useHashObserverfor this field#[observe(noop)]— useNoopObserverfor this field#[observe(shallow)]— useShallowObserverfor this field#[observe(snapshot)]— useSnapshotObserverfor this field
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>
#[observe(noop)]
cache: String, // Not tracked
#[observe(shallow)]
metadata: Metadata, // ShallowObserver<Metadata>
}
#[derive(Serialize)]
struct Metadata {
created_at: String,
updated_at: String,
}