Struct redo::Checkpoint

source ·
pub struct Checkpoint<'a, T, C> { /* 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 {
    type Error = &'static str;

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

Returns a checkpoint.

Reserves capacity for at least additional more commands in the checkpoint.

Panics

Panics if the new capacity overflows usize.

Returns the capacity of the checkpoint.

Returns the number of commands in the checkpoint.

Returns true if the checkpoint is empty.

Commits the changes and consumes the checkpoint.

Calls the apply method.

Calls the undo method.

Calls the redo method.

Calls the go_to method.

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.

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.