logo

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.

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.

Calls a function if the incoming message is a broadcast and has a specific type.

Calls a function if the incoming message can’t be replied to and has a specific type.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Downcast implemented type to Any. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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