pub struct SnapshotStore<T> { /* private fields */ }Expand description
A snapshot-based undo/redo store using Arc<T> for structural sharing.
T should ideally use persistent data structures internally (e.g.,
im::HashMap) so that cloning is O(1) and snapshots share memory.
§Invariants
undo_stackis never empty after the firstpush.undo_stack.len() <= config.max_depth(after any operation).- Redo stack is cleared on every
push. current()always returns the most recently pushed or restored snapshot.
Implementations§
Source§impl<T> SnapshotStore<T>
impl<T> SnapshotStore<T>
Sourcepub fn new(config: SnapshotConfig) -> Self
pub fn new(config: SnapshotConfig) -> Self
Create a new snapshot store with the given configuration.
Sourcepub fn with_default_config() -> Self
pub fn with_default_config() -> Self
Create a new snapshot store with default configuration.
Sourcepub fn push(&mut self, state: T)
pub fn push(&mut self, state: T)
Push a new snapshot, clearing the redo stack (new branch).
The snapshot is wrapped in Arc for structural sharing.
If the undo stack exceeds max_depth, the oldest snapshot is evicted.
Sourcepub fn push_arc(&mut self, state: Arc<T>)
pub fn push_arc(&mut self, state: Arc<T>)
Push a pre-wrapped Arc<T> snapshot.
Use this when you already have an Arc<T> and want to avoid
double-wrapping.
Sourcepub fn undo(&mut self) -> Option<Arc<T>>
pub fn undo(&mut self) -> Option<Arc<T>>
Undo: move the current snapshot to the redo stack and return the previous snapshot.
Returns None if there is only one snapshot (the initial state)
or the store is empty.
Sourcepub fn redo(&mut self) -> Option<Arc<T>>
pub fn redo(&mut self) -> Option<Arc<T>>
Redo: move the most recently undone snapshot back to the undo stack.
Returns None if there is nothing to redo.
Sourcepub fn current(&self) -> Option<&Arc<T>>
pub fn current(&self) -> Option<&Arc<T>>
Get the current snapshot (the most recent on the undo stack).
Returns None if the store is empty.
Sourcepub fn undo_depth(&self) -> usize
pub fn undo_depth(&self) -> usize
Number of snapshots on the undo stack (including current).
Sourcepub fn redo_depth(&self) -> usize
pub fn redo_depth(&self) -> usize
Number of snapshots on the redo stack.
Sourcepub fn total_snapshots(&self) -> usize
pub fn total_snapshots(&self) -> usize
Total number of snapshots across both stacks.
Sourcepub fn config(&self) -> &SnapshotConfig
pub fn config(&self) -> &SnapshotConfig
Get the configuration.