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
use std::error::Error; use std::fmt::{self, Display, Formatter}; use redo::{Command, Record}; #[derive(Debug)] struct StrErr(&'static str); impl Display for StrErr { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str(self.0) } } impl Error for StrErr { fn description(&self) -> &str { self.0 } } #[derive(Debug)] struct Add(char); impl Command<String> for Add { type Err = StrErr; fn redo(&mut self, s: &mut String) -> Result<(), Self::Err> { s.push(self.0); Ok(()) } fn undo(&mut self, s: &mut String) -> Result<(), Self::Err> { self.0 = s.pop().ok_or(StrErr("`String` is unexpectedly empty"))?; Ok(()) } } fn foo() -> Result<(), Box<Error>> { let mut record = Record::default(); record.push(Add('a'))?; record.push(Add('b'))?; record.push(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]
fn new<T: Into<R>>(receiver: T) -> Record<'a, R, C>
[src]
Returns a new record.
fn builder() -> RecordBuilder<'a, R, C>
[src]
Returns a builder for a record.
fn capacity(&self) -> usize
[src]
Returns the capacity of the record.
fn len(&self) -> usize
[src]
Returns the number of commands in the record.
fn is_empty(&self) -> bool
[src]
Returns true
if the record is empty.
fn limit(&self) -> Option<usize>
[src]
Returns the limit of the record, or None
if it has no limit.
fn can_undo(&self) -> bool
[src]
Returns true
if the record can undo.
fn can_redo(&self) -> bool
[src]
Returns true
if the record can redo.
fn set_saved(&mut self)
[src]
Marks the receiver as currently being in a saved state.
fn set_unsaved(&mut self)
[src]
Marks the receiver as no longer being in a saved state.
fn is_saved(&self) -> bool
[src]
Returns true
if the receiver is in a saved state, false
otherwise.
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.
fn push(&mut self, cmd: C) -> Result<Commands<C>, Error<R, C>>
[src]
Pushes the command on top of the record and executes its redo
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 redo
or merging commands,
the error is returned together with the command.
Examples
let mut record = Record::default(); record.push(Add('a'))?; record.push(Add('b'))?; record.push(Add('c'))?; assert_eq!(record.as_receiver(), "abc"); record.undo().unwrap()?; record.undo().unwrap()?; let mut bc = record.push(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);
fn undo(&mut self) -> Option<Result<(), C::Err>>
[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.
fn redo(&mut self) -> Option<Result<(), C::Err>>
[src]
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 and the state is left unchanged.
fn as_receiver(&self) -> &R
[src]
Returns a reference to the receiver
.
fn into_receiver(self) -> R
[src]
Consumes the record, returning the receiver
.
impl<'a, R, C: Command<R> + ToString> Record<'a, R, C>
[src]
fn to_undo_string(&self) -> Option<String>
[src]
Returns the string of the command which will be undone in the next call to undo
.
fn to_redo_string(&self) -> Option<String>
[src]
Returns the string of the command which will be redone in the next call to redo
.