Struct undo::Checkpoint

source ·
pub struct Checkpoint<'a, T, R> { /* private fields */ }
Expand description

A checkpoint wrapper.

Wraps a Record or History and gives it checkpoint functionality.

Examples

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

impl Command<String> for Add {
    fn apply(&mut self, s: &mut String) -> Result<(), Box<dyn Error + Send + Sync>> {
        s.push(self.0);
        Ok(())
    }

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

fn main() -> undo::Result<String> {
    let mut record = Record::default();
    let mut cp = record.checkpoint();
    cp.apply(Add('a'))?;
    cp.apply(Add('b'))?;
    cp.apply(Add('c'))?;
    assert_eq!(cp.as_receiver(), "abc");
    cp.cancel()?;
    assert_eq!(record.as_receiver(), "");
    Ok(())
}

Implementations§

Calls the apply method.

Calls the undo method.

Calls the redo method.

Calls the go_to method.

Commits the changes and consumes the checkpoint.

Cancels the changes and consumes the checkpoint.

Errors

If an error occur when canceling the changes, 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.

Calls the apply method.

Calls the undo method.

Calls the redo method.

Calls the go_to method.

Commits the changes and consumes the checkpoint.

Cancels the changes and consumes the checkpoint.

Errors

If an error occur when canceling the changes, 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.

Trait Implementations§

Converts this type into a mutable reference of the (usually inferred) input type.
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.
Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more
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

Returns the argument unchanged.

Calls U::from(self).

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

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.