[−][src]Trait redo::Command
Base functionality for all commands.
Associated Types
Loading content...Required methods
fn apply(&mut self, target: &mut Self::Target) -> Result<Self>
Applies the command on the target and returns Ok
if everything went fine,
and Err
if something went wrong.
fn undo(&mut self, target: &mut Self::Target) -> Result<Self>
Restores the state of the target as it was before the command was applied
and returns Ok
if everything went fine, and Err
if something went wrong.
Provided methods
fn redo(&mut self, target: &mut Self::Target) -> Result<Self>
Reapplies the command on the target and return Ok
if everything went fine,
and Err
if something went wrong.
The default implementation uses the apply
implementation.
fn merge(&mut self, command: Self) -> Merge<Self> where
Self: Sized,
Self: Sized,
Used for manual merging of two commands.
Examples
struct Add(String); impl Command for Add { type Target = String; type Error = (); fn apply(&mut self, s: &mut String) -> redo::Result<Add> { s.push_str(&self.0); Ok(()) } fn undo(&mut self, s: &mut String) -> redo::Result<Add> { let len = s.len() - self.0.len(); s.truncate(len); Ok(()) } fn merge(&mut self, Add(s): Self) -> Merge<Add> { self.0.push_str(&s); Merge::Yes } } fn main() -> redo::Result<Add> { let mut record = Record::default(); // The `a`, `b`, and `c` commands are merged. record.apply(Add("a".into()))?; record.apply(Add("b".into()))?; record.apply(Add("c".into()))?; assert_eq!(record.as_target(), "abc"); // Calling `undo` once will undo all the merged commands. record.undo().unwrap()?; assert_eq!(record.as_target(), ""); // Calling `redo` once will redo all the merged commands. record.redo().unwrap()?; assert_eq!(record.as_target(), "abc"); Ok(()) }