Struct redo::record::Record [] [src]

pub struct Record<'a, T, C: Command<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 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 redo::{Command, 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<(), &'static str> {
    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, C: Command<T>> Record<'a, T, C>
[src]

Returns a new Record.

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");

Returns the limit of the Record, or None if it has no limit.

Returns the capacity of the Record.

Returns the number of Commands in the Record.

Returns true if the Record is empty.

Returns true if the state of the Record is clean, false otherwise.

Returns true if the state of the Record is dirty, false otherwise.

Returns a reference to the receiver.

Consumes the Record, returning the receiver.

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')).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_eq!(bc.next(), Some(Add('b')));
assert_eq!(bc.next(), Some(Add('c')));
assert_eq!(bc.next(), None);

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.

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.

Trait Implementations

impl<'a, T: Default, C: Command<T>> Default for Record<'a, T, C>
[src]

Returns the "default value" for a type. Read more

impl<'a, T, C: Command<T>> AsRef<T> for Record<'a, T, C>
[src]

Performs the conversion.

impl<'a, T: Debug, C: Command<T> + Debug> Debug for Record<'a, T, C>
[src]

Formats the value using the given formatter.