Struct redo::record::Record
[−]
[src]
pub struct Record<'a, R, C: Command<R>> { /* fields omitted */ }
A record of commands.
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
has an internal state that is either clean or dirty.
A clean state means that the Record
does not have any Command
s to redo,
while a dirty state means that it does. The user can give the Record
a function
that is called each time the state changes by using the config
constructor.
Examples
use redo::{Command, Error, Record}; #[derive(Debug)] struct Add(char); impl Command<String> for Add { type Err = &'static str; fn redo(&mut self, s: &mut String) -> Result<(), &'static str> { s.push(self.0); Ok(()) } fn undo(&mut self, s: &mut String) -> Result<(), &'static str> { self.0 = s.pop().ok_or("`String` is unexpectedly empty")?; Ok(()) } } fn foo() -> Result<(), Error<String, Add>> { 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>
Returns a new Record
.
fn config<T: Into<R>>(receiver: T) -> Config<'a, R, C>
Returns a configurator for a Record
.
Examples
let mut record = Record::config("") .capacity(2) .limit(2) .create(); record.push(Add('a'))?; record.push(Add('b'))?; record.push(Add('c'))?; // 'a' is removed from the record since limit is 2. assert_eq!(record.as_receiver(), "abc"); record.undo().unwrap()?; record.undo().unwrap()?; assert!(record.undo().is_none()); assert_eq!(record.into_receiver(), "a");
fn limit(&self) -> Option<usize>
Returns the limit of the Record
, or None
if it has no limit.
fn capacity(&self) -> usize
Returns the capacity of the Record
.
fn len(&self) -> usize
Returns the number of commands in the Record
.
fn is_empty(&self) -> bool
Returns true
if the Record
is empty.
fn is_clean(&self) -> bool
Returns true
if the state of the Record
is clean, false
otherwise.
fn is_dirty(&self) -> bool
Returns true
if the state of the Record
is dirty, false
otherwise.
fn as_receiver(&self) -> &R
Returns a reference to the receiver
.
fn into_receiver(self) -> R
Consumes the Record
, returning the receiver
.
fn push(&mut self, cmd: C) -> Result<Commands<C>, Error<R, C>>
Pushes cmd
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 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 command that caused the error is removed from
the record and returned together with the error.
fn undo(&mut self) -> Option<Result<(), Error<R, C>>>
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]
fn as_ref(&self) -> &R
Performs the conversion.