zwire 0.3.0

Zero-dependency, no_std-compatible transactional reactive state engine: scoped push-pull arena, rebaseable optimistic layers + STM drafts, exact incremental Top-K collections, and open @zwire/core wire protocols
Documentation
//! Killer Rust demo: real-time orderbook with 10,000 orders, O(1)-pruned reactive Top-5 bids,
//! overlapping optimistic user balance/position intents auto-rebasing against concurrent exchange
//! fills, surgical cancellation (`revert`), and atomic zero-flicker settlement (`settle`).

use core::cell::Cell;
use std::rc::Rc;

use zwire::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq)]
struct Bid {
    id: u64,
    price_cents: i64,
    qty: u32,
    active: bool,
}

fn main() {
    let scope = Scope::new();

    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
    let bids = ReactiveCollection::with_filter(
        &scope,
        |b: &Bid| b.id,
        |a, b| b.price_cents.cmp(&a.price_cents),
        |b| b.active && b.qty > 0,
    );

    bids.upsert_many((1..=10_000u64).map(|id| Bid {
        id,
        price_cents: i64::try_from(id).unwrap() * 10,
        qty: 5,
        active: true,
    }));

    let top5 = bids.top_k_signal(5);
    let best_bid =
        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));

    let renders = Rc::new(Cell::new(0usize));
    let renders_c = Rc::clone(&renders);
    let _sub = scope.subscribe(best_bid, move |price| {
        renders_c.set(renders_c.get() + 1);
        println!(
            "[book] best bid updated -> ${}.{:02}",
            price / 100,
            price.abs() % 100
        );
    });

    let init_price = scope.get(best_bid);
    println!(
        "Initial best bid across {} active bids: ${}.{:02}",
        bids.indexed_len(),
        init_price / 100,
        init_price.abs() % 100
    );

    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
    for id in 1..=100u64 {
        bids.mutate(&id, |b| b.price_cents += 1);
    }
    assert_eq!(renders.get(), 0);
    println!(
        "After 100 off-top updates: {} UI renders (O(1) pruned)",
        renders.get()
    );

    // New aggressive bid beats top-of-book -> triggers exactly 1 render
    bids.upsert(Bid {
        id: 99_999,
        price_cents: 105_000,
        qty: 12,
        active: true,
    });
    assert_eq!(renders.get(), 1);

    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
    let available_usd = scope.atom(10_000i64);

    let mut order_a = OptimisticLayer::new(&scope);
    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800

    let mut order_b = OptimisticLayer::new(&scope);
    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
    println!(
        "After 2 optimistic orders -> displayed balance: ${}",
        scope.get(available_usd)
    );

    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
    scope.set(available_usd, 15_000);
    assert_eq!(scope.get(available_usd), 13_000);
    println!(
        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
        scope.get(available_usd)
    );

    // Order A rejected by venue -> surgical revert preserves Order B
    order_a.revert();
    assert_eq!(scope.get(available_usd), 14_200);
    println!(
        "After Order A rejected -> balance: ${}",
        scope.get(available_usd)
    );

    // Order B confirmed by venue -> atomic zero-flicker settle
    order_b.settle_with(|| scope.set(available_usd, 14_200));
    assert_eq!(scope.get(available_usd), 14_200);
    println!(
        "After Order B settled -> final committed balance: ${}",
        scope.get(available_usd)
    );
}