-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a proof of concept that we can match over different types using t…
…he MessageHandler
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use bastion::message::MessageHandler; | ||
use bastion::prelude::*; | ||
use std::fmt::Debug; | ||
use tracing::error; | ||
|
||
// This example shows that it is possible to use the MessageHandler to match | ||
// over different types of message. | ||
|
||
async fn child_task(ctx: BastionContext) -> Result<(), ()> { | ||
loop { | ||
MessageHandler::new(ctx.recv().await?) | ||
.on_question(|n: i32, sender| { | ||
if n == 42 { | ||
sender.reply(101).expect("Failed to reply to sender"); | ||
} else { | ||
error!("Expected number `42`, found `{}`", n); | ||
} | ||
}) | ||
.on_question(|s: &str, sender| { | ||
if s == "marco" { | ||
sender.reply("polo").expect("Failed to reply to sender"); | ||
} else { | ||
panic!("Expected string `marco`, found `{}`", s); | ||
} | ||
}) | ||
.on_fallback(|v, addr| panic!("Wrong message from {:?}: got {:?}", addr, v)) | ||
} | ||
} | ||
|
||
async fn request<T: 'static + Debug + Send + Sync>( | ||
child: &ChildRef, | ||
body: T, | ||
) -> std::io::Result<()> { | ||
let answer = child | ||
.ask_anonymously(body) | ||
.expect("Couldn't perform request") | ||
.await | ||
.expect("Couldn't receive answer"); | ||
|
||
MessageHandler::new(answer) | ||
.on_tell(|n: i32, _| assert_eq!(n, 101)) | ||
.on_tell(|s: &str, _| assert_eq!(s, "polo")) | ||
.on_fallback(|_, _| panic!("Unknown message")); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
env_logger::init(); | ||
|
||
Bastion::init(); | ||
Bastion::start(); | ||
|
||
let children = | ||
Bastion::children(|c| c.with_exec(child_task)).expect("Failed to spawn children"); | ||
|
||
let child = &children.elems()[0]; | ||
|
||
run!(request(child, 42)).unwrap(); | ||
run!(request(child, "marco")).unwrap(); | ||
|
||
// run!(request(child, "foo")).unwrap(); | ||
} |