Struct bastion::message::MessageHandler [−][src]
pub struct MessageHandler<O> { /* fields omitted */ }
Expand description
Matches a Msg
(as returned by BastionContext::recv
or BastionContext::try_recv
) with different types.
This type may replace the msg!
macro in the future.
The new
function creates a new MessageHandler
, which is then
matched on with the on_*
functions.
There are different kind of messages:
- messages that are broadcasted, which can be matched with the
on_broadcast
method, - messages that can be responded to, which are matched with the
on_question
method, - messages that can not be responded to, which are matched with
on_tell
, - fallback case, which matches everything, entitled
on_fallback
.
The closure passed to the functions described previously must return the
same type. This value is retrieved when on_fallback
is invoked.
Questions can be responded to by calling reply
on the provided
sender.
Example
// The message that will be broadcasted...
const BCAST_MSG: &'static str = "A message containing data (broadcast).";
// The message that will be "told" to the child...
const TELL_MSG: &'static str = "A message containing data (tell).";
// The message that will be "asked" to the child...
const ASK_MSG: &'static str = "A message containing data (ask).";
Bastion::children(|children| {
children.with_exec(|ctx: BastionContext| {
async move {
loop {
MessageHandler::new(ctx.recv().await?)
// We match on broadcasts of &str
.on_broadcast(|msg: &&str, _sender_addr| {
assert_eq!(*msg, BCAST_MSG);
// Handle the message...
})
// We match on messages of &str
.on_tell(|msg: &str, _sender_addr| {
assert_eq!(msg, TELL_MSG);
// Handle the message...
})
// We match on questions of &str
.on_question(|msg: &str, sender| {
assert_eq!(msg, ASK_MSG);
// Handle the message...
// ...and eventually answer to it...
sender.reply("An answer to the message.");
})
// We are only broadcasting, "telling" and "asking" a
// `&str` in this example, so we know that this won't
// happen...
.on_fallback(|msg, _sender_addr| ());
}
}
})
}).expect("Couldn't start the children group.");
Implementations
Creates a new MessageHandler
with an incoming message.
pub fn on_question<T, F>(self, f: F) -> MessageHandler<O> where
T: 'static,
F: FnOnce(T, AnswerSender) -> O,
pub fn on_question<T, F>(self, f: F) -> MessageHandler<O> where
T: 'static,
F: FnOnce(T, AnswerSender) -> O,
Matches on a question of a specific type.
This will consume the inner data and call f
if the contained message
can be replied to.
Calls a fallback function if the message has still not matched yet.
This consumes the MessageHandler
, so that no matching can be
performed anymore.
pub fn on_broadcast<T, F>(self, f: F) -> MessageHandler<O> where
T: 'static + Send + Sync,
F: FnOnce(&T, RefAddr) -> O,
pub fn on_broadcast<T, F>(self, f: F) -> MessageHandler<O> where
T: 'static + Send + Sync,
F: FnOnce(&T, RefAddr) -> O,
Calls a function if the incoming message is a broadcast and has a specific type.
Trait Implementations
Auto Trait Implementations
impl<O> !RefUnwindSafe for MessageHandler<O>
impl<O> Send for MessageHandler<O> where
O: Send,
impl<O> Sync for MessageHandler<O> where
O: Sync,
impl<O> Unpin for MessageHandler<O> where
O: Unpin,
impl<O> !UnwindSafe for MessageHandler<O>
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more