Struct redo::Record
[−]
[src]
pub struct Record<'a, R, C: Command<R>> { /* fields omitted */ }
The command record.
The record works mostly like a stack, but it stores the commands
instead of returning them when undoing. This means it 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 MyError(&'static str); // impl Display + Error for MyError.. #[derive(Debug)] struct Add(char); impl Command<String> for Add { type Error = MyError; fn exec(&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(MyError("`String` is unexpectedly empty"))?; Ok(()) } } fn foo() -> Result<(), Box<Error>> { let mut record = Record::default(); record.exec(Add('a'))?; record.exec(Add('b'))?; record.exec(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<'a, R, C: Command<R>> Record<'a, R, C>
[src]
pub fn new<T: Into<R>>(receiver: T) -> Record<'a, R, C>
[src]
Returns a new record.
pub fn builder() -> RecordBuilder<'a, R, C>
[src]
Returns a builder for a record.
pub fn capacity(&self) -> usize
[src]
Returns the capacity of the record.
pub fn len(&self) -> usize
[src]
Returns the number of commands in the record.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the record is empty.
pub fn limit(&self) -> Option<usize>
[src]
Returns the limit of the record, or None
if it has no limit.
pub fn can_undo(&self) -> bool
[src]
Returns true
if the record can undo.
pub fn can_redo(&self) -> bool
[src]
Returns true
if the record can redo.
pub fn set_saved(&mut self)
[src]
Marks the receiver as currently being in a saved state.
pub fn set_unsaved(&mut self)
[src]
Marks the receiver as no longer being in a saved state.
pub fn is_saved(&self) -> bool
[src]
Returns true
if the receiver is in a saved state, false
otherwise.
pub fn clear(&mut self)
[src]
Removes all commands from the record without undoing them.
This resets the record back to its initial state and emits the appropriate signals, while leaving the receiver unmodified.
pub fn exec(&mut self, cmd: C) -> Result<Commands<C>, Error<R, C>>
[src]
Pushes the command on top of the record and executes its exec
method.
The command is merged with the previous top command if merge
does not return None
.
All commands above the active one are removed from the stack and returned as an iterator.
Errors
If an error occur when executing exec
or merging commands,
the error is returned together with the command.
Examples
let mut record = Record::default(); record.exec(Add('a'))?; record.exec(Add('b'))?; record.exec(Add('c'))?; assert_eq!(record.as_receiver(), "abc"); record.undo().unwrap()?; record.undo().unwrap()?; let mut bc = record.exec(Add('e'))?; assert_eq!(record.into_receiver(), "ae"); assert_eq!(bc.next(), Some(Add('b'))); assert_eq!(bc.next(), Some(Add('c'))); assert_eq!(bc.next(), None);
pub fn undo(&mut self) -> Option<Result<(), C::Error>>
[src]
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 and the state is left unchanged.
pub fn redo(&mut self) -> Option<Result<(), C::Error>>
[src]
Calls the exec
method for the active command and sets the next one as the new
active one.
Errors
If an error occur when executing exec
the
error is returned and the state is left unchanged.
pub fn as_receiver(&self) -> &R
[src]
Returns a reference to the receiver
.
pub fn into_receiver(self) -> R
[src]
Consumes the record, returning the receiver
.
impl<'a, R, C: Command<R> + ToString> Record<'a, R, C>
[src]
pub fn to_undo_string(&self) -> Option<String>
[src]
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]
Returns the string of the command which will be redone in the next call to redo
.
Trait Implementations
impl<'a, R: Default, C: Command<R>> Default for Record<'a, R, C>
[src]
impl<'a, R, C: Command<R>> AsRef<R> for Record<'a, R, C>
[src]
impl<'a, R, C: Command<R>> From<R> for Record<'a, R, C>
[src]
impl<'a, R: Debug, C: Command<R> + Debug> Debug for Record<'a, R, C>
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more