-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstreams.rs
More file actions
37 lines (33 loc) · 1.03 KB
/
streams.rs
File metadata and controls
37 lines (33 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use hannibal::prelude::*;
#[derive(Default, Actor)]
struct FizzBuzzer(&'static str);
impl StreamHandler<i32> for FizzBuzzer {
async fn handle(&mut self, _ctx: &mut Context<Self>, msg: i32) {
match (msg % 3 == 0, msg % 5 == 0) {
(true, true) => {
self.0 = "fizzbuzz";
println!("{msg} -> {inner}", inner = self.0, msg = msg);
}
(true, false) => {
self.0 = "fizz";
println!("{msg} -> {inner}", inner = self.0, msg = msg);
}
(false, true) => {
self.0 = "buzz";
println!("{msg} -> {inner}", inner = self.0, msg = msg);
}
_ => {}
}
}
async fn finished(&mut self, ctx: &mut Context<Self>) {
ctx.stop().unwrap();
}
}
#[hannibal::main]
async fn main() {
let num_stream = futures::stream::iter(1..30);
let addr = hannibal::setup_actor(FizzBuzzer::default())
.on_stream(num_stream)
.spawn();
addr.await.unwrap();
}