Crate cset

source ·
Expand description

Fine-grained and reversible struct transactions.

This crate offers a Track macro that structs can derive to generate the plumbing needed to precisely track changes to fields. Calling edit() returns a draft that stores edits separately from the underyling struct, such that no values are written.

When apply() is called on the draft, edits are applied to the base struct. Each replaced value is returned to the caller as a Change in a ChangeSet. This changeset can then be re-applied to a struct of the same type, which replaces fields with values from the ChangeSet. This operation produces a new ChangeSet, allowing for the implementation of an undo-redo paradigm.

Example

use cset::Track;

#[derive(Track)]
struct Foo {
    x: usize,
    #[track(flatten)]
    bar: Bar,
}

#[derive(Track)]
struct Bar {
    y: usize,
}

let mut foo = Foo::new(10, Bar::new(42));

// Enter the non-destructive editing mode
let mut foo_draft = foo.edit();
foo_draft.set_x(42);
foo_draft.edit_bar().set_y(1024);

// Drop the draft to rollback, or apply the changes with `.apply()`
let undo_changeset = foo_draft.apply();
assert_eq!(foo, Foo::new(42, Bar::new(1024)));

let redo_changeset = foo.apply(undo_changeset);
assert_eq!(foo, Foo::new(10, Bar::new(42)));

foo.apply(redo_changeset);
assert_eq!(foo, Foo::new(42, Bar::new(1024)));

Structs

Enums

Derive Macros