pub struct Record<E, S = ()> { /* private fields */ }
Expand description
A linear record of Edit
commands.
The record can roll the targets state backwards and forwards by using
the undo and redo methods. In addition, the record can notify the user
about changes to the stack or the target through Event
.
The user can give the record a function that is called each time the state
changes by using the Builder
.
When adding a new edit command to the record the previously undone commands
will be discarded.
If you want to keep all edits you can use History
instead.
Examples
let mut target = String::new();
let mut record = Record::new();
record.edit(&mut target, Add('a'));
record.edit(&mut target, Add('b'));
record.edit(&mut target, Add('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "ab");
// 'c' will be discarded.
record.edit(&mut target, Add('d'));
assert_eq!(target, "abd");
Implementations§
source§impl<E, S> Record<E, S>
impl<E, S> Record<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 record 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 checkpoint(&mut self) -> Checkpoint<'_, E, S>
pub fn checkpoint(&mut self) -> Checkpoint<'_, E, S>
Returns a checkpoint.
source§impl<E: Edit, S: Slot> Record<E, S>
impl<E: Edit, S: Slot> Record<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 on top of the record 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 revert(&mut self, target: &mut E::Target) -> Vec<E::Output>
pub fn revert(&mut self, target: &mut E::Target) -> Vec<E::Output>
Revert the changes done to the target since the saved state.
sourcepub fn go_to(&mut self, target: &mut E::Target, index: usize) -> Vec<E::Output>
pub fn go_to(&mut self, target: &mut E::Target, index: usize) -> Vec<E::Output>
Repeatedly calls Edit::undo
or Edit::redo
until the edit at index
is reached.
source§impl<E: Display, S> Record<E, S>
impl<E: Display, S> Record<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 Record::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 Record::redo
.