Crate transactional

Source
Expand description

§Transactional

A lightweight, lock-free library for managing transactional state in Rust.

§Overview

This library provides a way to manage state changes in a transactional manner, allowing for commit or rollback operations.

§Features

  • For synchronous, non-concurrent use
  • Lightweight, clone-free, lock-free, and extremely fast
  • Simple API for managing transactional state

§Examples

Basic usage:

use transactional::prelude::*;

// Create a new transactional state
let mut state = State::new(42);

// Start a transaction and commit unconditionally
let transaction = state.transact(Some(100)).commit();

assert_eq!(state.get(), Some(&100));

Using validation with commit_if_ok:

use transactional::prelude::*;

let mut state = State::new(10);

// Define a validation function
let validate = |prev: Option<&i32>, curr: Option<&i32>| -> Result<(), UserError> {
    match (prev, curr) {
        (Some(p), Some(c)) if *c > *p => Ok(()),
        _ => Err("Value must increase".into()),
    }
};

// This will succeed because 20 > 10
let result = state.transact(Some(20)).commit_if_ok(validate);
assert!(result.is_ok());

// This would fail because 5 < 20
let result = state.transact(Some(5)).commit_if_ok(validate);
assert!(result.is_err());

// This will bypass checks
state.transact(Some(3)).commit();
assert_eq!(state.get(), Some(&3));

Modules§

error
prelude
state
transaction