Struct redo::Stack [] [src]

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

A stack of commands.

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, Stack};

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

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

    fn redo(&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<(), (Add, &'static str)> {
    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<T, C: Command<T>> Stack<T, C>
[src]

Creates a new Stack.

Creates a new Stack with the given capacity.

Returns the capacity of the Stack.

Returns the number of Commands in the Stack.

Returns true if the Stack is empty.

Returns a reference to the receiver.

Consumes the Stack, returning the receiver.

Pushes cmd on the stack and executes its redo 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.

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.

Trait Implementations

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Formats the value using the given formatter.

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

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

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

This method tests for !=.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Performs the conversion.