Struct undo::record::Record
[−]
[src]
pub struct Record<'a, T> { /* 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 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().expect("`String` is unexpectedly empty"); Ok(()) } } fn foo() -> Result<(), Box<Error>> { let mut record = Record::default(); record.push(Add('a')).map_err(|(_, e)| e)?; record.push(Add('b')).map_err(|(_, e)| e)?; record.push(Add('c')).map_err(|(_, e)| e)?; assert_eq!(record.as_receiver(), "abc"); record.undo()?; record.undo()?; record.undo()?; assert_eq!(record.as_receiver(), ""); record.redo()?; record.redo()?; record.redo()?; assert_eq!(record.into_receiver(), "abc"); Ok(()) }
Methods
impl<'a, T> Record<'a, T>
[src]
fn new<U: Into<T>>(receiver: U) -> Record<'a, T>
Returns a new Record
.
fn config<U: Into<T>>(receiver: U) -> Config<'a, T>
Returns a configurator for a Record
.
Examples
let mut record = Record::config("") .capacity(2) .limit(2) .finish(); record.push(Add('a')).map_err(|(_, e)| e)?; record.push(Add('b')).map_err(|(_, e)| e)?; record.push(Add('c')).map_err(|(_, e)| e)?; // 'a' is removed from the record since limit is 2. assert_eq!(record.as_receiver(), "abc"); record.undo()?; record.undo()?; record.undo()?; assert_eq!(record.as_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 Command
s 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) -> &T
Returns a reference to the receiver
.
fn into_receiver(self) -> T
Consumes the Record
, returning the receiver
.
fn push<C>(
&mut self,
cmd: C
) -> Result<Commands<T>, (Box<Command<T>>, Box<Error>)> where
C: Command<T> + 'static,
T: 'static,
&mut self,
cmd: C
) -> Result<Commands<T>, (Box<Command<T>>, Box<Error>)> where
C: Command<T> + 'static,
T: 'static,
Pushes cmd
to the top of the stack and executes its redo
method.
This pops off all other commands above the active command from the stack.
If cmd
s id is equal to the top command on the stack, the two commands are merged.
Errors
If an error occur when executing redo
the error is returned
and the state of the stack is left unchanged.
Examples
let mut record = Record::default(); record.push(Add('a')).map_err(|(_, e)| e)?; record.push(Add('b')).map_err(|(_, e)| e)?; record.push(Add('c')).map_err(|(_, e)| e)?; assert_eq!(record.as_receiver(), "abc"); record.undo()?; record.undo()?; let mut bc = record.push(Add('e')).map_err(|(_, e)| e)?; assert_eq!(record.as_receiver(), "ae"); assert!(bc.next().is_some()); assert!(bc.next().is_some()); assert!(bc.next().is_none());
fn redo(&mut self) -> Result<(), Box<Error>>
Calls the redo
method for the active Command
and sets the next Command
as the new
active one.
Errors
If an error occur when executing redo
the error is returned
and the state of the stack is left unchanged.
fn undo(&mut self) -> Result<(), Box<Error>>
Trait Implementations
impl<'a, T: Default> Default for Record<'a, T>
[src]
impl<'a, T> AsRef<T> for Record<'a, T>
[src]
fn as_ref(&self) -> &T
Performs the conversion.