pub struct Record<A, S = Nop> { /* private fields */ }
Expand description
A linear record of actions.
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 Signal
.
The user can give the record a function that is called each time the state
changes by using the Builder
.
Examples
let mut target = String::new();
let mut record = Record::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('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);
record.redo(&mut target);
assert_eq!(target, "abc");
Implementations§
source§impl<A> Record<A>
impl<A> Record<A>
sourcepub fn new() -> Record<A>
pub fn new() -> Record<A>
Returns a new record.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut record = Record::new();
let mut target = String::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "a");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
println!("{}", record.display());
}
source§impl<A, S> Record<A, S>
impl<A, S> Record<A, S>
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more actions.
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 signal 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 display(&self) -> Display<'_, A, S>
pub fn display(&self) -> Display<'_, A, S>
Returns a structure for configurable formatting of the record.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut record = Record::new();
let mut target = String::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "a");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
println!("{}", record.display());
}
sourcepub fn checkpoint(&mut self) -> Checkpoint<'_, A, S>
pub fn checkpoint(&mut self) -> Checkpoint<'_, A, S>
Returns a checkpoint.
source§impl<A: Action, S: Slot> Record<A, S>
impl<A: Action, S: Slot> Record<A, S>
sourcepub fn apply(&mut self, target: &mut A::Target, action: A) -> A::Output
pub fn apply(&mut self, target: &mut A::Target, action: A) -> A::Output
Pushes the action on top of the record and executes its Action::apply
method.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut record = Record::new();
let mut target = String::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "a");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
println!("{}", record.display());
}
sourcepub fn undo(&mut self, target: &mut A::Target) -> Option<A::Output>
pub fn undo(&mut self, target: &mut A::Target) -> Option<A::Output>
Calls the Action::undo
method for the active action and sets
the previous one as the new active one.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut record = Record::new();
let mut target = String::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "a");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
println!("{}", record.display());
}
sourcepub fn redo(&mut self, target: &mut A::Target) -> Option<A::Output>
pub fn redo(&mut self, target: &mut A::Target) -> Option<A::Output>
Calls the Action::redo
method for the active action and sets
the next one as the new active one.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
let mut record = Record::new();
let mut target = String::new();
record.apply(&mut target, Push('a'));
record.apply(&mut target, Push('b'));
record.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "a");
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
println!("{}", record.display());
}
sourcepub fn set_saved(&mut self, saved: bool)
pub fn set_saved(&mut self, saved: bool)
Marks the target as currently being in a saved or unsaved state.
sourcepub fn revert(&mut self, target: &mut A::Target) -> Vec<A::Output>
pub fn revert(&mut self, target: &mut A::Target) -> Vec<A::Output>
Revert the changes done to the target since the saved state.
sourcepub fn go_to(
&mut self,
target: &mut A::Target,
current: usize
) -> Vec<A::Output>
pub fn go_to( &mut self, target: &mut A::Target, current: usize ) -> Vec<A::Output>
Repeatedly calls Action::undo
or Action::redo
until the action at current
is reached.
source§impl<A: ToString, S> Record<A, S>
impl<A: ToString, S> Record<A, S>
sourcepub fn undo_string(&self) -> Option<String>
pub fn undo_string(&self) -> Option<String>
Returns the string of the action 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 action which will be redone
in the next call to Record::redo
.