pub struct History<A, S = Nop> { /* private fields */ }
Expand description
A history tree of actions.
Unlike Record
which maintains a linear undo history,
History
maintains an undo tree containing every edit made to the target.
Examples
let mut target = String::new();
let mut history = History::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
let abc = history.branch();
history.go_to(&mut target, abc, 1);
history.apply(&mut target, Push('f'));
history.apply(&mut target, Push('g'));
assert_eq!(target, "afg");
history.go_to(&mut target, abc, 3);
assert_eq!(target, "abc");
Implementations§
source§impl<A> History<A>
impl<A> History<A>
sourcepub fn new() -> History<A>
pub fn new() -> History<A>
Returns a new history.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.display());
}
source§impl<A, S> History<A, S>
impl<A, S> History<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 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 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 branch(&self) -> usize
pub fn branch(&self) -> usize
Returns the current branch.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.display());
}
sourcepub fn current(&self) -> usize
pub fn current(&self) -> usize
Returns the position of the current action.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.display());
}
sourcepub fn display(&self) -> Display<'_, A, S>
pub fn display(&self) -> Display<'_, A, S>
Returns a structure for configurable formatting of the history.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.display());
}
sourcepub fn actions(&self) -> impl Iterator<Item = &A>
pub fn actions(&self) -> impl Iterator<Item = &A>
Returns an iterator over the actions in the current branch.
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> History<A, S>
impl<A: Action, S: Slot> History<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
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.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 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.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.
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 go_to(
&mut self,
target: &mut A::Target,
branch: usize,
current: usize
) -> Vec<A::Output>
pub fn go_to( &mut self, target: &mut A::Target, branch: usize, current: usize ) -> Vec<A::Output>
Repeatedly calls Action::undo
or Action::redo
until the action in branch
at current
is reached.
Examples found in repository?
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() {
let mut history = History::new();
let mut target = String::new();
history.apply(&mut target, Push('a'));
history.apply(&mut target, Push('b'));
history.apply(&mut target, Push('c'));
assert_eq!(target, "abc");
let abc_branch = history.branch();
let abc_current = history.current();
history.undo(&mut target);
assert_eq!(target, "ab");
history.apply(&mut target, Push('d'));
history.apply(&mut target, Push('e'));
history.apply(&mut target, Push('f'));
assert_eq!(target, "abdef");
let abdef_branch = history.branch();
let abdef_current = history.current();
history.go_to(&mut target, abc_branch, abc_current);
assert_eq!(target, "abc");
history.go_to(&mut target, abdef_branch, abdef_current);
assert_eq!(target, "abdef");
println!("{}", history.display());
}
source§impl<A: ToString, S> History<A, S>
impl<A: ToString, S> History<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 History::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 History::redo
.