Skip to main content

recallable_model

Attribute Macro recallable_model 

Source
#[recallable_model]
Expand description

Attribute macro that prepares a struct for the Memento pattern.

Adds #[derive(Recallable, Recall)] automatically. When the serde feature is enabled, also derives serde::Serialize on the struct and injects #[serde(skip)] on fields marked with #[recallable(skip)].

Lifetime parameters are supported only when the generated memento can stay owned: non-skipped fields may not borrow one of the struct’s lifetimes. Skipped borrowed fields and lifetime-only markers such as PhantomData<&'a T> are allowed.

This example requires the serde feature.

use recallable::{Recall, Recallable, recallable_model};

#[recallable_model]
#[derive(Clone, Debug)]
struct Settings {
    volume: u8,
    brightness: u8,
    #[recallable(skip)]
    on_change: fn(),
}

fn noop() {}

let mut settings = Settings { volume: 50, brightness: 80, on_change: noop };
let memento: <Settings as Recallable>::Memento =
    serde_json::from_str(r#"{"volume":75,"brightness":60}"#).unwrap();
settings.recall(memento);
assert_eq!(settings.volume, 75);
assert_eq!(settings.brightness, 60);
// on_change is skipped — unchanged by recall

Attribute macro that augments a struct with Recallable/Recall derives.

  • Always adds #[derive(Recallable, Recall)].
  • When the serde feature is enabled for the macro crate, it also adds #[derive(serde::Serialize)].
  • For fields annotated with #[recallable(skip)], it injects #[serde(skip)] to keep serde output aligned with recall behavior.
  • This attribute itself takes no arguments.

This macro preserves the original struct shape and only mutates attributes.

Attribute ordering: This macro must appear before any attributes it needs to inspect. An attribute macro only receives attributes that follow it in source order. For example, #[derive(Serialize)] placed above #[recallable_model] is invisible to the macro and will cause a duplicate-derive error.