An undo/redo library with static dispatch and manual command merging.
It uses the [Command Pattern] where the user modifies a receiver by
applying Command
s on it.
The library has currently two data structures that can be used to modify the receiver:
Stack
that pushes and pops commands to modify the receiver.Record
that can roll the state of the receiver forwards and backwards.```rust use redo::{Command, Stack};
struct Push(char);
impl Command
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<(), (Push, &'static str)> { let mut stack = Stack::default();
stack.push(Push('a'))?;
stack.push(Push('b'))?;
stack.push(Push('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.into_receiver(), "");
stack.push(a)?;
stack.push(b)?;
stack.push(c)?;
assert_eq!(stack.into_receiver(), "abc");
Ok(())
} ```