2026-01-25 03:39:44 +00:00
|
|
|
use swactor::{
|
|
|
|
|
actor::{ActorAddress, ActorInterface},
|
2026-01-25 04:45:34 +00:00
|
|
|
runtime::{Inbox, Runtime, RuntimeFlavor},
|
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 05:54:01 +00:00
|
|
|
let mut rt = Runtime::new();
|
2026-01-23 06:48:26 +00:00
|
|
|
let inbox: Inbox<RingMessage> = rt.new_inbox();
|
|
|
|
|
|
|
|
|
|
let mut next = rt
|
|
|
|
|
.spawn(RingActor::new(*inbox.addr()))
|
|
|
|
|
.expect("failed to spawn");
|
|
|
|
|
for _ in 0..500 {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("{msg:?}")
|
|
|
|
|
}
|