#[repr(transparent)]pub struct U16<O>(_, _);
Expand description
A 16-bit unsigned integer stored in O
byte order.
U16
is like the native u16
type with
two major differences: First, it has no alignment requirement (its alignment is 1).
Second, the endianness of its memory layout is given by the type parameter O
.
A U16
can be constructed using
the new
method, and its contained value can be obtained as a native
u16
using the get
method, or updated in place with
the set
method. In all cases, if the endianness O
is not the same as the
endianness of the current platform, an endianness swap will be performed in
order to uphold the invariants that a) the layout of U16
has endianness O
and that, b) the layout of u16
has
the platform’s native endianness.
U16
implements FromBytes
, AsBytes
, and Unaligned
,
making it useful for parsing and serialization. See the module documentation for an
example of how it can be used for parsing UDP packets.
Implementations§
source§impl<O> U16<O>
impl<O> U16<O>
sourcepub const ZERO: U16<O> = _
pub const ZERO: U16<O> = _
The value zero.
This constant should be preferred to constructing a new value
using new
, as new
may perform an endianness swap depending
on the endianness and platform.
sourcepub const MAX_VALUE: U16<O> = _
pub const MAX_VALUE: U16<O> = _
The maximum value.
This constant should be preferred to constructing a new value using
new
, as new
may perform an endianness swap depending on the
endianness O
and the endianness of the platform.
sourcepub const fn from_bytes(bytes: [u8; 2]) -> U16<O>
pub const fn from_bytes(bytes: [u8; 2]) -> U16<O>
Constructs a new value from bytes which are already in the
endianness O
.
source§impl<O: ByteOrder> U16<O>
impl<O: ByteOrder> U16<O>
sourcepub fn new(n: u16) -> U16<O>
pub fn new(n: u16) -> U16<O>
Constructs a new value, possibly performing an endianness swap
to guarantee that the returned value has endianness O
.
Trait Implementations§
source§impl<O> AsBytes for U16<O>where
PhantomData<O>: AsBytes,
[u8; 2]: AsBytes,
impl<O> AsBytes for U16<O>where PhantomData<O>: AsBytes, [u8; 2]: AsBytes,
source§fn as_bytes_mut(&mut self) -> &mut [u8]where
Self: FromBytes,
fn as_bytes_mut(&mut self) -> &mut [u8]where Self: FromBytes,
source§fn write_to_prefix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
fn write_to_prefix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
source§fn write_to_suffix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
fn write_to_suffix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
source§impl<O> FromBytes for U16<O>where
PhantomData<O>: FromBytes,
[u8; 2]: FromBytes,
impl<O> FromBytes for U16<O>where PhantomData<O>: FromBytes, [u8; 2]: FromBytes,
source§fn new_zeroed() -> Selfwhere
Self: Sized,
fn new_zeroed() -> Selfwhere Self: Sized,
Self
from zeroed bytes.source§fn new_box_zeroed() -> Box<Self>where
Self: Sized,
fn new_box_zeroed() -> Box<Self>where Self: Sized,
Box<Self>
from zeroed bytes. Read more