Struct undo::History [−][src]
pub struct History<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 { 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() -> Result<(), Box<dyn Error>> { 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.apply(Add('f'))?.unwrap(); 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> History<R>[src]
impl<R> History<R>pub fn new(
receiver: impl Into<R>
) -> History<R>[src]
pub fn new(
receiver: impl Into<R>
) -> History<R>Returns a new history.
pub fn builder() -> HistoryBuilder<R>[src]
pub fn builder() -> HistoryBuilder<R>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) -> usizeReturns the capacity of the history.
pub fn len(&self) -> usize[src]
pub fn len(&self) -> usizeReturns the number of commands in the current branch of the history.
pub fn is_empty(&self) -> bool[src]
pub fn is_empty(&self) -> boolReturns true if the current branch of the history is empty.
pub fn limit(&self) -> usize[src]
pub fn limit(&self) -> usizeReturns the limit of the history.
pub fn set_limit(&mut self, limit: usize) -> usize[src]
pub fn set_limit(&mut self, limit: usize) -> usizeSets 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) -> boolReturns true if the history can undo.
pub fn can_redo(&self) -> bool[src]
pub fn can_redo(&self) -> boolReturns 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) -> boolReturns true if the receiver is in a saved state, false otherwise.
pub fn root(&self) -> usize[src]
pub fn root(&self) -> usizeReturns the current branch.
pub fn cursor(&self) -> usize[src]
pub fn cursor(&self) -> usizeReturns 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,
cmd: impl Command<R> + 'static
) -> Result<Option<usize>, Error<R>> where
R: 'static, [src]
pub fn apply(
&mut self,
cmd: impl Command<R> + 'static
) -> Result<Option<usize>, Error<R>> where
R: 'static, Pushes the command to the top of the history and executes its apply method.
The command is merged with the previous top command if they have the same id.
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>>>[src]
pub fn undo(&mut self) -> Option<Result<(), Error<R>>>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>>>[src]
pub fn redo(&mut self) -> Option<Result<(), Error<R>>>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<usize, Error<R>>> where
R: 'static, [src]
pub fn go_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<usize, Error<R>>> where
R: 'static, 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 jump_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<usize, Error<R>>> where
R: 'static, [src]
pub fn jump_to(
&mut self,
branch: usize,
cursor: usize
) -> Option<Result<usize, Error<R>>> where
R: 'static, Jump directly to the command in branch at cursor and executes its undo or redo method.
This method can be used if the commands store the whole state of the receiver,
and does not require the commands in between to be called to get the same result.
Use go_to otherwise.
Errors
If an error occur when executing undo or redo the error is returned together with the command.
pub fn as_receiver(&self) -> &R[src]
pub fn as_receiver(&self) -> &RReturns a reference to the receiver.
pub fn as_mut_receiver(&mut self) -> &mut R[src]
pub fn as_mut_receiver(&mut self) -> &mut RReturns 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) -> RConsumes the history, returning the receiver.
pub fn commands(
&self
) -> impl Iterator<Item = &impl Command<R>>[src]
pub fn commands(
&self
) -> impl Iterator<Item = &impl Command<R>>Returns an iterator over the commands in the current branch.
Trait Implementations
impl<R: Debug> Debug for History<R>[src]
impl<R: Debug> Debug for History<R>fn fmt(&self, f: &mut Formatter) -> Result[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormats the value using the given formatter. Read more
impl<R: Default> Default for History<R>[src]
impl<R: Default> Default for History<R>impl<R> AsRef<R> for History<R>[src]
impl<R> AsRef<R> for History<R>impl<R> AsMut<R> for History<R>[src]
impl<R> AsMut<R> for History<R>impl<R> From<R> for History<R>[src]
impl<R> From<R> for History<R>impl<R> From<Record<R>> for History<R>[src]
impl<R> From<Record<R>> for History<R>impl<R> From<History<R>> for Record<R>[src]
impl<R> From<History<R>> for Record<R>