Struct undo::History

source ·
pub struct History<R> { /* private fields */ }
Expand description

A history of commands.

A history works like the Record but also provides branching, like vim’s undo-tree.

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 history = History::default();
    history.apply(Add('a'))?;
    history.apply(Add('b'))?;
    history.apply(Add('c'))?;
    assert_eq!(history.as_receiver(), "abc");
    let root = history.root();
    history.go_to(root, 1).unwrap()?;
    assert_eq!(history.as_receiver(), "a");
    let abc = history.root();
    history.apply(Add('f'))?;
    history.apply(Add('g'))?;
    assert_eq!(history.as_receiver(), "afg");
    history.go_to(abc, 3).unwrap()?;
    assert_eq!(history.as_receiver(), "abc");
    Ok(())
}

Implementations§

Returns a new history.

Returns a builder for a history.

Reserves capacity for at least additional more commands.

Panics

Panics if the new capacity overflows usize.

Returns the capacity of the history.

Returns the number of commands in the current branch of the history.

Returns true if the current branch of the history is empty.

Returns the limit of the history.

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

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

If 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 history can undo.

Returns true if the history 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 current branch.

Returns the position of the current command.

Removes all commands from the history without undoing them.

Pushes the command to the top of the history 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 executing redo the error is returned together with the command.

Repeatedly calls undo or redo until the command in branch 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 history, returning the receiver.

Returns an iterator over the commands in the current branch.

Returns an iterator over the branches in the history, excluding the root branch.

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
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.

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.