101 unstable releases

0.51.0 Sep 30, 2023
0.49.0 Mar 26, 2023
0.48.0 Dec 19, 2022
0.47.2 Jun 18, 2022
0.2.4 Dec 26, 2016

#186 in Data structures

Download history 18/week @ 2023-12-15 37/week @ 2023-12-22 45/week @ 2023-12-29 39/week @ 2024-01-05 61/week @ 2024-01-12 34/week @ 2024-01-19 10/week @ 2024-01-26 1/week @ 2024-02-02 46/week @ 2024-02-09 52/week @ 2024-02-16 47/week @ 2024-02-23 151/week @ 2024-03-01 57/week @ 2024-03-08 106/week @ 2024-03-15 32/week @ 2024-03-22 1197/week @ 2024-03-29

1,400 downloads per month
Used in 2 crates

MIT/Apache

68KB
1.5K SLoC

undo

An undo-redo library.

Rust Crates.io Docs

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.

Examples

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");
}

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~0–8.5MB
~54K SLoC