jamsocket / aper

A Rust data structure library built on state machines.
https://aper.dev
MIT License
299 stars 12 forks source link

Transactions #7

Closed paulgb closed 2 years ago

paulgb commented 3 years ago

For built-in data structures, it might sometimes be useful to be able to atomically perform multiple operations. I'd need to give some thought to the best way to do this, but one idea is to have a Transactioned pseudo-data structure that wraps a state machine and accepts a Transaction update. Something like (not tested):

pub struct Transaction<T: Transition>(Vec<T>);

pub struct Transactioned<T: StateMachine>(T);

impl<T: StateMachine> StateMachine for Transactioned<T> {
    type Transition = Transactioned<<T as StateMachine>::Transition>;

    fn apply(&mut self, transition: Self::Transition) {
        for t in transition.0 {
            self.0.apply(t);
        }
    }
}
paulgb commented 2 years ago

The more I think about it, the more I think transactions are the wrong approach. The need for transactions implies that a user intent could result in multiple state transitions, but the correct way to model that is for the user intent to represent one state transition that captures both actions.