Skip to main content

escriba_core/
gen.rs

1//! [`EditGen`] — the monotonic edit-generation stamp.
2//!
3//! One per `EditorState`. It bumps on every state mutation (edit, motion,
4//! mode change, viewport move) and is the root of escriba's sealed refresh
5//! tree (`theory/ESCRIBA.md` §Refresh-Seal): a rendered product is *fresh* iff
6//! its stamp equals the current `EditGen`, so a stale frame — one that shows a
7//! product older than the state it claims — has no reachable code path once
8//! the renderer gates on the stamp. Same monotonic-`u64` technique as mado's
9//! frame seqno, promoted to a typed primitive.
10
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13
14/// A monotonic edit-generation counter. Equality is the freshness test:
15/// `product_gen == current_gen` ⇒ the product matches the live state.
16#[derive(
17    Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default, Serialize, Deserialize,
18    JsonSchema,
19)]
20pub struct EditGen(pub u64);
21
22impl EditGen {
23    /// The next generation. Wraps (a 64-bit counter at 1 bump/ns lasts ~585
24    /// years; wrap is defensive, never reached).
25    #[must_use]
26    pub fn next(self) -> Self {
27        Self(self.0.wrapping_add(1))
28    }
29}