2018-05-10 08:50:07 -07:00
|
|
|
/// Message sent by a given source.
|
2018-05-05 06:39:32 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2018-05-10 08:50:07 -07:00
|
|
|
pub struct SourcedMessage<M, N> {
|
2018-11-26 06:35:24 -08:00
|
|
|
/// The ID of the sender.
|
2018-05-10 08:50:07 -07:00
|
|
|
pub source: N,
|
2018-11-27 03:13:42 -08:00
|
|
|
/// The content of a message.
|
2018-05-10 08:50:07 -07:00
|
|
|
pub message: M,
|
2018-04-18 01:15:12 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 03:13:42 -08:00
|
|
|
/// The intended recipient(s) of a message.
|
2018-04-19 10:27:26 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2018-05-10 08:50:07 -07:00
|
|
|
pub enum Target<N> {
|
2018-11-26 06:35:24 -08:00
|
|
|
/// The message must be sent to all remote nodes.
|
2018-04-09 06:06:30 -07:00
|
|
|
All,
|
2018-11-26 06:35:24 -08:00
|
|
|
/// The message must be sent to the node with the given ID.
|
2018-05-10 08:50:07 -07:00
|
|
|
Node(N),
|
2018-04-09 06:06:30 -07:00
|
|
|
}
|
|
|
|
|
2018-05-12 07:09:07 -07:00
|
|
|
impl<N> Target<N> {
|
|
|
|
/// Returns a `TargetedMessage` with this target, and the given message.
|
|
|
|
pub fn message<M>(self, message: M) -> TargetedMessage<M, N> {
|
|
|
|
TargetedMessage {
|
|
|
|
target: self,
|
|
|
|
message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 06:06:30 -07:00
|
|
|
/// Message with a designated target.
|
2018-04-19 10:27:26 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2018-05-10 08:50:07 -07:00
|
|
|
pub struct TargetedMessage<M, N> {
|
2018-11-26 06:35:24 -08:00
|
|
|
/// The node or nodes that this message must be delivered to.
|
2018-05-10 08:50:07 -07:00
|
|
|
pub target: Target<N>,
|
2018-11-26 06:35:24 -08:00
|
|
|
/// The content of the message that must be serialized and sent to the target.
|
2018-05-10 08:50:07 -07:00
|
|
|
pub message: M,
|
2018-04-09 06:06:30 -07:00
|
|
|
}
|
2018-05-12 07:09:07 -07:00
|
|
|
|
|
|
|
impl<M, N> TargetedMessage<M, N> {
|
|
|
|
/// Applies the given transformation of messages, preserving the target.
|
|
|
|
pub fn map<T, F: Fn(M) -> T>(self, f: F) -> TargetedMessage<T, N> {
|
|
|
|
TargetedMessage {
|
|
|
|
target: self.target,
|
|
|
|
message: f(self.message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|