Struct undo::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 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().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]

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 number of commands the stack can hold without reallocating.

Returns the number of Commands in the Record.

Returns true if the Record is empty.

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

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

Returns a reference to the receiver.

Consumes the Record, returning the receiver.

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 cmds 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());

Calls the redo method for the active UndoCmd and sets the next UndoCmd 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.

Calls the undo method for the active UndoCmd and sets the previous UndoCmd as the new active one.

Errors

If an error occur when executing undo the error is returned and the state of the stack is left unchanged.

Trait Implementations

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

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

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

Performs the conversion.

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

Formats the value using the given formatter.