Struct redo::Stack [] [src]

pub struct Stack<R, C: Command<R>> { /* fields omitted */ }

The command stack.

The stack is the simplest data structure and works by pushing and popping off commands that modifies the receiver. Unlike the record, it does not have a special state that can be used for callbacks.

Examples

use redo::{Command, Error, Stack};

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

impl Command<String> for Add {
    type Err = &'static str;

    fn exec(&mut self, s: &mut String) -> Result<(), &'static str> {
        s.push(self.0);
        Ok(())
    }

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

fn foo() -> Result<(), Error<String, Add>> {
    let mut stack = Stack::default();

    stack.push(Add('a'))?;
    stack.push(Add('b'))?;
    stack.push(Add('c'))?;

    assert_eq!(stack.as_receiver(), "abc");

    let c = stack.pop().unwrap()?;
    let b = stack.pop().unwrap()?;
    let a = stack.pop().unwrap()?;

    assert_eq!(stack.as_receiver(), "");

    stack.push(a)?;
    stack.push(b)?;
    stack.push(c)?;

    assert_eq!(stack.into_receiver(), "abc");

    Ok(())
}

Methods

impl<R, C: Command<R>> Stack<R, C>
[src]

[src]

Creates a new stack.

[src]

Returns the number of commands in the stack.

[src]

Returns true if the stack is empty.

[src]

Removes all commands from the stack without undoing them.

This resets the stack back to its initial state while leaving the receiver unmodified.

[src]

Pushes the command on the stack and executes its exec method. The command is merged with the previous top command if merge does not return None.

Errors

If an error occur when executing redo or merging commands, the error is returned together with the command.

[src]

Calls the top commands undo method and pops it off the stack. Returns None if the stack is empty.

Errors

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

[src]

Returns a reference to the receiver.

[src]

Consumes the stack, returning the receiver.

Trait Implementations

impl<R: Clone, C: Clone + Command<R>> Clone for Stack<R, C>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<R: Debug, C: Debug + Command<R>> Debug for Stack<R, C>
[src]

[src]

Formats the value using the given formatter. Read more

impl<R: Eq, C: Eq + Command<R>> Eq for Stack<R, C>
[src]

impl<R: PartialEq, C: PartialEq + Command<R>> PartialEq for Stack<R, C>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<R: Hash, C: Hash + Command<R>> Hash for Stack<R, C>
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<R: Ord, C: Ord + Command<R>> Ord for Stack<R, C>
[src]

[src]

This method returns an Ordering between self and other. Read more

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<R: PartialOrd, C: PartialOrd + Command<R>> PartialOrd for Stack<R, C>
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

[src]

This method tests less than (for self and other) and is used by the < operator. Read more

[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<R: Default, C: Command<R>> Default for Stack<R, C>
[src]

[src]

Returns the "default value" for a type. Read more

impl<R, C: Command<R>> AsRef<R> for Stack<R, C>
[src]

[src]

Performs the conversion.

impl<R, C: Command<R>> From<R> for Stack<R, C>
[src]

[src]

Performs the conversion.

impl<R, C: Command<R> + Display> Display for Stack<R, C>
[src]

[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<R, C> Send for Stack<R, C> where
    C: Send,
    R: Send

impl<R, C> Sync for Stack<R, C> where
    C: Sync,
    R: Sync