From a9f7abba25653b3fc46e4e1f7957c53522f3d28c Mon Sep 17 00:00:00 2001 From: Zachery Aaron Shores-Chmielewski Date: Fri, 23 Jan 2026 13:48:26 +0700 Subject: [PATCH] feat: mvp actor ring test --- examples/hello.rs | 7 +++-- examples/ring.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++- 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 examples/ring.rs diff --git a/examples/hello.rs b/examples/hello.rs index 3cf176a..d83fbc0 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -13,7 +13,9 @@ struct GreetMessage { /// who do we send out greeting back to? return_addr: ActorAddress, } -impl Message for GreetMessage {} + +#[derive(Debug, Default, Clone)] +struct GreetResponse(String); impl ActorInterface for Greeter { type Incoming = GreetMessage; @@ -29,9 +31,6 @@ impl ActorInterface for Greeter { } } -#[derive(Debug, Default, Clone)] -struct GreetResponse(String); -impl Message for GreetResponse {} fn main() { let mut rt = Runtime::new(100, Some(RuntimeFlavor::SingleThreaded)); diff --git a/examples/ring.rs b/examples/ring.rs new file mode 100644 index 0000000..4e854f5 --- /dev/null +++ b/examples/ring.rs @@ -0,0 +1,65 @@ +use swactor::{ActorAddress, ActorInterface, Inbox, Runtime, RuntimeFlavor}; + +#[derive(Debug, Default, Clone)] +struct RingMessage { + 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 = (); + fn handle(&mut self, ctx: &Runtime, msg: Self::Incoming) { + if let Err(_) = ctx.send_to(self.next, msg.next()) { + // do nothing + } + } +} + +fn main() { + let mut rt = Runtime::new(10_000, Some(RuntimeFlavor::SingleThreaded)); + let inbox: Inbox = 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:?}") +} diff --git a/src/lib.rs b/src/lib.rs index bd6913b..7da4647 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,9 +20,11 @@ pub fn get_random(buf: &mut [u8]) { /// process total_messages // 2 const WATERLEVEL: usize = 10; -const DEFAULT_INBOX_CAPACITY: usize = 100; +const DEFAULT_INBOX_CAPACITY: usize = 1_000; pub trait Message: 'static + Sized + Clone + Send {} +impl Message for T {} + pub type Envelope = Box; pub trait ActorInterface: 'static + Send {