Struct etherparse::IpFragOffset
source · pub struct IpFragOffset(/* private fields */);
Expand description
The fragment offset is a 13 bit unsigned integer indicating the stating position of the payload of a packet relative to the originally fragmented packet payload.
This value can be present in an crate::Ipv4Header
or an
crate::Ipv6FragmentHeader
.
§Example Usage:
use etherparse::IpFragOffset;
// try into
{
let frag_offset: IpFragOffset = 123.try_into().unwrap();
assert_eq!(frag_offset.value(), 123);
// fragment offset can always be converted back to an u16
let value: u16 = frag_offset.into();
assert_eq!(123, value);
}
// via try_new
{
let frag_offset = IpFragOffset::try_new(123).unwrap();
assert_eq!(frag_offset.value(), 123);
// note that only 13 bit numbers are allowed (meaning
// 0b0001_1111_1111_1111 is the maximum allowed value)
use etherparse::err::{ValueTooBigError, ValueType};
assert_eq!(
IpFragOffset::try_new(IpFragOffset::MAX_U16 + 1),
Err(ValueTooBigError{
actual: IpFragOffset::MAX_U16 + 1,
max_allowed: IpFragOffset::MAX_U16,
value_type: ValueType::IpFragmentOffset,
})
);
}
// via new_unchecked
{
// in case you are sure the number does not exceed the max
// you can use the unsafe new_unchecked function
let frag_offset = unsafe {
// please make sure that the value is not greater than IpFragOffset::MAX_U16
// before calling this method
IpFragOffset::new_unchecked(123)
};
assert_eq!(frag_offset.value(), 123);
}
Implementations§
source§impl IpFragOffset
impl IpFragOffset
sourcepub const ZERO: IpFragOffset = _
pub const ZERO: IpFragOffset = _
IpFragOffset with value 0.
sourcepub const fn try_new(value: u16) -> Result<IpFragOffset, ValueTooBigError<u16>>
pub const fn try_new(value: u16) -> Result<IpFragOffset, ValueTooBigError<u16>>
Tries to create an IpFragOffset
and checks that the passed value
is smaller or equal than IpFragOffset::MAX_U16
(13 bit unsigned integer).
In case the passed value is bigger then what can be represented in an 13 bit
integer an error is returned. Otherwise an Ok
containing the IpFragOffset
.
use etherparse::IpFragOffset;
let frag_offset = IpFragOffset::try_new(123).unwrap();
assert_eq!(frag_offset.value(), 123);
// if a number that can not be represented in an 13 bit integer
// gets passed in an error is returned
use etherparse::err::{ValueTooBigError, ValueType};
assert_eq!(
IpFragOffset::try_new(IpFragOffset::MAX_U16 + 1),
Err(ValueTooBigError{
actual: IpFragOffset::MAX_U16 + 1,
max_allowed: IpFragOffset::MAX_U16,
value_type: ValueType::IpFragmentOffset,
})
);
sourcepub const unsafe fn new_unchecked(value: u16) -> IpFragOffset
pub const unsafe fn new_unchecked(value: u16) -> IpFragOffset
Creates an IpFragOffset
without checking that the value
is smaller or equal than IpFragOffset::MAX_U16
(13 bit unsigned integer).
The caller must guarantee that value <= IpFragOffset::MAX_U16
.
§Safety
value
must be smaller or equal than IpFragOffset::MAX_U16
otherwise the behavior of functions or data structures relying
on this pre-requirement is undefined.
Trait Implementations§
source§impl Clone for IpFragOffset
impl Clone for IpFragOffset
source§fn clone(&self) -> IpFragOffset
fn clone(&self) -> IpFragOffset
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for IpFragOffset
impl Debug for IpFragOffset
source§impl Default for IpFragOffset
impl Default for IpFragOffset
source§fn default() -> IpFragOffset
fn default() -> IpFragOffset
source§impl Display for IpFragOffset
impl Display for IpFragOffset
source§impl From<IpFragOffset> for u16
impl From<IpFragOffset> for u16
source§fn from(value: IpFragOffset) -> Self
fn from(value: IpFragOffset) -> Self
source§impl Hash for IpFragOffset
impl Hash for IpFragOffset
source§impl Ord for IpFragOffset
impl Ord for IpFragOffset
source§fn cmp(&self, other: &IpFragOffset) -> Ordering
fn cmp(&self, other: &IpFragOffset) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq for IpFragOffset
impl PartialEq for IpFragOffset
source§fn eq(&self, other: &IpFragOffset) -> bool
fn eq(&self, other: &IpFragOffset) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for IpFragOffset
impl PartialOrd for IpFragOffset
source§fn partial_cmp(&self, other: &IpFragOffset) -> Option<Ordering>
fn partial_cmp(&self, other: &IpFragOffset) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more