pub struct History<E, S = ()> { /* private fields */ }
Expand description
A history tree of Edit
commands.
Unlike Record
which maintains a linear undo history,
History
maintains an undo tree containing every edit made to the target.
See this example for an interactive view of the history tree.
Examples
let mut target = String::new();
let mut history = History::new();
history.edit(&mut target, Add('a'));
history.edit(&mut target, Add('b'));
history.edit(&mut target, Add('c'));
let abc = history.head();
history.undo(&mut target);
history.undo(&mut target);
assert_eq!(target, "a");
// Instead of discarding 'b' and 'c', a new branch is created.
history.edit(&mut target, Add('f'));
history.edit(&mut target, Add('g'));
assert_eq!(target, "afg");
// We can now switch back to the original branch.
history.go_to(&mut target, abc);
assert_eq!(target, "abc");
Implementations§
source§impl<E, S> History<E, S>
impl<E, S> History<E, S>
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more edits.
Panics
Panics if the new capacity overflows usize.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the history as much as possible.
sourcepub fn connect(&mut self, slot: S) -> Option<S>
pub fn connect(&mut self, slot: S) -> Option<S>
Sets how the event should be handled when the state changes.
sourcepub fn disconnect(&mut self) -> Option<S>
pub fn disconnect(&mut self) -> Option<S>
Removes and returns the slot if it exists.
sourcepub fn is_saved(&self) -> bool
pub fn is_saved(&self) -> bool
Returns true
if the target is in a saved state, false
otherwise.
sourcepub fn next_branch_head(&self) -> Option<At>
pub fn next_branch_head(&self) -> Option<At>
Returns the head of the next branch in the history.
This will be the first edit that was stored in the branch.
This can be used in combination with History::go_to
to go to the next branch.
sourcepub fn prev_branch_head(&self) -> Option<At>
pub fn prev_branch_head(&self) -> Option<At>
Returns the head of the previous branch in the history.
This will be the first edit that was stored in the branch.
This can be used in combination with History::go_to
to go to the previous branch.
sourcepub fn get_entry(&self, index: usize) -> Option<&Entry<E>>
pub fn get_entry(&self, index: usize) -> Option<&Entry<E>>
Returns the entry at the index in the current root branch.
Use History::get_branch if you want to get entry from other branches.
sourcepub fn entries(&self) -> impl Iterator<Item = &Entry<E>>
pub fn entries(&self) -> impl Iterator<Item = &Entry<E>>
Returns an iterator over the entries in the current root branch.
sourcepub fn get_branch(&self, id: usize) -> Option<&Branch<E>>
pub fn get_branch(&self, id: usize) -> Option<&Branch<E>>
Returns the branch with the given id.
sourcepub fn branches(&self) -> impl Iterator<Item = (&usize, &Branch<E>)>
pub fn branches(&self) -> impl Iterator<Item = (&usize, &Branch<E>)>
Returns an iterator over the branches in the history.
This does not include the current root branch.
sourcepub fn checkpoint(&mut self) -> Checkpoint<'_, E, S>
pub fn checkpoint(&mut self) -> Checkpoint<'_, E, S>
Returns a checkpoint.
source§impl<E: Edit, S: Slot> History<E, S>
impl<E: Edit, S: Slot> History<E, S>
sourcepub fn edit(&mut self, target: &mut E::Target, edit: E) -> E::Output
pub fn edit(&mut self, target: &mut E::Target, edit: E) -> E::Output
Pushes the Edit
to the top of the history and executes its Edit::edit
method.
sourcepub fn undo(&mut self, target: &mut E::Target) -> Option<E::Output>
pub fn undo(&mut self, target: &mut E::Target) -> Option<E::Output>
Calls the Edit::undo
method for the active edit
and sets the previous one as the new active one.
sourcepub fn redo(&mut self, target: &mut E::Target) -> Option<E::Output>
pub fn redo(&mut self, target: &mut E::Target) -> Option<E::Output>
Calls the Edit::redo
method for the active edit
and sets the next one as the new active one.
sourcepub fn go_to(&mut self, target: &mut E::Target, at: At) -> Vec<E::Output>
pub fn go_to(&mut self, target: &mut E::Target, at: At) -> Vec<E::Output>
Repeatedly calls Edit::undo
or Edit::redo
until the edit at at
is reached.
source§impl<E: Display, S> History<E, S>
impl<E: Display, S> History<E, S>
sourcepub fn undo_string(&self) -> Option<String>
pub fn undo_string(&self) -> Option<String>
Returns the string of the edit which will be undone
in the next call to History::undo
.
sourcepub fn redo_string(&self) -> Option<String>
pub fn redo_string(&self) -> Option<String>
Returns the string of the edit which will be redone
in the next call to History::redo
.