Skip to main content

Module snapshot_store

Module snapshot_store 

Source
Expand description

Persistent-snapshot undo/redo store.

SnapshotStore provides O(1) snapshot capture and restore using Arc-based structural sharing. Each call to push clones the Arc (not the state), so 1000 snapshots of a large state cost barely more than a single copy when the state uses persistent data structures (e.g., im::HashMap, im::Vector).

§Architecture

push(s3)
┌──────────────────────────────────────────────────┐
│ Undo Stack:  [Arc(s0), Arc(s1), Arc(s2), Arc(s3)]│
│ Redo Stack:  []                                   │
│ Current:     Arc(s3)                              │
└──────────────────────────────────────────────────┘

undo() x2
┌──────────────────────────────────────────────────┐
│ Undo Stack:  [Arc(s0), Arc(s1)]                  │
│ Redo Stack:  [Arc(s2), Arc(s3)]                  │
│ Current:     Arc(s1)                              │
└──────────────────────────────────────────────────┘

push(s4) — new branch, clears redo
┌──────────────────────────────────────────────────┐
│ Undo Stack:  [Arc(s0), Arc(s1), Arc(s4)]         │
│ Redo Stack:  []                                   │
│ Current:     Arc(s4)                              │
└──────────────────────────────────────────────────┘

§When to Use

Use SnapshotStore when your state type T uses persistent collections (e.g., im::HashMap, im::Vector) so that Arc::new(state.clone()) is cheap thanks to structural sharing. For command-pattern undo (reversible mutations), use HistoryManager.

§Memory Model

Each snapshot is an Arc<T>. When T uses persistent data structures, cloning T shares most of the underlying memory. The store enforces configurable depth limits. Memory is reclaimed when the last Arc referencing a snapshot is dropped.

Structs§

SnapshotConfig
Configuration for the snapshot store.
SnapshotStore
A snapshot-based undo/redo store using Arc<T> for structural sharing.