swactor/examples/hello.rs
zacheryasc 33956d7f22 feat: Multithreaded runtime (#2)
Implements a tunable configuration for a single or multi-threaded runtime.

Reviewed-on: http://zachery.lol/code/code/zacheryasc/swactor/pulls/2


Signed-off-by: Zachery Aaron Shores-Chmielewski <zacheryasc@gmail.com>
2026-01-25 13:38:34 +00:00

65 lines
1.6 KiB
Rust

use swactor::{
actor::{ActorAddress, ActorInterface},
runtime::{Runtime, RuntimeConfig},
};
#[derive(Debug, Default)]
struct Greeter {
pub num_greeted: usize,
}
#[derive(Debug, Default, Clone)]
struct GreetMessage {
/// who do we greet?
who: String,
/// who do we send out greeting back to?
return_addr: ActorAddress,
}
#[derive(Debug, Default, Clone)]
struct GreetResponse(String);
impl ActorInterface for Greeter {
type Incoming = GreetMessage;
type Response = GreetResponse;
fn handle(&mut self, ctx: &Runtime, msg: GreetMessage) {
let res = GreetResponse(format!("Hello, {}!", msg.who));
self.num_greeted += 1;
if let Err(_) = ctx.send_to(msg.return_addr, res) {
// no error handling
self.num_greeted -= 1;
}
}
}
fn main() {
let rt = Runtime::new(RuntimeConfig::default());
// spawn a `Greeter` in the runtime, returning an address to contact it with
let addr = rt
.spawn(Greeter::default())
.expect("failed to spawn greeter");
// create an `Inbox` that allows us to receive messages from the runtime
let inbox = rt.new_inbox::<GreetResponse>().unwrap();
// send a message to the `Greeter` we spawned
rt.send_to(
addr,
GreetMessage {
who: "world".into(),
return_addr: *inbox.addr(),
},
)
.unwrap();
// default runtime is single threaded, and requires the parent process to drive
for _ in 0..3 {
rt.tick();
}
let resp = inbox.try_recv().expect("greeter should have said hello");
println!("{}", resp.0);
}