Struct redo::History [−][src]
pub struct History<R, C: Command<R>> { /* fields omitted */ }
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 { type Error = Box<dyn error::Error>; fn apply(&mut self, s: &mut String) -> Result<(), Self::Error> { s.push(self.0); Ok(()) } fn undo(&mut self, s: &mut String) -> Result<(), Self::Error> { self.0 = s.pop().ok_or("`s` is empty")?; Ok(()) } } fn main() -> Result<(), Error<String, Add>> { 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(()) }
Methods
impl<R, C: Command<R>> History<R, C>
[src]
impl<R, C: Command<R>> History<R, C>
pub fn new(
receiver: impl Into<R>
) -> History<R, C>
[src]
pub fn new(
receiver: impl Into<R>
) -> History<R, C>
Returns a new history.
pub fn builder() -> HistoryBuilder<R, C>
[src]
pub fn builder() -> HistoryBuilder<R, C>
Returns a builder for a history.
pub fn reserve(&mut self, additional: usize)
[src]
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more commands.
Panics
Panics if the new capacity overflows usize.
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
Returns the capacity of the history.
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the number of commands in the current branch of the history.
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true
if the current branch of the history is empty.
pub fn limit(&self) -> usize
[src]
pub fn limit(&self) -> usize
Returns the limit of the history.
pub fn set_limit(&mut self, limit: usize) -> usize
[src]
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
.
pub fn set_signal(
&mut self,
f: impl FnMut(Signal) + Send + Sync + 'static
)
[src]
pub fn set_signal(
&mut self,
f: impl FnMut(Signal) + Send + Sync + 'static
)
Sets how the signal should be handled when the state changes.
pub fn can_undo(&self) -> bool
[src]
pub fn can_undo(&self) -> bool
Returns true
if the history can undo.
pub fn can_redo(&self) -> bool
[src]
pub fn can_redo(&self) -> bool
Returns true
if the history can redo.
pub fn set_saved(&mut self, saved: bool)
[src]
pub fn set_saved(&mut self, saved: bool)
Marks the receiver as currently being in a saved or unsaved state.
pub fn is_saved(&self) -> bool
[src]
pub fn is_saved(&self) -> bool
Returns true
if the receiver is in a saved state, false
otherwise.
pub fn revert(&mut self) -> Option<Result<(), Error<R, C>>>
[src]
pub fn revert(&mut self) -> Option<Result<(), Error<R, C>>>
Revert the changes done to the receiver since the saved state.
pub fn root(&self) -> usize
[src]
pub fn root(&self) -> usize
Returns the current branch.
pub fn cursor(&self) -> usize
[src]
pub fn cursor(&self) -> usize
Returns the position of the current command.
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Removes all commands from the history without undoing them.
pub fn apply(&mut self, command: C) -> Result<(), Error<R, C>>
[src]
pub fn apply(&mut self, command: C) -> Result<(), Error<R, C>>
Pushes the command to the top of the history and executes its apply
method.
Errors
If an error occur when executing apply
the error is returned together with the command.
pub fn undo(&mut self) -> Option<Result<(), Error<R, C>>>
[src]
pub fn undo(&mut self) -> Option<Result<(), Error<R, C>>>
Calls the undo
method for the active command and sets the previous one as the new active one.
Errors
If an error occur when executing undo
the error is returned together with the command.
pub fn redo(&mut self) -> Option<Result<(), Error<R, C>>>
[src]
pub fn redo(&mut self) -> Option<Result<(), Error<R, C>>>
Calls the redo
method for the active command and sets the next one as the
new active one.
Errors
If an error occur when executing redo
the error is returned together with the command.
pub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R, C>>>
[src]
pub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<(), Error<R, C>>>
Repeatedly calls undo
or redo
until the command in branch
at cursor
is reached.
Errors
If an error occur when executing undo
or redo
the error is returned together with the command.
pub fn checkpoint(&mut self) -> Checkpoint<History<R, C>, C>
[src]
pub fn checkpoint(&mut self) -> Checkpoint<History<R, C>, C>
Returns a checkpoint.
pub fn queue(&mut self) -> Queue<History<R, C>, C>
[src]
pub fn queue(&mut self) -> Queue<History<R, C>, C>
Returns a queue.
pub fn as_receiver(&self) -> &R
[src]
pub fn as_receiver(&self) -> &R
Returns a reference to the receiver
.
pub fn as_mut_receiver(&mut self) -> &mut R
[src]
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.
pub fn into_receiver(self) -> R
[src]
pub fn into_receiver(self) -> R
Consumes the history, returning the receiver
.
pub fn commands(
&self
) -> impl Iterator<Item = &C>
[src]
pub fn commands(
&self
) -> impl Iterator<Item = &C>
Returns an iterator over the commands in the current branch.
pub fn branches(
&self
) -> impl Iterator<Item = &usize>
[src]
pub fn branches(
&self
) -> impl Iterator<Item = &usize>
Returns an iterator over the branches in the history, excluding the root branch.
impl<R, C: Command<R> + ToString> History<R, C>
[src]
impl<R, C: Command<R> + ToString> History<R, C>
pub fn to_undo_string(&self) -> Option<String>
[src]
pub fn to_undo_string(&self) -> Option<String>
Returns the string of the command which will be undone in the next call to undo
.
pub fn to_redo_string(&self) -> Option<String>
[src]
pub fn to_redo_string(&self) -> Option<String>
Returns the string of the command which will be redone in the next call to redo
.
pub fn display(&self) -> Display<Self>
[src]
pub fn display(&self) -> Display<Self>
Returns a structure for configurable formatting of the record.
Trait Implementations
impl<R: Debug, C: Debug + Command<R>> Debug for History<R, C>
[src]
impl<R: Debug, C: Debug + Command<R>> Debug for History<R, C>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<R: Default, C: Command<R>> Default for History<R, C>
[src]
impl<R: Default, C: Command<R>> Default for History<R, C>
impl<R, C: Command<R>> AsRef<R> for History<R, C>
[src]
impl<R, C: Command<R>> AsRef<R> for History<R, C>
impl<R, C: Command<R>> AsMut<R> for History<R, C>
[src]
impl<R, C: Command<R>> AsMut<R> for History<R, C>
impl<R, C: Command<R>> From<R> for History<R, C>
[src]
impl<R, C: Command<R>> From<R> for History<R, C>
impl<R, C: Command<R>> From<Record<R, C>> for History<R, C>
[src]
impl<R, C: Command<R>> From<Record<R, C>> for History<R, C>
impl<R, C: Command<R> + Display> Display for History<R, C>
[src]
impl<R, C: Command<R> + Display> Display for History<R, C>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<R, C: Command<R>> From<History<R, C>> for Record<R, C>
[src]
impl<R, C: Command<R>> From<History<R, C>> for Record<R, C>