# Transactional Collections
`id_effect::stm` builds eight collections on top of `TRef`. Their operations return `Stm` values,
so they compose with `TRef` operations and with each other in one transaction, and they work
with `Stm::retry` (an operation that has to wait parks the fiber until a cell it read changes)
and `Stm::or_else` (run an alternative when the first branch would wait). Each constructor is a
transaction too: commit it once and share the handle (every type is `Clone + Send + Sync`).
| `TRef<A>` | `TRef::make(a)` | `read_stm`, `write_stm`, `update_stm`, `modify_stm` | never |
| `TQueue<A>` | `TQueue::bounded(n)`, `TQueue::unbounded()` | `offer(a) -> bool` (`false` when full), `take()` | `take` on an empty queue |
| `TMap<K, V>` | `TMap::make()` | `get(&k) -> Option<V>`, `set(k, v)`, `delete(&k)` | never |
| `TSemaphore` | `TSemaphore::make(n)` | `acquire()`, `release()` | `acquire` with no permit left |
| `TSet<A>` | `TSet::make()`, `TSet::from_values(it)` | `add(a) -> bool`, `remove(&a) -> bool`, `contains`, `size`, `is_empty`, `to_vec`, `take_any()` | `take_any` on an empty set |
| `TArray<A>` | `TArray::make(vec)` | `get(i) -> Option<A>`, `set(i, a) -> bool`, `update(i, f) -> bool`, `to_vec`, `fold`, `len`, `cell(i)` | never |
| `TPriorityQueue<A: Ord>` | `TPriorityQueue::empty()`, `from_values(it)` | `offer`, `offer_all`, `take()` (smallest first), `take_option`, `take_all`, `peek()`, `size` | `take` / `peek` on an empty queue |
| `TPromise<A, E>` | `TPromise::make()` | `await_() -> Stm<A, E>`, `succeed(a) -> bool`, `fail(e) -> bool`, `complete(r)`, `poll`, `is_done` | `await_` while pending |
| `TReentrantLock` | `TReentrantLock::make()` | `acquire_read`, `release_read`, `acquire_write`, `release_write`, `read_locks`, `write_locks`, `is_locked` | an acquire another fiber blocks |
The examples below share this helper, which commits a transaction on the global runtime:
```rust
# use id_effect::{Cause, Effect, Exit};
fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
match eff.block_on() {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(_) => panic!("effect died or was interrupted"),
}
}
```
## TQueue: producer / consumer
`take` waits (retries) while the queue is empty. `offer` on a full bounded queue does *not* wait:
it returns `false`. Add `Stm::check` to make a waiting offer:
```rust
use id_effect::stm::{Stm, TQueue, commit};
# use id_effect::{Cause, Effect, Exit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
fn offer_blocking(queue: &TQueue<u32>, job: u32) -> Stm<(), ()> {
queue.offer(job).flat_map(Stm::<(), ()>::check) // retry while full
}
let queue = run(commit(TQueue::<u32>::bounded(2))).expect("queue");
let producer = {
let queue = queue.clone();
run(commit(offer_blocking(&queue, job))).expect("offer");
}
})
};
let sum: u32 = (0..10)
## TSemaphore: transactional permits
```rust
# use id_effect::{Cause, Effect, Exit};
# use id_effect::stm::{Stm, TSemaphore, commit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
let sem = run(commit(TSemaphore::make(1))).expect("sem");
run(commit(sem.acquire::<()>()))?; // takes the only permit
// Non-blocking try: `or_else` runs when `acquire` would retry.
let try_acquire = sem.acquire::<()>().map(|()| true).or_else(Stm::succeed(false));
assert_eq!(run(commit(try_acquire.clone())), Ok(false));
run(commit(sem.release::<()>()))?;
assert_eq!(run(commit(try_acquire)), Ok(true));
# Ok::<(), ()>(())
```
## TArray: one cell per element
A `TArray` has a fixed length and one `TRef` per element, so transactions that touch different
indices never conflict, and a waiting transaction wakes only when an element it read changes.
Out-of-range indices are not failures: `get` gives `None`, `set` and `update` give `false`.
```rust
# use id_effect::{Cause, Effect, Exit};
# use id_effect::stm::{Stm, TArray, commit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
let balances = run(commit(TArray::make(vec![100_i64, 100]))).expect("array");
let transfer = |from: usize, to: usize, amount: i64| {
let b = balances.clone();
balances.get::<&str>(from).flat_map(move |bal| match bal {
Some(bal) if bal >= amount => {
let b2 = b.clone();
b.update(from, move |n| n - amount)
.flat_map(move |_| b2.update(to, move |n| n + amount))
.map(|_| ())
}
Some(_) => Stm::fail("insufficient funds"),
None => Stm::fail("no such account"),
})
};
assert_eq!(run(commit(transfer(0, 1, 30))), Ok(()));
assert_eq!(run(commit(transfer(0, 1, 1_000))), Err("insufficient funds"));
assert_eq!(run(commit(balances.to_vec::<()>())), Ok(vec![70, 130]));
```
## TPriorityQueue: smallest first
`take` removes the smallest element by `Ord` and waits while the queue is empty; wrap elements in
`std::cmp::Reverse` for largest-first. `take_option` never waits.
```rust
# use id_effect::{Cause, Effect, Exit};
# use id_effect::stm::{TPriorityQueue, commit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
let jobs = run(commit(TPriorityQueue::<(u8, &str)>::empty())).expect("queue");
let worker = {
let jobs = jobs.clone();
std::thread::spawn(move || run(commit(jobs.take::<()>())).expect("take")) // waits
};
run(commit(jobs.offer_all::<()>(vec![(2, "report"), (0, "page on-call"), (1, "rotate logs")])))?;
assert_eq!(worker.join().expect("join"), (0, "page on-call"));
assert_eq!(run(commit(jobs.take_all::<()>())), Ok(vec![(1, "rotate logs"), (2, "report")]));
assert_eq!(run(commit(jobs.take_option::<()>())), Ok(None));
# Ok::<(), ()>(())
```
## TPromise: complete once, await many
`await_` waits until the promise is completed, then yields the value or fails the transaction
with the promise's error. The first `succeed` / `fail` wins; later ones return `false`.
```rust
# use id_effect::{Cause, Effect, Exit};
# use id_effect::stm::{Stm, TPromise, commit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
let config = run(commit(TPromise::<String, String>::make())).expect("promise");
let reader = {
let config = config.clone();
std::thread::spawn(move || run(commit(config.await_())).expect("await")) // waits
};
assert_eq!(run(commit(config.succeed::<()>("db=primary".into()))), Ok(true));
assert_eq!(run(commit(config.succeed::<()>("db=replica".into()))), Ok(false));
assert_eq!(reader.join().expect("join"), "db=primary");
// `or_else` gives a default instead of waiting on a pending promise.
let pending = run(commit(TPromise::<String, String>::make())).expect("promise");
let value = pending.await_().or_else(Stm::succeed("defaults".to_string()));
assert_eq!(run(commit(value)), Ok("defaults".to_string()));
```
## TReentrantLock: read/write lock owned by fibers
The lock owner is the fiber running `commit`. Any number of fibers may hold read locks while no
other fiber writes; one fiber at a time holds the write lock, and only while no other fiber reads.
Both locks are reentrant: a fiber may take them again (each acquire needs a matching release),
the writer may also read, and the only reader may upgrade to the write lock. Acquire and release
in one effect chain so both run on the same fiber:
```rust
# use id_effect::{Cause, Effect, Exit};
# use id_effect::stm::{Stm, TReentrantLock, commit};
# fn run<A: Send + 'static, E: Send + 'static>(eff: Effect<A, E>) -> Result<A, E> {
# match eff.block_on() {
# Exit::Success(a) => Ok(a),
# Exit::Failure(Cause::Fail(e)) => Err(e),
# Exit::Failure(_) => panic!("effect died or was interrupted"),
# }
# }
let lock = run(commit(TReentrantLock::make())).expect("lock");
let (l1, l2, l3) = (lock.clone(), lock.clone(), lock.clone());
let critical = commit(l1.acquire_write::<()>())
.flat_map(move |_| commit(l2.acquire_write::<()>())) // reentrant: 2 holds
.flat_map(move |held| {
let l = l3.clone();
commit(l3.release_write::<()>())
.flat_map(move |_| commit(l.release_write::<()>()))
.map(move |_| held)
});
assert_eq!(run(critical), Ok(2));
// A try-lock: `or_else` runs when the acquire would wait.