use core::fmt;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Channel(u32);
impl Channel {
pub const BROADCAST: Channel = Channel(0xffff_ffff);
pub fn to_bytes(self) -> [u8; 4] {
self.into()
}
}
impl From<u32> for Channel {
fn from(id: u32) -> Self {
Self(id)
}
}
impl From<Channel> for u32 {
fn from(channel: Channel) -> u32 {
channel.0
}
}
impl From<[u8; 4]> for Channel {
fn from(data: [u8; 4]) -> Self {
Self::from(u32::from_be_bytes(data))
}
}
impl From<&[u8; 4]> for Channel {
fn from(data: &[u8; 4]) -> Self {
Self::from(*data)
}
}
impl From<Channel> for [u8; 4] {
fn from(channel: Channel) -> Self {
channel.0.to_be_bytes()
}
}
impl fmt::Display for Channel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
#[cfg(test)]
mod test {
use super::Channel;
impl quickcheck::Arbitrary for Channel {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self(u32::arbitrary(g))
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(self.0.shrink().map(Self))
}
}
quickcheck::quickcheck! {
fn from_u32(value: u32) -> bool {
u32::from(Channel::from(value)) == value
}
}
}