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.
Sourcepub const fn byte_offset(self) -> u16
pub const fn byte_offset(self) -> u16
Returns the offset in bytes (offset raw value multiplied by 8).
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 more