zwire (Rust Facade)
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.
Feature Flags & Modular Workspace
Section titled “Feature Flags & Modular Workspace”Use the umbrella zwire crate with granular feature flags, or depend directly on any standalone crate:
| Crate | Feature | Standalone? | What It Provides |
|---|---|---|---|
zwire-collections |
collections |
✅ | Exact 2-level Chunked B-Tree + O(1) Top-K boundary pruning (zero T: Clone) |
zwire-core |
core, sync |
✅ | Zero-global Scope + Copy handles + deadlock-free SyncScope (Send + Sync) |
zwire-tx |
tx |
core |
OptimisticLayer, RAII OptimisticGuard, atomic zero-flicker try_settle, STM Draft |
zwire-protocol |
protocol |
core |
Zero-dep JSON WireValue, out-of-order hydrate/dehydrate, ExternalSource, SyncBridge, InspectorHook |
# 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 protocolzwire = "0.1"Quick Start
Section titled “Quick Start”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 guardlet balance = scope.atom(1_000i64);let mut l1 = OptimisticLayer::new(&scope);l1.update(balance, |b| b - 200); // UI: 800
// Authoritative server push arrives concurrently -> active intents auto-rebase!scope.set(balance, 1_500);assert_eq!(scope.get(balance), 1_300);
// Atomic zero-flicker settlel1.settle_with(|| scope.set(balance, 1_300));assert_eq!(scope.get(balance), 1_300);Running the Example & Tests
Section titled “Running the Example & Tests”cd cratescargo test --workspace --all-targets --all-featurescargo test --workspace --doccargo check -p zwire --target wasm32-unknown-unknown --no-default-features --features "core tx collections protocol"cargo run -p zwire --example orderbook_optimisticSee docs/RUST-1.100-GUIDELINES.md for Rust 1.100 patterns, Clippy configuration, and invariants.
Ownership, notifications and async transactions
Section titled “Ownership, notifications and async transactions”scope.owner()creates a lexical owner.owner.atom(...),owner.computed(...), andowner.track(handle)bind nodes to its lifetime. A nestedowner.child()can release its own graph earlier. Both local and thread-safe scopes support owners.scope.changed(signal).awaitobserves the next change.scope.changes(signal)subscribes once and supports repeatedchanged().awaitorpoll_next(cx). Writes coalesce until the consumer polls. Items containRc<T>locally orArc<T>forSyncScope. Disposed signals end the stream. With an existing futures dependency, wrap it asfutures_util::stream::poll_fn(move |cx| changes.poll_next(cx)).run_optimistic(&scope, apply, request, commit).awaitandrun_sync_optimisticbind an optimistic layer to a request’s lifetime. Cancellation and request failure revert its intents. The sync commit callback stages server values in aSyncSettlement.- Implement
zwire::optics::Lens<S, A>to read and mutate a field and expose it as a subscribable computed. - Enable the
deriveCargo feature to deriveWireCodecon 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.