High Level:
- I am looking for a way to a db-like "transactions" on Rust structures: i.e.
strat-transaction
... do some work ... without IO / network .. (but includes mutations)
either commit or rollback
- I know about STMs, but there's no concurrency involved here, and performance is important, so I am looking for something lighter weight.
=====
Low level details:
- We have a VM state that looks something like:
pub struct State {
code: Vec<Instr>,
data: Vec<Data>,
alt: Vec<Data>,
variables: HashMap<String, Data>,
function_code: Vec<Instr>,
function_table: Vec<usize>,
}
Now, we have some VM function. The Add function looks something like:
Instr::Add => {
let rhs = data.pop().unwrap();
let lhs = data.pop().unwrap();
match (lhs, rhs) {
(Data::I32(lhs), Data::I32(rhs)) => data.push(Data::I32(lhs + rhs)),
(Data::F32(lhs), Data::F32(rhs)) => data.push(Data::F32(lhs + rhs)),
_ => panic!("+ fail"), } },
Now, what happens during error handling? Lots of bad things:
If we get a F32 and an String, we have already popped both arguments by the time we get to the panic!
In a sense, on an error, I want to be able to "rollback" the two pop()'s, and say: here is the data stack when we tried to execute +, and it failed.
So I want each instruction to be "transactional" in that it either: (1) succeeds or (2) does not modify the "struct State", and reports an error.
The two ways to do this seem to be:
(1) manually get data w/o modifying State, do cmputation; only modify struct is success is guaranteed -- this could get messy on more complicated instrs
(2) get some type of lightweight "transactions" on Rust structs, so we can "rollback" in case of a failure.
Suggestions?