# `zwire`
**Zero-dependency, `#![forbid(unsafe_code)]`, `#![no_std]` (`alloc`), Rust 1.100 (Edition 2024) transactional reactive state engine for Rust** — combining a scoped push-pull arena (`Copy` handles + `SyncScope`), **rebaseable optimistic mutation layers + STM read-verify drafts**, **exact incremental 2-level Chunked B-Tree Top-K collections**, and **open JSON wire protocols** compatible with [`@zwire/core`](https://github.com/kjmin-dev/zwire#readme).
## Feature Flags & Modular Workspace
Use the umbrella `zwire` crate with granular feature flags, or depend directly on any standalone crate:
| [`zwire-collections`](https://github.com/kjmin-dev/zwire/blob/main/rust/crates/zwire-collections/README.md) | `collections` | ✅ | Exact 2-level Chunked B-Tree + `O(1)` Top-K boundary pruning (zero `T: Clone`) |
| [`zwire-core`](https://github.com/kjmin-dev/zwire/blob/main/rust/crates/zwire-core/README.md) | `core`, `sync` | ✅ | Zero-global `Scope` + `Copy` handles + deadlock-free `SyncScope` (`Send + Sync`) |
| [`zwire-tx`](https://github.com/kjmin-dev/zwire/blob/main/rust/crates/zwire-tx/README.md) | `tx` | `core` | `OptimisticLayer`, RAII `OptimisticGuard`, atomic zero-flicker `try_settle`, STM `Draft` |
| [`zwire-protocol`](https://github.com/kjmin-dev/zwire/blob/main/rust/crates/zwire-protocol/README.md) | `protocol` | `core` | Zero-dep JSON `WireValue`, out-of-order `hydrate`/`dehydrate`, `ExternalSource`, `SyncBridge`, `InspectorHook` |
```toml
# Only exact incremental Top-K collections (no_std + alloc, zero deps)
zwire = { version = "0.1", default-features = false, features = ["collections"] }
# Full transactional reactive engine + multi-threaded SyncScope + @zwire/core wire protocol
zwire = "0.1"
```
## Quick Start
```rust
use zwire::prelude::*;
let scope = Scope::new();
// 1. Reactive incremental Top-K collection (O(log N) updates, O(1) boundary skip)
let book = ReactiveCollection::with_filter(
&scope,
|o: &(u64, i64, bool)| o.0,
|a, b| b.1.cmp(&a.1),
|o| o.2,
);
let top3 = book.top_k_signal(3);
let best_price = scope.computed(move |cx| {
cx.with(top3, |rows| rows.first().map(|r| r.1).unwrap_or(0))
});
book.upsert_many([(1, 100, true), (2, 120, true), (3, 95, true)]);
assert_eq!(scope.get(best_price), 120);
// 2. Overlapping rebaseable optimistic layers + RAII auto-revert guard
let balance = scope.atom(1_000i64);
let mut l1 = OptimisticLayer::new(&scope);
// Authoritative server push arrives concurrently -> active intents auto-rebase!
scope.set(balance, 1_500);
assert_eq!(scope.get(balance), 1_300);
// Atomic zero-flicker settle
l1.settle_with(|| scope.set(balance, 1_300));
assert_eq!(scope.get(balance), 1_300);
```
## Running the Example & Tests
```bash
cd crates
cargo test --workspace --all-targets --all-features
cargo test --workspace --doc
cargo check -p zwire --target wasm32-unknown-unknown --no-default-features --features "core tx collections protocol"
cargo run -p zwire --example orderbook_optimistic
```
See [`docs/RUST-1.100-GUIDELINES.md`](https://github.com/kjmin-dev/zwire/blob/main/docs/RUST-1.100-GUIDELINES.md) for Rust 1.100 patterns, Clippy configuration, and invariants.
## Ownership, notifications and async transactions
- `scope.owner()` creates a lexical owner. `owner.atom(...)`, `owner.computed(...)`, and `owner.track(handle)` bind nodes to its lifetime. A nested `owner.child()` can release its own graph earlier. Both local and thread-safe scopes support owners.
- `scope.changed(signal).await` observes the next change. `scope.changes(signal)` subscribes once and supports repeated `changed().await` or `poll_next(cx)`. Writes coalesce until the consumer polls. Items contain `Rc<T>` locally or `Arc<T>` for `SyncScope`. Disposed signals end the stream. With an existing futures dependency, wrap it as `futures_util::stream::poll_fn(move |cx| changes.poll_next(cx))`.
- `run_optimistic(&scope, apply, request, commit).await` and `run_sync_optimistic` bind an optimistic layer to a request's lifetime. Cancellation and request failure revert its intents. The sync commit callback stages server values in a `SyncSettlement`.
- Implement `zwire::optics::Lens<S, A>` to read and mutate a field and expose it as a subscribable computed.
- Enable the `derive` Cargo feature to derive `WireCodec` on concrete named-field structs. With the facade, put `#[wire(crate = zwire::protocol)]` under `#[derive(WireCodec)]`. Nesting, nullable fields and safe-integer string encoding follow each field's codec.