Struct redo::Record

source ·
pub struct Record<R, C: Command<R>> { /* private fields */ }
Expand description

A record of commands.

The record 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 signal. The user can give the record a function that is called each time the state changes by using the builder.

Examples

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

impl Command<String> for Add {
    type Error = Box<dyn error::Error>;

    fn apply(&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("`s` is empty")?;
        Ok(())
    }
}

fn main() -> redo::Result<String, Add> {
    let mut record = Record::default();
    record.apply(Add('a'))?;
    record.apply(Add('b'))?;
    record.apply(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.as_receiver(), "abc");
    Ok(())
}

Implementations

Returns a new record.

Returns a builder for a record.

Reserves capacity for at least additional more commands.

Panics

Panics if the new capacity overflows usize.

Returns the capacity of the record.

Returns the number of commands in the record.

Returns true if the record is empty.

Returns the limit of the record.

Sets the limit of the record and returns the new limit.

If this limit is reached it will start popping of commands at the beginning of the record when new commands are applied. No limit is set by default which means it may grow indefinitely.

If 0 < limit < len the first commands will be removed until len == limit. However, if the current active command is going to be removed, the limit is instead adjusted to len - active so the active command is not removed.

Panics

Panics if limit is 0.

Sets how the signal should be handled when the state changes.

Returns true if the record can undo.

Returns true if the record can redo.

Marks the receiver as currently being in a saved or unsaved state.

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

Revert the changes done to the receiver since the saved state.

Returns the position of the current command.

Removes all commands from the record without undoing them.

Pushes the command on top of the record and executes its apply method.

Errors

If an error occur when executing apply the error is returned together with the command.

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 together with the command.

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

Errors

If an error occur when applying redo the error is returned together with the command.

Repeatedly calls undo or redo until the command at cursor is reached.

Errors

If an error occur when executing undo or redo the error is returned together with the command.

Returns a checkpoint.

Returns a queue.

Returns a reference to the receiver.

Returns a mutable reference to the receiver.

This method should only be used when doing changes that should not be able to be undone.

Consumes the record, returning the receiver.

Returns an iterator over the commands in the record.

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

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

Returns a structure for configurable formatting of the record.

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.