1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! # 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:
//!
//! ```rust
//! 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`:
//!
//! ```rust
//! 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));
//! ```
//!
pub