Struct undo::record::Record
[−]
[src]
pub struct Record<'a, 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 Commands 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 std::error::Error; use undo::{Command, Record}; struct Add(char); impl Command<String> for Add { fn redo(&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 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> Record<'a, R>[src]
fn new<T: Into<R>>(receiver: T) -> Record<'a, R>
Returns a new Record.
fn config<T: Into<R>>(receiver: T) -> Config<'a, R>
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 number of commands the stack can hold without reallocating.
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 stack is clean, false otherwise.
fn is_dirty(&self) -> bool
Returns true if the state of the stack 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<C>(&mut self, cmd: C) -> Result<Commands<R>, Error<R>> where
C: Command<R> + 'static,
R: 'static,
C: Command<R> + 'static,
R: 'static,
Pushes cmd to the 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 the error is returned together with the command,
and the state of the stack is left unchanged.
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!(bc.next().is_some()); assert!(bc.next().is_some()); assert!(bc.next().is_none());
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 command that caused the error is removed from
the record and returned together with the error.
fn undo(&mut self) -> Option<Result<(), Error<R>>>
Trait Implementations
impl<'a, R: Default> Default for Record<'a, R>[src]
impl<'a, R> AsRef<R> for Record<'a, R>[src]
fn as_ref(&self) -> &R
Performs the conversion.