# TQueue, TMap, TSemaphore — Transactional Collections
`id_effect_fp` builds three collections on top of `TRef`. Their operations return `Stm` values, so
they compose with `TRef` operations in the same transaction. Each constructor is a transaction
too: commit it once and share the handle (all three are `Clone + Send + Sync`).
| `TQueue<A>` | `TQueue::bounded(n)`, `TQueue::unbounded()` | `offer(a) -> Stm<bool, E>` (`false` when full), `take() -> Stm<A, E>` (retries while empty) |
| `TMap<K, V>` | `TMap::make()` | `get(&k) -> Stm<Option<V>, E>`, `set(k, v)`, `delete(&k)` |
| `TSemaphore` | `TSemaphore::make(n)` | `acquire()` (retries while no permit), `release()` |
## 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,ignore
use id_effect_fp::{Stm, TQueue, commit};
fn offer_blocking(queue: &TQueue<u32>, job: u32) -> Stm<(), ()> {
queue.offer(job).flat_map(Stm::<(), ()>::check) // retry while full
}
let queue = run_blocking(commit(TQueue::<u32>::bounded(2)), ()).expect("queue");
let producer = {
let queue = queue.clone();
run_blocking(commit(offer_blocking(&queue, job)), ()).expect("offer");
}
})
};
let sum: u32 = (0..10)
```rust,ignore
let sem = run_blocking(commit(TSemaphore::make(1)), ()).expect("sem");
run_blocking(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_blocking(commit(try_acquire.clone()), ()), Ok(false));
run_blocking(commit(sem.release::<()>()), ())?;
assert_eq!(run_blocking(commit(try_acquire), ()), Ok(true));
```
Because `acquire` and `release` are `Stm`s, you can combine taking a permit with other state
changes in one transaction. To bound concurrent *effects* (permits held across I/O), use core
`id_effect::Semaphore` instead; STM transactions must not span I/O.
Full example: [`crates/id_effect_fp/examples/086_stm_collections.rs`](https://github.com/Industrial/id_effect/blob/main/crates/id_effect_fp/examples/086_stm_collections.rs) (`cargo run -p id_effect_fp --example 086_stm_collections`); minimal per-type examples are `080_stm_tmap`, `081_stm_tqueue`, `082_stm_tsemaphore`.
## Summary
| `TRef<T>` | Single mutable value |
| `TQueue<T>` | FIFO queue; `take` waits while empty |
| `TMap<K, V>` | Transactional hash map |
| `TSemaphore` | Transactional permit counter |
All compose with `flat_map` and commit atomically with other STM operations.