use super::{core::SeqNum, id};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FragmentHeader {
pub(crate) header: u8,
pub(crate) frag_id: SeqNum,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub(crate) enum FragmentKind {
First = 0b00 << Self::SHIFT,
More = 0b01 << Self::SHIFT,
Last = 0b10 << Self::SHIFT,
}
impl FragmentKind {
pub(super) const SHIFT: u8 = 5;
pub(super) const MASK: u8 = 0b11 << Self::SHIFT;
#[cfg(test)]
pub(crate) fn rand() -> Self {
match rand::random_range(0..3) {
0 => Self::First,
1 => Self::More,
2 => Self::Last,
_ => unreachable!(),
}
}
}
impl FragmentHeader {
pub(crate) const fn new(kind: FragmentKind, frag_id: SeqNum) -> Self {
Self {
header: id::FRAGMENT | kind as u8,
frag_id,
}
}
pub(crate) const unsafe fn from_parts(header: u8, frag_id: SeqNum) -> Self {
Self { header, frag_id }
}
pub(crate) fn kind(&self) -> FragmentKind {
unsafe { core::mem::transmute(self.header & FragmentKind::MASK) }
}
#[cfg(test)]
pub(crate) fn rand() -> Self {
FragmentHeader::new(FragmentKind::rand(), SeqNum::rand())
}
}