diff --git a/src/message.rs b/src/message.rs index a96a2a2..fb1326e 100644 --- a/src/message.rs +++ b/src/message.rs @@ -37,6 +37,7 @@ pub trait MessageValue: Serialize + for<'de> Deserialize<'de>{} impl 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 { @@ -50,6 +51,34 @@ pub struct Message value: V, } +// Accessors +impl Message +{ + /// 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; "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, } +/// 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 AsRef for Message { #[inline(always)] fn as_ref(&self) -> &V