Skip to main content

Recall

Trait Recall 

Source
pub trait Recall: Recallable {
    // Required method
    fn recall(&mut self, memento: Self::Memento);
}
Expand description

A type that can change state by absorbing one companion memento value.

The meaning of “apply this memento” is type-defined: a Recall implementation may replace the whole value, merge fields, or selectively update nested state.

§Example

use recallable::{Recall, Recallable};

struct Settings {
    volume: u32,
    brightness: u32,
}

#[derive(Clone, Debug, PartialEq)]
struct SettingsMemento {
    volume: u32,
    brightness: u32,
}

impl Recallable for Settings {
    type Memento = SettingsMemento;
}

impl Recall for Settings {
    fn recall(&mut self, memento: Self::Memento) {
        self.volume = memento.volume;
        self.brightness = memento.brightness;
    }
}

fn main() {
   let mut settings = Settings { volume: 50, brightness: 70 };
   let memento = SettingsMemento { volume: 80, brightness: 40 };
   settings.recall(memento);
   assert_eq!(settings.volume, 80);
   assert_eq!(settings.brightness, 40);
}

Required Methods§

Source

fn recall(&mut self, memento: Self::Memento)

Applies the given memento to update the structure.

Implementors§