euv-core 0.47.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Generation-guarded token preventing stale async callbacks from mutating destroyed, evicted, or replaced pages.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
pub struct PageGenerationToken {
    /// Stable monotonic page identifier.
    #[get(type(copy))]
    pub(crate) page_id: u64,
    /// Monotonic instance generation bumped on every eviction reconstruction and destruction.
    #[get(type(copy))]
    pub(crate) generation: u64,
}

/// Versioned restorable page snapshot preserved across `Freeze` and `Evict` (discarded on explicit `Destroy`).
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct PageSnapshot {
    /// Snapshot schema version for forward-compatible restoration validation.
    #[get(type(copy))]
    pub(crate) schema_version: u32,
    /// Canonical route identifier.
    pub(crate) route_key: String,
    /// Generation of the page instance when this snapshot was captured.
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Last primary scroll offset in logical pixels.
    #[get(type(copy))]
    pub(crate) scroll_offset: Point,
    /// Optional focused element key restored on resume.
    pub(crate) focus_key: String,
    /// Whether `focus_key` contains an active focus target.
    #[get(type(copy))]
    pub(crate) has_focus_key: bool,
    /// Serialized form/editor draft payload preserved across cold eviction.
    pub(crate) draft_payload: String,
    /// True when the page holds user-edited unsaved draft data.
    #[get(type(copy))]
    pub(crate) has_unsaved_draft: bool,
    /// True once an unsaved draft has been durably persisted into `draft_payload` or external storage.
    #[get(type(copy))]
    pub(crate) draft_persisted: bool,
    /// Last host/shared-store revision synchronized with this page.
    #[get(type(copy))]
    pub(crate) host_state_revision: u64,
    /// Estimated byte footprint of this snapshot.
    #[get(type(copy))]
    pub(crate) estimated_bytes: usize,
}

/// Bounded cold-page retention policy enforced by `PageLifecycleController`.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, New, PartialEq)]
pub struct PageBudgetPolicy {
    /// Maximum count of cold `Frozen` pages kept in memory simultaneously.
    #[get(type(copy))]
    pub(crate) max_frozen_pages: usize,
    /// Maximum aggregate retained bytes across all `Frozen` pages.
    #[get(type(copy))]
    pub(crate) max_retained_bytes: usize,
    /// Maximum byte size allowed for one retained `PageSnapshot`.
    #[get(type(copy))]
    pub(crate) max_snapshot_bytes: usize,
}

/// Complete deterministic lifecycle record for one routed page or nested sub-page.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct PageLifecycleEntry {
    /// Unique monotonic page identifier.
    #[get(type(copy))]
    pub(crate) page_id: u64,
    /// Canonical route identifier.
    pub(crate) route_key: String,
    /// Current live generation incremented on eviction reconstruction or destruction.
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Parent page identifier when `has_parent` is `true` for parent-child lifecycle propagation.
    #[get(type(copy))]
    pub(crate) parent_page_id: u64,
    /// Whether this page belongs to a parent page container.
    #[get(type(copy))]
    pub(crate) has_parent: bool,
    /// Active lifecycle phase (`Active`, `Frozen`, `Evicted`, `Destroyed`, etc.).
    #[get(type(copy))]
    pub(crate) phase: LifecyclePhase,
    /// Retention strategy (`Freeze`, `Destroy`, `Alive`).
    #[get(type(copy))]
    pub(crate) retention: PageRetention,
    /// Core retained state/scene memory footprint in bytes while `Active` or `Frozen`.
    #[get(type(copy))]
    pub(crate) retained_bytes: usize,
    /// Reconstructible cache footprint (decoded images, glyphs, pre-baked tiles) evicted BEFORE evicting the page.
    #[get(type(copy))]
    pub(crate) reconstructible_cache_bytes: usize,
    /// Monotonic logical tick when this page was last active (governs LRU cold eviction).
    #[get(type(copy))]
    pub(crate) last_active_tick: u64,
    /// Cumulative number of idempotent `Freeze` transitions executed on this page.
    #[get(type(copy))]
    pub(crate) freeze_count: u64,
    /// Cumulative number of idempotent `Resume` transitions executed on this page.
    #[get(type(copy))]
    pub(crate) resume_count: u64,
    /// Cumulative number of cold `Evict` transitions executed on this page.
    #[get(type(copy))]
    pub(crate) evict_count: u64,
    /// Last host/shared-store revision synchronized onto this page.
    #[get(type(copy))]
    pub(crate) host_synced_revision: u64,
    /// Versioned snapshot retained while `Frozen` or `Evicted` (cleared on `Destroy`).
    pub(crate) snapshot: PageSnapshot,
    /// Whether `snapshot` contains a captured versioned snapshot.
    #[get(type(copy))]
    pub(crate) has_snapshot: bool,
    /// True while OS background suspension is active independently of route `Freeze`.
    #[get(type(copy))]
    pub(crate) os_background_suspended: bool,
    /// Latest graphics surface generation observed on surface loss/recreation.
    #[get(type(copy))]
    pub(crate) surface_lost_generation: u64,
    /// True while the graphics surface is currently lost independently of route `Freeze`.
    #[get(type(copy))]
    pub(crate) has_surface_lost: bool,
}

/// Structured report returned by `PageLifecycleController::enforce_budget`.
#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
pub struct PageEvictionReport {
    /// Total reconstructible cache bytes drained from frozen pages prior to any page eviction.
    #[get(type(copy))]
    pub(crate) reconstructible_bytes_freed: usize,
    /// Ordered list of cold page identifiers transitioned from `Frozen` to `Evicted`.
    pub(crate) evicted_page_ids: Vec<u64>,
    /// Number of frozen pages spared from silent eviction because they held unpersisted unsaved drafts.
    #[get(type(copy))]
    pub(crate) protected_unsaved_pages: usize,
}

/// Unified deterministic controller managing page `Freeze`, `Resume`, `Evict`, `Destroy`, generation guards, and boundary events.
#[derive(Clone, Data, Debug)]
pub struct PageLifecycleController {
    /// Active cold-page count and retained-byte budget policy.
    #[get(type(copy))]
    pub(crate) budget: PageBudgetPolicy,
    /// Registered pages keyed by `page_id`.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) pages: HashMap<u64, PageLifecycleEntry>,
    /// Next monotonic page identifier.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) next_page_id: u64,
    /// Monotonic logical activity tick for LRU cold eviction ordering.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) active_clock: u64,
    /// Current host/shared-store revision checked during `Resume` synchronization.
    #[get(type(copy))]
    pub(crate) host_revision: u64,
    /// Cumulative count of stale async callbacks rejected by `PageGenerationToken` guards.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) stale_callbacks_blocked: u64,
    /// Cumulative count of `LifecycleBoundaryEvent::OsBackground` events processed.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) os_background_events: u64,
    /// Cumulative count of `LifecycleBoundaryEvent::OsForeground` events processed.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) os_foreground_events: u64,
    /// Cumulative count of `LifecycleBoundaryEvent::SurfaceLost` events processed.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) surface_lost_events: u64,
    /// Cumulative count of `LifecycleBoundaryEvent::SurfaceRecreated` events processed.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) surface_recreated_events: u64,
    /// Cumulative count of explicitly destroyed pages.
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) total_destroyed_pages: u64,
}