2026-01-25 03:39:44 +00:00
|
|
|
use swactor::{
|
|
|
|
|
actor::{ActorAddress, ActorInterface},
|
2026-01-25 13:31:34 +00:00
|
|
|
runtime::{Inbox, Runtime, RuntimeConfig},
|
2026-01-25 03:39:44 +00:00
|
|
|
};
|
2026-01-23 06:48:26 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
2026-01-23 07:09:31 +00:00
|
|
|
pub struct RingMessage {
|
2026-01-23 06:48:26 +00:00
|
|
|
count: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RingMessage {
|
|
|
|
|
pub fn next(self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
count: self.count + 1,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
struct RingActor {
|
|
|
|
|
next: ActorAddress,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RingActor {
|
|
|
|
|
pub fn new(next: ActorAddress) -> Self {
|
|
|
|
|
Self { next }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ActorInterface for RingActor {
|
|
|
|
|
type Incoming = RingMessage;
|
|
|
|
|
type Response = ();
|
2026-01-25 04:45:34 +00:00
|
|
|
fn handle(&mut self, ctx: &Runtime, msg: Self::Incoming) {
|
2026-01-23 06:48:26 +00:00
|
|
|
if let Err(_) = ctx.send_to(self.next, msg.next()) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2026-01-25 13:31:34 +00:00
|
|
|
let config = RuntimeConfig::default();
|
|
|
|
|
let rt = Runtime::new(config);
|
|
|
|
|
let inbox: Inbox<RingMessage> = rt.new_inbox().unwrap();
|
2026-01-23 06:48:26 +00:00
|
|
|
|
|
|
|
|
let mut next = rt
|
|
|
|
|
.spawn(RingActor::new(*inbox.addr()))
|
|
|
|
|
.expect("failed to spawn");
|
2026-01-25 13:31:34 +00:00
|
|
|
let num_passes = 500;
|
|
|
|
|
for _ in 0..num_passes {
|
2026-01-23 06:48:26 +00:00
|
|
|
let new = rt.spawn(RingActor::new(next)).expect("failed to spawn");
|
|
|
|
|
next = new;
|
|
|
|
|
}
|
|
|
|
|
rt.send_to(next, RingMessage { count: 0 })
|
|
|
|
|
.expect("failed to start message ring");
|
|
|
|
|
|
|
|
|
|
let msg: RingMessage;
|
|
|
|
|
loop {
|
|
|
|
|
match inbox.try_recv() {
|
|
|
|
|
Some(m) => {
|
|
|
|
|
msg = m;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
rt.tick();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-25 13:31:34 +00:00
|
|
|
assert_eq!(msg.count, num_passes + 1); // count should equal the number of passes plus the return to main process inbox
|
2026-01-23 06:48:26 +00:00
|
|
|
|
2026-01-25 13:31:34 +00:00
|
|
|
println!("{msg:?}");
|
2026-01-23 06:48:26 +00:00
|
|
|
}
|