pub struct Transaction<'a> { /* private fields */ }Expand description
Representation of a transaction in STM.
Implementations§
Source§impl<'a> Transaction<'a>
impl<'a> Transaction<'a>
Sourcepub fn on_commit<F>(&mut self, f: F)
pub fn on_commit<F>(&mut self, f: F)
Schedule a side-effect (I/O) to run ONLY if the transaction commits successfully.
This closure will run after all locks are released.
Sourcepub fn on_rollback<F>(&mut self, f: F)
pub fn on_rollback<F>(&mut self, f: F)
Schedule a side-effect to run ONLY if the transaction aborts or fails.
This is useful for cleaning up temporary resources or logging failures. It runs when the transaction is dropped without committing.
§Example
let tmp_file = create_temp_file();
tx.on_rollback(move || remove_file(tmp_file));Sourcepub fn load<T: Any + Send + Sync + Clone>(
&mut self,
tvar: &TVar<T>,
) -> Result<T, StmError>
pub fn load<T: Any + Send + Sync + Clone>( &mut self, tvar: &TVar<T>, ) -> Result<T, StmError>
Read a TVar.
The TVar wrapper contains an Arc<TVarInner<T>>. This method clones the Arc
into the read entry (_keep_alive), so the version_lock pointer stored in
ReadEntry.lock_atomic remains valid even if the TVar handle is dropped before
commit() runs. The stable identity key is Arc::as_ptr(&tvar.0) - the address
of the heap-allocated TVarInner, which is invariant across TVar clones.
Sourcepub fn store<T: Any + Send + Sync + 'static>(
&mut self,
tvar: &TVar<T>,
val: T,
) -> Result<(), StmError>
pub fn store<T: Any + Send + Sync + 'static>( &mut self, tvar: &TVar<T>, val: T, ) -> Result<(), StmError>
Write to a TVar.
This method clones the Arc<TVarInner<T>> from the TVar wrapper and moves it
into the committer closure. The committer is invoked during commit(), which
may run after the TVar handle is dropped. The Arc clone ensures the
TVarInner (and thus version_lock and data) remains alive until the
commit closure executes.