use interweave::{World, explore};
#[derive(Debug, Clone, Copy)]
struct Reply {
id: i32,
result: i32,
}
fn rpc_mux(world: &mut World) {
let in_flight = world.atomic("in_flight", -1);
let (conn, reader) = world.channel::<Reply>("conn");
for id in 0..2 {
let (in_flight, conn) = (in_flight.clone(), conn.clone());
world.spawn(format!("caller-{id}"), async move {
in_flight.store(id).await;
conn.send(Reply {
id,
result: id * 10,
})
.await;
Ok(())
});
}
world.spawn("reader", async move {
for _ in 0..2 {
let frame = reader.recv().await;
let routed_to = in_flight.load().await;
if frame.result != routed_to * 10 {
return Err(format!(
"call {routed_to} received call {}'s result ({})",
frame.id, frame.result
)
.into());
}
}
Ok(())
});
}
fn main() {
match explore(&rpc_mux, &mut ()) {
Ok(()) => println!("every reply reaches its caller (unexpected for this program)"),
Err(failed) => {
println!("found a schedule where a reply is misrouted:");
println!(" {failed}");
}
}
}