Expand description
Standardised messages.
To use these message the receiving actor should implement From
<Message>
,
this way the sending actor can simply send the message, without having to
wrap it in a message type first. See the examples below.
Most message types have an optional id, defaulting to ()
. This allows a
single actor to receive messages from multiple sources with the ability to
differentiate the source of the message.
Three more default messages should be considered, the two variants from
Result
: Ok
and Err
. Lastly there is the Signal
type from the
heph-rt
crate to handle process signals.
Examples
Implementing From<Message>
to allow for easy sending of messages.
use heph::messages::Ack;
#[derive(Debug, Eq, PartialEq)]
struct OK;
#[derive(Debug, Eq, PartialEq)]
struct Error;
/// The message type for the coordinating actor.
#[derive(Debug, Eq, PartialEq)]
enum Message {
/// Acknowledgement of receiving an message.
Ack(usize),
/// An ok result.
Ok(OK),
/// An erroneous result.
Error(Error),
}
// This allows us to receive an `Ack` message.
impl From<Ack<usize>> for Message {
fn from(ack: Ack<usize>) -> Message {
Message::Ack(ack.0)
}
}
// Abilities to receive an result from a working actor.
impl From<Result<OK, Error>> for Message {
fn from(res: Result<OK, Error>) -> Message {
match res {
Ok(ok) => Message::Ok(ok),
Err(err) => Message::Error(err),
}
}
}