pub(crate) const CHANNEL_ID_LEN: usize = core::mem::size_of::<u8>();
pub(crate) const MESSAGE_ID_LEN: usize = core::mem::size_of::<u32>();
pub(crate) const MESSAGE_LEN_LEN: usize = core::mem::size_of::<u16>();
pub(crate) const FRAGMENT_OFFSET_LEN: usize = core::mem::size_of::<u16>();
pub(crate) const FRAGMENT_FLAGS_LEN: usize = core::mem::size_of::<u8>();
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ChannelId(pub u8);
impl ChannelId {
pub const DEFAULT: Self = Self(0);
pub const LOG: Self = Self(1);
pub const LIVENESS: Self = Self(2);
pub const FIRST_APPLICATION_DEFINED: Self = Self(16);
#[must_use]
pub const fn new(raw: u8) -> Self {
Self(raw)
}
#[must_use]
pub const fn get(self) -> u8 {
self.0
}
#[must_use]
pub const fn is_default(self) -> bool {
self.0 == Self::DEFAULT.0
}
#[must_use]
pub const fn is_log(self) -> bool {
self.0 == Self::LOG.0
}
#[must_use]
pub const fn is_liveness(self) -> bool {
self.0 == Self::LIVENESS.0
}
#[must_use]
pub const fn is_application_defined(self) -> bool {
self.0 >= Self::FIRST_APPLICATION_DEFINED.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MessageId(pub u32);
impl MessageId {
pub const ZERO: Self = Self(0);
#[must_use]
pub const fn new(raw: u32) -> Self {
Self(raw)
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MessageFlags(pub u8);
impl MessageFlags {
pub const EMPTY: Self = Self(0);
pub const FIRST: Self = Self(1 << 0);
pub const LAST: Self = Self(1 << 1);
#[must_use]
pub const fn from_bits(bits: u8) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u8 {
self.0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
#[cfg(test)]
mod tests {
use super::{ChannelId, MessageFlags, MessageId};
#[test]
fn message_identity_primitives_expose_raw_values() {
assert_eq!(ChannelId::new(9).get(), 9);
assert_eq!(MessageId::new(7).get(), 7);
assert!(MessageFlags::FIRST.contains(MessageFlags::FIRST));
assert!(!MessageFlags::FIRST.contains(MessageFlags::LAST));
}
}