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 Command
s 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]
fn new<U: Into<T>>(receiver: U) -> Stack<T>
Creates a new Stack
.
fn with_capacity<U: Into<T>>(receiver: U, capacity: usize) -> Stack<T>
Creates a new Stack
with the given capacity
.
fn capacity(&self) -> usize
Returns the capacity of the Stack
.
fn len(&self) -> usize
Returns the number of Command
s in the Stack
.
fn is_empty(&self) -> bool
Returns true
if the Stack
is empty.
fn as_receiver(&self) -> &T
Returns a reference to the receiver
.
fn into_receiver(self) -> T
Consumes the Stack
, returning the receiver
.
fn push<C>(&mut self, cmd: C) -> Result<(), (Box<Command<T>>, Box<Error>)> where
C: Command<T> + 'static,
T: 'static,
C: Command<T> + 'static,
T: 'static,
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
.
fn pop(
&mut self
) -> Option<Result<Box<Command<T>>, (Box<Command<T>>, Box<Error>)>>
&mut self
) -> Option<Result<Box<Command<T>>, (Box<Command<T>>, Box<Error>)>>
Trait Implementations
impl<T: Debug> Debug for Stack<T>
[src]
impl<T: Default> Default for Stack<T>
[src]
impl<T> AsRef<T> for Stack<T>
[src]
fn as_ref(&self) -> &T
Performs the conversion.