#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct InternetHeaderLength(u8);
impl Into<u8> for InternetHeaderLength
{
#[inline(always)]
fn into(self) -> u8
{
self.0
}
}
impl Into<usize> for InternetHeaderLength
{
#[inline(always)]
fn into(self) -> usize
{
self.0 as usize
}
}
impl Display for InternetHeaderLength
{
#[inline(always)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result
{
write!(f, "{}", self.0)
}
}
impl TryFrom<u8> for InternetHeaderLength
{
type Error = ();
#[inline(always)]
fn try_from(value: u8) -> Result<Self, Self::Error>
{
if value < 5 || value > 15
{
Err(())
}
else
{
Ok(InternetHeaderLength(value))
}
}
}
impl InternetHeaderLength
{
#[inline(always)]
pub fn from_original_field_in_packet(original_value: u8) -> Result<Self, ()>
{
const BitsPerWord: u8 = 32;
const BitsPerOctet: u8 = 8;
const BytesScalar: u8 = BitsPerWord / BitsPerOctet;
let masked_original_value = original_value & 0xF0 >> 4;
if masked_original_value < 5
{
Err(())
}
else
{
Ok(InternetHeaderLength((masked_original_value) * BytesScalar))
}
}
#[inline(always)]
pub fn internet_control_message_protocol_destination_unreachable_packet_length(self) -> usize
{
const InternetControlMessageProtocolPacketHeader: usize = 8;
let into: usize = self.into();
const _64_Bits: usize = 8;
let payload_length = into + _64_Bits;
InternetControlMessageProtocolPacketHeader + (payload_length as usize)
}
}