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 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<(), 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]
fn new<T: Into<R>>(receiver: T) -> Stack<R, C>
[src]
Creates a new stack.
fn len(&self) -> usize
[src]
Returns the number of commands in the stack.
fn is_empty(&self) -> bool
[src]
Returns true
if the stack is empty.
fn clear(&mut self)
[src]
Removes all commands from the stack without undoing them.
This resets the stack back to its initial state while leaving the receiver unmodified.
fn push(&mut self, cmd: C) -> Result<(), Error<R, C>>
[src]
Pushes the command 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.
fn pop(&mut self) -> Option<Result<C, Error<R, C>>>
[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.
fn as_receiver(&self) -> &R
[src]
Returns a reference to the receiver
.
fn into_receiver(self) -> R
[src]
Consumes the stack, returning the receiver
.
Trait Implementations
impl<R: Clone, C: Clone + Command<R>> Clone for Stack<R, C>
[src]
fn clone(&self) -> Stack<R, C>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<R: Debug, C: Debug + Command<R>> Debug for Stack<R, C>
[src]
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]
fn eq(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests for !=
.
impl<R: Hash, C: Hash + Command<R>> Hash for Stack<R, C>
[src]
fn hash<__HRC: Hasher>(&self, __arg_0: &mut __HRC)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
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]
fn cmp(&self, __arg_0: &Stack<R, C>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.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]
fn partial_cmp(&self, __arg_0: &Stack<R, C>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Stack<R, C>) -> bool
[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more