From 156a992faa377f692e28fcd897f277a344374338 Mon Sep 17 00:00:00 2001 From: Sasha Pourcelot Date: Wed, 17 Feb 2021 19:34:28 +0100 Subject: [PATCH] Add a proof of concept that we can match over different types using the MessageHandler --- .../message_handler_multiple_types.rs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/bastion/examples/message_handler_multiple_types.rs diff --git a/src/bastion/examples/message_handler_multiple_types.rs b/src/bastion/examples/message_handler_multiple_types.rs new file mode 100644 index 00000000..0f93e09d --- /dev/null +++ b/src/bastion/examples/message_handler_multiple_types.rs @@ -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( + 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(); +}