Struct undo::Record [−][src]
pub struct Record<R> { /* fields omitted */ }
A record of commands.
The record can roll the receivers 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 receiver through signals. The user
can give the record a function that is called each time the state changes
by using the builder
.
Examples
#[derive(Debug)] struct Add(char); impl Command<String> for Add { fn apply(&mut self, s: &mut String) -> Result<(), Box<Error>> { s.push(self.0); Ok(()) } fn undo(&mut self, s: &mut String) -> Result<(), Box<Error>> { self.0 = s.pop().ok_or("`String` is unexpectedly empty")?; Ok(()) } } fn main() -> Result<(), Box<Error>> { let mut record = Record::default(); record.apply(Add('a'))?; record.apply(Add('b'))?; record.apply(Add('c'))?; assert_eq!(record.as_receiver(), "abc"); record.undo().unwrap()?; record.undo().unwrap()?; record.undo().unwrap()?; assert_eq!(record.as_receiver(), ""); record.redo().unwrap()?; record.redo().unwrap()?; record.redo().unwrap()?; assert_eq!(record.into_receiver(), "abc"); Ok(()) }
Methods
impl<R> Record<R>
[src]
impl<R> Record<R>
pub fn new<T: Into<R>>(receiver: T) -> Record<R>
[src]
pub fn new<T: Into<R>>(receiver: T) -> Record<R>
Returns a new record.
pub fn builder() -> RecordBuilder<R>
[src]
pub fn builder() -> RecordBuilder<R>
Returns a builder for a record.
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 record.
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the number of commands in the record.
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true
if the record is empty.
pub fn limit(&self) -> usize
[src]
pub fn limit(&self) -> usize
Returns the limit of the record.
pub fn set_limit(&mut self, limit: usize) -> usize
[src]
pub fn set_limit(&mut self, limit: usize) -> usize
Sets the limit of the record and returns the new limit.
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 that the active command is not popped off.
pub fn set_signals<F>(&mut self, f: F) where
F: FnMut(Signal) + Send + Sync + 'static,
[src]
pub fn set_signals<F>(&mut self, f: F) where
F: FnMut(Signal) + Send + Sync + 'static,
Sets how different signals should be handled when the state changes.
pub fn can_undo(&self) -> bool
[src]
pub fn can_undo(&self) -> bool
Returns true
if the record can undo.
pub fn can_redo(&self) -> bool
[src]
pub fn can_redo(&self) -> bool
Returns true
if the record can redo.
pub fn set_saved(&mut self)
[src]
pub fn set_saved(&mut self)
Marks the receiver as currently being in a saved state.
pub fn set_unsaved(&mut self)
[src]
pub fn set_unsaved(&mut self)
Marks the receiver as no longer being in a saved 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 clear(&mut self)
[src]
pub fn clear(&mut self)
Removes all commands from the record without undoing them.
pub fn apply<C>(
&mut self,
cmd: C
) -> Result<impl Iterator<Item = Box<Command<R> + 'static>>, Error<R>> where
C: Command<R> + 'static,
R: 'static,
[src]
pub fn apply<C>(
&mut self,
cmd: C
) -> Result<impl Iterator<Item = Box<Command<R> + 'static>>, Error<R>> where
C: Command<R> + 'static,
R: 'static,
Pushes the command to the top of the record and executes its apply
method.
The command is merged with the previous top command if they have the same id
.
All commands above the active one are removed from the stack and returned as an iterator.
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 cursor(&self) -> usize
[src]
pub fn cursor(&self) -> usize
Returns the position of the current command.
pub fn set_cursor(&mut self, cursor: usize) -> Option<Result<(), Error<R>>>
[src]
pub fn set_cursor(&mut self, cursor: usize) -> Option<Result<(), Error<R>>>
Repeatedly calls undo
or redo
until the command at cursor
is reached.
The signals are emitted once after reaching the cursor
.
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) -> &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 record, returning the receiver
.
Trait Implementations
impl<R: Default> Default for Record<R>
[src]
impl<R: Default> Default for Record<R>
impl<R> AsRef<R> for Record<R>
[src]
impl<R> AsRef<R> for Record<R>
impl<R> AsMut<R> for Record<R>
[src]
impl<R> AsMut<R> for Record<R>
impl<R> From<R> for Record<R>
[src]
impl<R> From<R> for Record<R>
impl<R: Debug> Debug for Record<R>
[src]
impl<R: Debug> Debug for Record<R>