Skip to main content

atomic_actors/
atomic_actors.rs

1//! Atomic Hop: one [`byteflow::Value::Message`] per request/reply hop.
2//!
3//! Canonical Atomic Hop demo: server loop + two `Ask` clients
4//! (`samples::atomic_actors`). Minted `request_id`s, stable `reply_cap`,
5//! and a 72-sum join. See `docs/atomic-hop.md`.
6//!
7//! ```text
8//! cargo run -p byteflow-actors --example atomic_actors
9//!
10//! # scheduler stderr (spawn/send/recv/finish) — separate from print's stdout
11//! $env:BYTEFLOW_LOG="info"
12//! cargo run -p byteflow-actors --example atomic_actors
13//! ```
14//!
15//! Failures from `Runtime::…` / `spawn` are printed and exit non-zero —
16//! matching the fail-closed host API (no `.expect` on the happy path).
17
18use byteflow::{samples, std_native_table, FlowOutcome, Runtime, RuntimeConfig, Value};
19
20fn main() {
21    let chunk = samples::atomic_actors();
22    let rt = match Runtime::with_natives_and_config(
23        chunk,
24        std_native_table(),
25        RuntimeConfig {
26            workers: 2,
27            quantum: 10_000,
28            mailbox: byteflow::MailboxConfig::DEFAULT,
29            ..Default::default()
30        },
31    ) {
32        Ok(rt) => rt,
33        Err(e) => {
34            eprintln!("runtime: {e}");
35            std::process::exit(1);
36        }
37    };
38    let Some(main) = rt.function_index("main") else {
39        eprintln!("missing main");
40        std::process::exit(1);
41    };
42    let handle = match rt.spawn(main, &[]) {
43        Ok(h) => h,
44        Err(e) => {
45            eprintln!("spawn: {e}");
46            std::process::exit(1);
47        }
48    };
49    let outcome = handle.join();
50    let metrics = rt.metrics();
51    rt.shutdown();
52
53    match outcome {
54        FlowOutcome::Completed(Value::Int(72)) => {
55            println!("atomic actors ok: two clients × 8 Ask = 72");
56            println!("{metrics}");
57        }
58        other => {
59            eprintln!("unexpected {other:?}");
60            std::process::exit(1);
61        }
62    }
63}