#[derive(Debug, PartialEq)]
pub struct Flags {
pub broadcast: bool,
}
impl Flags {
const EMPTY: [u8; 2] = [0, 0];
const BROADCAST: [u8; 2] = [128, 0];
#[inline]
pub const fn temporary_to_bytes(&self) -> [u8; 2] {
if self.broadcast {
Self::BROADCAST
} else {
Self::EMPTY
}
}
}
impl From<&Flags> for [u8; 2] {
fn from(flags: &Flags) -> Self {
let mut bytes: u16 = 0b00000000_00000000;
if flags.broadcast {
bytes |= 0b10000000_00000000
}
bytes.to_be_bytes()
}
}
impl From<u8> for Flags {
fn from(byte: u8) -> Self {
let broadcast = byte & 0b10000000 == 0b10000000;
Flags { broadcast }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn broadcast_const_is_correct() {
let decimal: u16 = 0b10000000_00000000;
assert_eq!(Flags::BROADCAST, decimal.to_be_bytes());
}
}