Added message header accessors.

Fortune for rsh's current commit: Future small blessing − 末小吉
specialisation
Avril 3 years ago
parent 2931a3e8cb
commit c98f1c5c6c

@ -37,6 +37,7 @@ pub trait MessageValue: Serialize + for<'de> Deserialize<'de>{}
impl<T: ?Sized> MessageValue for T
where T: Serialize + for<'de> Deserialize<'de>{}
/// A message that can send a value into bytes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Message<V: ?Sized + MessageValue>
{
@ -50,6 +51,34 @@ pub struct Message<V: ?Sized + MessageValue>
value: V,
}
// Accessors
impl<V: ?Sized + MessageValue> Message<V>
{
/// Get a reference to this message's header
#[inline(always)] pub fn header(&self) -> MessageHeader<'_, V>
{
MessageHeader(&self.header, PhantomData)
}
}
macro_rules! accessor {
($name:ident, $fn_name:ident, $type:ty $(; $comment:literal)?) => {
$(#[doc = $comment])?
#[inline] pub fn $fn_name(&self) -> &$type
{
&self.0.$name
}
};
($name:ident, $type:ty $(; $comment:literal)?) => (accessor!($name, $name, $type $(; $comment)?););
}
impl<'a, V: ?Sized> MessageHeader<'a, V>
{
accessor!(id, Uuid; "This message's randomly generated ID");
accessor!(idemp, idempotence, Uuid; "This message's idempotence key");
accessor!(timestamp, unix_timestamp_utc, u64; "This message's UTX unix timestamp");
accessor!(responds_to, Option<Uuid>; "The ID this message is responding to (if any)");
}
/// `SerializedMessage` header.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct SerHeader
@ -64,6 +93,10 @@ struct SerHeader
responds_to: Option<Uuid>,
}
/// A reference to a message's header.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MessageHeader<'a, V:?Sized>(&'a SerHeader, PhantomData<&'a V>);
impl<V: ?Sized + MessageValue> AsRef<V> for Message<V>
{
#[inline(always)] fn as_ref(&self) -> &V

Loading…
Cancel
Save