pub const HardwareAddressLength: usize = HardwareAddress::MaximumLength.get();
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[repr(transparent)]
pub struct HardwareAddress(ArrayVec<u8, HardwareAddressLength>);
impl Deref for HardwareAddress
{
type Target = [u8];
#[inline(always)]
fn deref(&self) -> &Self::Target
{
&self.0[..]
}
}
impl From<ArrayVec<u8, HardwareAddressLength>> for HardwareAddress
{
#[inline(always)]
fn from(value: ArrayVec<u8, HardwareAddressLength>) -> Self
{
Self(value)
}
}
impl<'a> TryFrom<&'a [u8]> for HardwareAddress
{
type Error = String;
#[inline(always)]
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error>
{
let length = value.len();
if unlikely!(length < HardwareAddress::MinimumLength.get())
{
Err(format!("field has a hardware address that is too short ({}), can not be less than HardwareAddress::MinimumLength ({})", length, Self::MinimumLength))
}
else if unlikely!(length > HardwareAddress::MaximumLength.get())
{
Err(format!("field has a hardware address that is too long ({}), can not greater than HardwareAddress::MaximumLength ({})", length, Self::MaximumLength))
}
else
{
Ok(Self(ArrayVec::try_from(value).unwrap()))
}
}
}
impl HardwareAddress
{
pub const MinimumLength: NonZeroUsize = new_non_zero_usize(1);
pub const MaximumLength: NonZeroUsize = new_non_zero_usize(MAX_ADDR_LEN);
pub const EthernetMediaAccessControlLength: NonZeroUsize = new_non_zero_usize(6);
}