[][src]Trait undo::Command

pub trait Command<R>: Debug + Display {
    fn apply(&mut self, receiver: &mut R) -> Result;
fn undo(&mut self, receiver: &mut R) -> Result; fn redo(&mut self, receiver: &mut R) -> Result { ... }
fn merge(&self) -> Merge { ... }
fn is_dead(&self) -> bool { ... } }

Base functionality for all commands.

Required methods

fn apply(&mut self, receiver: &mut R) -> Result

Applies the command on the receiver and returns Ok if everything went fine, and Err if something went wrong.

fn undo(&mut self, receiver: &mut R) -> Result

Restores the state of the receiver as it was before the command was applied and returns Ok if everything went fine, and Err if something went wrong.

Loading content...

Provided methods

fn redo(&mut self, receiver: &mut R) -> Result

Reapplies the command on the receiver and return Ok if everything went fine, and Err if something went wrong.

The default implementation uses the apply implementation.

fn merge(&self) -> Merge

Used for automatic merging of commands.

When commands are merged together, undoing and redoing them are done in one step.

Examples

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

impl Command<String> for Add {
    fn apply(&mut self, s: &mut String) -> undo::Result {
        s.push(self.0);
        Ok(())
    }

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

    fn merge(&self) -> Merge {
        Merge::Yes
    }
}

fn main() -> undo::Result {
    let mut record = Record::default();
    // The `a`, `b`, and `c` commands are merged.
    record.apply(Add('a'))?;
    record.apply(Add('b'))?;
    record.apply(Add('c'))?;
    assert_eq!(record.as_receiver(), "abc");
    // Calling `undo` once will undo all merged commands.
    record.undo().unwrap()?;
    assert_eq!(record.as_receiver(), "");
    // Calling `redo` once will redo all merged commands.
    record.redo().unwrap()?;
    assert_eq!(record.as_receiver(), "abc");
    Ok(())
}

fn is_dead(&self) -> bool

Says if the command is dead.

A dead command will be removed the next time it becomes the current command. This can be used to remove command if for example executing it caused an error, and it needs to be removed.

Loading content...

Implementations on Foreign Types

impl<R, C: Command<R> + ?Sized> Command<R> for Box<C>[src]

Loading content...

Implementors

impl<R> Command<R> for Merged<R>[src]

Loading content...