swactor/examples/hello.rs
Zachery Aaron Shores-Chmielewski 88121780ce init
Skeletal actor framework. Somewhat unweildy, needs a message box, a better runtime, and different channels. However, hello world example works


Signed-off-by: Zachery Aaron Shores-Chmielewski <zacheryasc@gmail.com>
2025-11-24 19:39:17 -05:00

46 lines
1.1 KiB
Rust

use swactor::Actor;
use tokio::sync::oneshot;
pub enum GreeterMessage {
Name(String),
}
pub enum GreeterResponse {
Hello(String),
}
pub struct Greeter;
impl Actor for Greeter {
type Message = GreeterMessage;
type Response = GreeterResponse;
fn handle_message(&self, msg: Self::Message, tx: oneshot::Sender<Self::Response>) {
let rep = match msg {
GreeterMessage::Name(name) => GreeterResponse::Hello(format!("Hello, {name}!")),
};
if let Err(_) = tx.send(rep) {
// Greeter is not responsible for a dropped Receiver
}
}
}
fn main() {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.expect("failed to build runtime");
let greeter = Greeter::spawn(Greeter, &rt);
let response = rt
.block_on(async move {
greeter
.send(GreeterMessage::Name("world".to_string()))
.await
})
.expect("failed to get respose");
match response {
GreeterResponse::Hello(hello) => println!("{hello}"),
}
}