pub struct U16<O>(/* private fields */);
Expand description
A 16-bit unsigned integer stored in a given 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
,
which can be any type which implements ByteOrder
. In particular, this refers
to BigEndian
, LittleEndian
, NativeEndian
, and NetworkEndian
.
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: ByteOrder> AddAssign for U16<O>
impl<O: ByteOrder> AddAssign for U16<O>
Source§fn add_assign(&mut self, rhs: U16<O>)
fn add_assign(&mut self, rhs: U16<O>)
+=
operation. Read moreSource§impl<O> AsBytes for U16<O>
impl<O> AsBytes for U16<O>
Source§impl<O: ByteOrder> BitAndAssign for U16<O>
impl<O: ByteOrder> BitAndAssign for U16<O>
Source§fn bitand_assign(&mut self, rhs: U16<O>)
fn bitand_assign(&mut self, rhs: U16<O>)
&=
operation. Read moreSource§impl<O: ByteOrder> BitOrAssign for U16<O>
impl<O: ByteOrder> BitOrAssign for U16<O>
Source§fn bitor_assign(&mut self, rhs: U16<O>)
fn bitor_assign(&mut self, rhs: U16<O>)
|=
operation. Read moreSource§impl<O: ByteOrder> BitXorAssign for U16<O>
impl<O: ByteOrder> BitXorAssign for U16<O>
Source§fn bitxor_assign(&mut self, rhs: U16<O>)
fn bitxor_assign(&mut self, rhs: U16<O>)
^=
operation. Read moreSource§impl<O: ByteOrder> DivAssign for U16<O>
impl<O: ByteOrder> DivAssign for U16<O>
Source§fn div_assign(&mut self, rhs: U16<O>)
fn div_assign(&mut self, rhs: U16<O>)
/=
operation. Read moreSource§impl<O> FromBytes for U16<O>
impl<O> FromBytes for U16<O>
Source§fn mut_from_prefix(bytes: &mut [u8]) -> Option<&mut Self>
fn mut_from_prefix(bytes: &mut [u8]) -> Option<&mut Self>
Source§fn mut_from_suffix(bytes: &mut [u8]) -> Option<&mut Self>
fn mut_from_suffix(bytes: &mut [u8]) -> Option<&mut Self>
Source§fn slice_from_prefix(bytes: &[u8], count: usize) -> Option<(&[Self], &[u8])>where
Self: Sized,
fn slice_from_prefix(bytes: &[u8], count: usize) -> Option<(&[Self], &[u8])>where
Self: Sized,
bytes
as a &[Self]
with length
equal to count
without copying. Read moreSource§fn slice_from_suffix(bytes: &[u8], count: usize) -> Option<(&[u8], &[Self])>where
Self: Sized,
fn slice_from_suffix(bytes: &[u8], count: usize) -> Option<(&[u8], &[Self])>where
Self: Sized,
bytes
as a &[Self]
with length
equal to count
without copying. Read moreSource§fn mut_slice_from(bytes: &mut [u8]) -> Option<&mut [Self]>
fn mut_slice_from(bytes: &mut [u8]) -> Option<&mut [Self]>
Source§fn mut_slice_from_prefix(
bytes: &mut [u8],
count: usize,
) -> Option<(&mut [Self], &mut [u8])>
fn mut_slice_from_prefix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [Self], &mut [u8])>
bytes
as a &mut [Self]
with length
equal to count
without copying. Read moreSource§fn mut_slice_from_suffix(
bytes: &mut [u8],
count: usize,
) -> Option<(&mut [u8], &mut [Self])>
fn mut_slice_from_suffix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [u8], &mut [Self])>
bytes
as a &mut [Self]
with length
equal to count
without copying. Read moreSource§impl<O> FromZeroes for U16<O>
impl<O> FromZeroes for U16<O>
Source§impl<O: ByteOrder> MulAssign for U16<O>
impl<O: ByteOrder> MulAssign for U16<O>
Source§fn mul_assign(&mut self, rhs: U16<O>)
fn mul_assign(&mut self, rhs: U16<O>)
*=
operation. Read moreSource§impl<O: ByteOrder> RemAssign for U16<O>
impl<O: ByteOrder> RemAssign for U16<O>
Source§fn rem_assign(&mut self, rhs: U16<O>)
fn rem_assign(&mut self, rhs: U16<O>)
%=
operation. Read moreSource§impl<O: ByteOrder> ShlAssign for U16<O>
impl<O: ByteOrder> ShlAssign for U16<O>
Source§fn shl_assign(&mut self, rhs: U16<O>)
fn shl_assign(&mut self, rhs: U16<O>)
<<=
operation. Read moreSource§impl<O: ByteOrder> ShrAssign for U16<O>
impl<O: ByteOrder> ShrAssign for U16<O>
Source§fn shr_assign(&mut self, rhs: U16<O>)
fn shr_assign(&mut self, rhs: U16<O>)
>>=
operation. Read moreSource§impl<O: ByteOrder> SubAssign for U16<O>
impl<O: ByteOrder> SubAssign for U16<O>
Source§fn sub_assign(&mut self, rhs: U16<O>)
fn sub_assign(&mut self, rhs: U16<O>)
-=
operation. Read moreimpl<O: Copy> Copy for U16<O>
impl<O: Eq> Eq for U16<O>
impl<O> StructuralPartialEq for U16<O>
impl<O> Unaligned for U16<O>
Auto Trait Implementations§
impl<O> Freeze for U16<O>
impl<O> RefUnwindSafe for U16<O>where
O: RefUnwindSafe,
impl<O> Send for U16<O>where
O: Send,
impl<O> Sync for U16<O>where
O: Sync,
impl<O> Unpin for U16<O>where
O: Unpin,
impl<O> UnwindSafe for U16<O>where
O: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 2 bytes