Skip to main content

Module reversible

Module reversible 

Source
Expand description

Reversible computing primitives for undo.

Each mutation knows its own inverse. This complements the command-pattern undo system in crate::undo with operation-level reversibility that composes algebraically.

§Core Idea

A Reversible operation can be run forward or backward:

use ftui_runtime::reversible::{Reversible, AddOp, Sequence};

let mut x = 10u64;
let op = AddOp::new(3);

op.forward(&mut x);
assert_eq!(x, 13);

op.backward(&mut x);
assert_eq!(x, 10);

Operations compose into sequences that undo in reverse order:

use ftui_runtime::reversible::{Reversible, AddOp, Sequence};

let mut x = 0u64;
let ops = Sequence::new(vec![
    Box::new(AddOp::new(10)),
    Box::new(AddOp::new(20)),
]);

ops.forward(&mut x);
assert_eq!(x, 30);

ops.backward(&mut x);
assert_eq!(x, 0);

Structs§

AddOp
Reversible addition: state += delta / state -= delta.
InsertOp
Reversible insert at index.
Journal
A journal that records operations as they are applied, enabling undo.
MulOp
Reversible multiplication: state *= factor / state /= factor.
PushOp
Reversible push to a Vec.
RemoveOp
Reversible remove at index (captures removed value for undo).
Sequence
A sequence of reversible operations applied in order.
SetOp
Reversible field set: captures old value for undo.
SwapOp
Reversible swap of two elements in a slice.
XorOp
Reversible XOR: state ^= mask (self-inverse).

Traits§

Reversible
A reversible operation on state S.

Functions§

remove_capturing
Create a RemoveOp that captures the value at index for undo.