Crates.io | undo |
lib.rs | undo |
version | 0.51.0 |
source | src |
created_at | 2016-12-12 16:17:10.977203 |
updated_at | 2023-09-30 10:54:53.142456 |
description | An undo-redo library. |
homepage | |
repository | https://github.com/evenorog/undo |
max_upload_size | |
id | 7551 |
size | 102,509 |
An undo-redo library.
An implementation of the command pattern, where all edits are done by creating objects that applies the modifications. All objects knows how to undo the changes it applies, and by using the provided data structures it is easy to undo and redo edits made to a target.
See the documentation and examples for more information.
use undo::{Edit, Record};
struct Add(char);
impl Edit for Add {
type Target = String;
type Output = ();
fn edit(&mut self, target: &mut String) {
target.push(self.0);
}
fn undo(&mut self, target: &mut String) {
self.0 = target.pop().unwrap();
}
}
fn main() {
let mut target = String::new();
let mut record = Record::new();
record.edit(&mut target, Add('a'));
record.edit(&mut target, Add('b'));
record.edit(&mut target, Add('c'));
assert_eq!(target, "abc");
record.undo(&mut target);
record.undo(&mut target);
record.undo(&mut target);
assert_eq!(target, "");
record.redo(&mut target);
record.redo(&mut target);
record.redo(&mut target);
assert_eq!(target, "abc");
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.