Skip to main content

HeadSnapshot

Trait HeadSnapshot 

Source
pub trait HeadSnapshot: EsEntity {
    // Required method
    fn capture(&self) -> Option<Self::Snapshot>;
}
Expand description

Implemented by every entity whose repo enables snapshot.

The #[derive(EsRepo)] macro checks that the entity’s own Snapshot associated type and the repo’s snapshot flag agree, so widening the repo without also widening the entity’s EntityEvents<E, S> (or the reverse) does not compile:

ⓘ
impl HeadSnapshot for Meter {
    fn capture(&self) -> Option<MeterSnapshot> {
        (self.events.tail_len() >= 4).then(|| MeterSnapshot { .. })
    }
}
ⓘ
use es_entity::*;
use serde::{Serialize, Deserialize};
// Missing: a real `Snapshot` — `events` stays
// `EntityEvents<SnapGuardMeterEvent>` (implicit `NoSnapshot`).
#[derive(EsEntity)]
pub struct SnapGuardMeter {
    pub id: SnapGuardMeterId,
    events: EntityEvents<SnapGuardMeterEvent>,
}
// error: entity snapshot type and `#[es_repo(snapshot)]` disagree.
#[derive(EsRepo, Debug)]
#[es_repo(
    entity = "SnapGuardMeter",
    tbl = "meters",
    events_tbl = "meter_events",
    snapshot,
    snapshot_tbl = "meter_snapshots"
)]
pub struct SnapGuardMeters {
    pool: es_entity::db::Pool,
}

A snapshot type with a Forgettable<T> field also requires the repo to enable forgettable — otherwise the payload would never be scrubbed:

ⓘ
use es_entity::*;
use serde::{Serialize, Deserialize};
#[derive(EsSnapshot, Debug, Clone, Serialize, Deserialize)]
#[es_snapshot(version = 1)]
pub struct SnapGuardClientSnapshot {
    pub id: SnapGuardClientId,
    pub email: Forgettable<String>,
}
// error: snapshot type has Forgettable fields but this repo does not
// enable `forgettable`.
#[derive(EsRepo, Debug)]
#[es_repo(
    entity = "SnapGuardClient",
    tbl = "clients",
    events_tbl = "client_events",
    snapshot,
    snapshot_tbl = "client_snapshots"
)]
pub struct SnapGuardClients {
    pool: es_entity::db::Pool,
}

Required Methods§

Source

fn capture(&self) -> Option<Self::Snapshot>

Called on every write the entity passes through — with staged events, or with none when the loaded state had no matching snapshot. Some(s) = persist s as the snapshot at the current head; None = keep whatever snapshot exists and let the tail grow.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§