Struct undo::Stack [] [src]

pub struct Stack<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 std::error::Error;
use undo::{Command, Stack};

struct Add(char);

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

    fn undo(&mut self, s: &mut String) -> Result<(), Box<Error>> {
        self.0 = s.pop().expect("`String` is unexpectedly empty");
        Ok(())
    }
}

fn foo() -> Result<(), (Box<Command<String>>, Box<Error>)> {
    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> Stack<T>
[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 their id is equal.

Errors

If an error occur when executing redo, 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: Debug> Debug for Stack<T>
[src]

Formats the value using the given formatter.

impl<T: Default> Default for Stack<T>
[src]

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

impl<T> AsRef<T> for Stack<T>
[src]

Performs the conversion.