Struct redo::Record [] [src]

pub struct Record<'a, R, C: Command<R>> { /* fields omitted */ }

The command record.

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 can notify the user about changes to the stack or the receiver through signals. The user can give the record a function that is called each time the state changes by using the builder.

Examples

#[derive(Debug)]
struct MyError(&'static str);

// impl Display + Error for MyError..

#[derive(Debug)]
struct Add(char);

impl Command<String> for Add {
    type Error = MyError;

    fn exec(&mut self, s: &mut String) -> Result<(), Self::Error> {
        s.push(self.0);
        Ok(())
    }

    fn undo(&mut self, s: &mut String) -> Result<(), Self::Error> {
        self.0 = s.pop().ok_or(MyError("`String` is unexpectedly empty"))?;
        Ok(())
    }
}

fn foo() -> Result<(), Box<Error>> {
    let mut record = Record::default();

    record.exec(Add('a'))?;
    record.exec(Add('b'))?;
    record.exec(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]

[src]

Returns a new record.

[src]

Returns a builder for a record.

[src]

Returns the capacity of the record.

[src]

Returns the number of commands in the record.

[src]

Returns true if the record is empty.

[src]

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

[src]

Returns true if the record can undo.

[src]

Returns true if the record can redo.

[src]

Marks the receiver as currently being in a saved state.

[src]

Marks the receiver as no longer being in a saved state.

[src]

Returns true if the receiver is in a saved state, false otherwise.

[src]

Removes all commands from the record without undoing them.

This resets the record back to its initial state and emits the appropriate signals, while leaving the receiver unmodified.

[src]

Pushes the command on top of the record and executes its exec 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 exec or merging commands, the error is returned together with the command.

Examples

let mut record = Record::default();

record.exec(Add('a'))?;
record.exec(Add('b'))?;
record.exec(Add('c'))?;

assert_eq!(record.as_receiver(), "abc");

record.undo().unwrap()?;
record.undo().unwrap()?;
let mut bc = record.exec(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);

[src]

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 and the state is left unchanged.

[src]

Calls the exec method for the active command and sets the next one as the new active one.

Errors

If an error occur when executing exec the error is returned and the state is left unchanged.

[src]

Returns a reference to the receiver.

[src]

Consumes the record, returning the receiver.

impl<'a, R, C: Command<R> + ToString> Record<'a, R, C>
[src]

[src]

Returns the string of the command which will be undone in the next call to undo.

[src]

Returns the string of the command which will be redone in the next call to redo.

Trait Implementations

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

[src]

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

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

[src]

Performs the conversion.

impl<'a, R, C: Command<R>> From<R> for Record<'a, R, C>
[src]

[src]

Performs the conversion.

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

[src]

Formats the value using the given formatter. Read more

impl<'a, R, C: Command<R> + Display> Display for Record<'a, R, C>
[src]

[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<'a, R, C> Send for Record<'a, R, C> where
    C: Send,
    R: Send

impl<'a, R, C> Sync for Record<'a, R, C> where
    C: Sync,
    R: Sync