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 Command
s 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]
fn new<U: Into<T>>(receiver: U) -> Stack<T, C>
Creates a new Stack
.
fn with_capacity<U: Into<T>>(receiver: U, capacity: usize) -> Stack<T, C>
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(&mut self, cmd: C) -> Result<(), (C, C::Err)>
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
.
fn pop(&mut self) -> Option<Result<C, (C, C::Err)>>
Trait Implementations
impl<T: Clone, C: Clone + Command<T>> Clone for Stack<T, C>
[src]
fn clone(&self) -> Stack<T, C>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: Debug, C: Debug + Command<T>> Debug for Stack<T, C>
[src]
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]
fn eq(&self, __arg_0: &Stack<T, C>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Stack<T, C>) -> bool
This method tests for !=
.
impl<T: Hash, C: Hash + Command<T>> Hash for Stack<T, C>
[src]
fn hash<__HTC: Hasher>(&self, __arg_0: &mut __HTC)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
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]
fn cmp(&self, __arg_0: &Stack<T, C>) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<T: PartialOrd, C: PartialOrd + Command<T>> PartialOrd for Stack<T, C>
[src]
fn partial_cmp(&self, __arg_0: &Stack<T, C>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Stack<T, C>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Stack<T, C>) -> bool
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<T, C>) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Stack<T, C>) -> bool
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]
impl<T, C: Command<T>> AsRef<T> for Stack<T, C>
[src]
fn as_ref(&self) -> &T
Performs the conversion.