pub struct History<R> { /* private fields */ }
Expand description
A history of commands.
A history works like the Record but also provides branching, like vim’s undo-tree.
Examples
#[derive(Debug)]
struct Add(char);
impl Command<String> for Add {
fn apply(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
s.push(self.0);
Ok(())
}
fn undo(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
self.0 = s.pop().ok_or("`s` is empty")?;
Ok(())
}
}
fn main() -> undo::Result<String> {
let mut history = History::default();
history.apply(Add('a'))?;
history.apply(Add('b'))?;
history.apply(Add('c'))?;
assert_eq!(history.as_receiver(), "abc");
let root = history.root();
history.go_to(root, 1).unwrap()?;
assert_eq!(history.as_receiver(), "a");
let abc = history.root();
history.apply(Add('f'))?;
history.apply(Add('g'))?;
assert_eq!(history.as_receiver(), "afg");
history.go_to(abc, 3).unwrap()?;
assert_eq!(history.as_receiver(), "abc");
Ok(())
}
Implementations§
source§impl<R> History<R>
impl<R> History<R>
sourcepub fn builder() -> HistoryBuilder<R>
pub fn builder() -> HistoryBuilder<R>
Returns a builder for a history.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more commands.
Panics
Panics if the new capacity overflows usize.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of commands in the current branch of the history.
sourcepub fn set_limit(&mut self, limit: usize) -> usize
pub fn set_limit(&mut self, limit: usize) -> usize
Sets the limit of the history and returns the new limit.
If this limit is reached it will start popping of commands at the beginning of the history when new commands are applied. No limit is set by default which means it may grow indefinitely.
If limit < len
the first commands will be removed until len == limit
.
However, if the current active command is going to be removed, the limit is instead
adjusted to len - active
so the active command is not removed.
Panics
Panics if limit
is 0
.
sourcepub fn connect(&mut self, f: impl FnMut(Signal) + Send + Sync + 'static)
pub fn connect(&mut self, f: impl FnMut(Signal) + Send + Sync + 'static)
Sets how the signal should be handled when the state changes.
sourcepub fn set_saved(&mut self, saved: bool)
pub fn set_saved(&mut self, saved: bool)
Marks the receiver as currently being in a saved or unsaved state.
sourcepub fn is_saved(&self) -> bool
pub fn is_saved(&self) -> bool
Returns true
if the receiver is in a saved state, false
otherwise.
sourcepub fn revert(&mut self) -> Option<Result<R>>where
R: 'static,
pub fn revert(&mut self) -> Option<Result<R>>where
R: 'static,
Revert the changes done to the receiver since the saved state.
sourcepub fn checkpoint(&mut self) -> Checkpoint<'_, History<R>, R>
pub fn checkpoint(&mut self) -> Checkpoint<'_, History<R>, R>
Returns a checkpoint.
sourcepub fn as_receiver(&self) -> &R
pub fn as_receiver(&self) -> &R
Returns a reference to the receiver
.
sourcepub fn as_mut_receiver(&mut self) -> &mut R
pub fn as_mut_receiver(&mut self) -> &mut R
Returns a mutable reference to the receiver
.
This method should only be used when doing changes that should not be able to be undone.
sourcepub fn into_receiver(self) -> R
pub fn into_receiver(self) -> R
Consumes the history, returning the receiver
.