feat: mvp actor ring test
This commit is contained in:
parent
55077dbf57
commit
a9f7abba25
3 changed files with 71 additions and 5 deletions
|
|
@ -13,7 +13,9 @@ struct GreetMessage {
|
||||||
/// who do we send out greeting back to?
|
/// who do we send out greeting back to?
|
||||||
return_addr: ActorAddress,
|
return_addr: ActorAddress,
|
||||||
}
|
}
|
||||||
impl Message for GreetMessage {}
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
struct GreetResponse(String);
|
||||||
|
|
||||||
impl ActorInterface for Greeter {
|
impl ActorInterface for Greeter {
|
||||||
type Incoming = GreetMessage;
|
type Incoming = GreetMessage;
|
||||||
|
|
@ -29,9 +31,6 @@ impl ActorInterface for Greeter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
struct GreetResponse(String);
|
|
||||||
impl Message for GreetResponse {}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut rt = Runtime::new(100, Some(RuntimeFlavor::SingleThreaded));
|
let mut rt = Runtime::new(100, Some(RuntimeFlavor::SingleThreaded));
|
||||||
|
|
|
||||||
65
examples/ring.rs
Normal file
65
examples/ring.rs
Normal file
|
|
@ -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<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:?}")
|
||||||
|
}
|
||||||
|
|
@ -20,9 +20,11 @@ pub fn get_random(buf: &mut [u8]) {
|
||||||
/// process total_messages // 2
|
/// process total_messages // 2
|
||||||
const WATERLEVEL: usize = 10;
|
const WATERLEVEL: usize = 10;
|
||||||
|
|
||||||
const DEFAULT_INBOX_CAPACITY: usize = 100;
|
const DEFAULT_INBOX_CAPACITY: usize = 1_000;
|
||||||
|
|
||||||
pub trait Message: 'static + Sized + Clone + Send {}
|
pub trait Message: 'static + Sized + Clone + Send {}
|
||||||
|
impl<T: 'static + Sized + Clone + Send> Message for T {}
|
||||||
|
|
||||||
pub type Envelope = Box<dyn std::any::Any + Send>;
|
pub type Envelope = Box<dyn std::any::Any + Send>;
|
||||||
|
|
||||||
pub trait ActorInterface: 'static + Send {
|
pub trait ActorInterface: 'static + Send {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue