zerocopy::byteorder

Struct U16

Source
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>

Source

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.

Source

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.

Source

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>

Source

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.

Source

pub fn get(self) -> u16

Returns the value as a primitive type, possibly performing an endianness swap to guarantee that the return value has the endianness of the native platform.

Source

pub fn set(&mut self, n: u16)

Updates the value in place as a primitive type, possibly performing an endianness swap to guarantee that the stored value has the endianness O.

Trait Implementations§

Source§

impl<O: ByteOrder> Add for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: U16<O>) -> U16<O>

Performs the + operation. Read more
Source§

impl<O: ByteOrder> AddAssign for U16<O>

Source§

fn add_assign(&mut self, rhs: U16<O>)

Performs the += operation. Read more
Source§

impl<O> AsBytes for U16<O>
where [u8; 2]: AsBytes, PhantomData<O>: AsBytes,

Source§

fn as_bytes(&self) -> &[u8]

Gets the bytes of this value. Read more
Source§

fn as_bytes_mut(&mut self) -> &mut [u8]
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
Source§

fn write_to(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to bytes. Read more
Source§

fn write_to_prefix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the prefix of bytes. Read more
Source§

fn write_to_suffix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the suffix of bytes. Read more
Source§

impl<O: ByteOrder> AsMut<[u8; 2]> for U16<O>

Source§

fn as_mut(&mut self) -> &mut [u8; 2]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<O: ByteOrder> AsRef<[u8; 2]> for U16<O>

Source§

fn as_ref(&self) -> &[u8; 2]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<O: ByteOrder> Binary for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: ByteOrder> BitAnd for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: U16<O>) -> U16<O>

Performs the & operation. Read more
Source§

impl<O: ByteOrder> BitAndAssign for U16<O>

Source§

fn bitand_assign(&mut self, rhs: U16<O>)

Performs the &= operation. Read more
Source§

impl<O: ByteOrder> BitOr for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: U16<O>) -> U16<O>

Performs the | operation. Read more
Source§

impl<O: ByteOrder> BitOrAssign for U16<O>

Source§

fn bitor_assign(&mut self, rhs: U16<O>)

Performs the |= operation. Read more
Source§

impl<O: ByteOrder> BitXor for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: U16<O>) -> U16<O>

Performs the ^ operation. Read more
Source§

impl<O: ByteOrder> BitXorAssign for U16<O>

Source§

fn bitxor_assign(&mut self, rhs: U16<O>)

Performs the ^= operation. Read more
Source§

impl<O: Clone> Clone for U16<O>

Source§

fn clone(&self) -> U16<O>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<O: ByteOrder> Debug for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O> Default for U16<O>

Source§

fn default() -> U16<O>

Returns the “default value” for a type. Read more
Source§

impl<O: ByteOrder> Display for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: ByteOrder> Div for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: U16<O>) -> U16<O>

Performs the / operation. Read more
Source§

impl<O: ByteOrder> DivAssign for U16<O>

Source§

fn div_assign(&mut self, rhs: U16<O>)

Performs the /= operation. Read more
Source§

impl<O: ByteOrder> From<[u8; 2]> for U16<O>

Source§

fn from(bytes: [u8; 2]) -> U16<O>

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for [u8; 2]

Source§

fn from(x: U16<O>) -> [u8; 2]

Converts to this type from the input type.
Source§

impl<O: ByteOrder, P: ByteOrder> From<U16<O>> for U128<P>

Source§

fn from(x: U16<O>) -> U128<P>

Converts to this type from the input type.
Source§

impl<O: ByteOrder, P: ByteOrder> From<U16<O>> for U32<P>

Source§

fn from(x: U16<O>) -> U32<P>

Converts to this type from the input type.
Source§

impl<O: ByteOrder, P: ByteOrder> From<U16<O>> for U64<P>

Source§

fn from(x: U16<O>) -> U64<P>

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for u128

Source§

fn from(x: U16<O>) -> u128

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for u16

Source§

fn from(x: U16<O>) -> u16

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for u32

Source§

fn from(x: U16<O>) -> u32

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for u64

Source§

fn from(x: U16<O>) -> u64

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<U16<O>> for usize

Source§

fn from(x: U16<O>) -> usize

Converts to this type from the input type.
Source§

impl<O: ByteOrder> From<u16> for U16<O>

Source§

fn from(x: u16) -> U16<O>

Converts to this type from the input type.
Source§

impl<O> FromBytes for U16<O>

Source§

fn ref_from(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the given bytes as a &Self without copying. Read more
Source§

fn ref_from_prefix(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the prefix of the given bytes as a &Self without copying. Read more
Source§

fn ref_from_suffix(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the suffix of the given bytes as a &Self without copying. Read more
Source§

fn mut_from(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the given bytes as a &mut Self without copying. Read more
Source§

fn mut_from_prefix(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the prefix of the given bytes as a &mut Self without copying. Read more
Source§

fn mut_from_suffix(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the suffix of the given bytes as a &mut Self without copying. Read more
Source§

fn slice_from(bytes: &[u8]) -> Option<&[Self]>
where Self: Sized,

Interprets the given bytes as a &[Self] without copying. Read more
Source§

fn slice_from_prefix(bytes: &[u8], count: usize) -> Option<(&[Self], &[u8])>
where Self: Sized,

Interprets the prefix of the given bytes as a &[Self] with length equal to count without copying. Read more
Source§

fn slice_from_suffix(bytes: &[u8], count: usize) -> Option<(&[u8], &[Self])>
where Self: Sized,

Interprets the suffix of the given bytes as a &[Self] with length equal to count without copying. Read more
Source§

fn mut_slice_from(bytes: &mut [u8]) -> Option<&mut [Self]>
where Self: Sized + AsBytes,

Interprets the given bytes as a &mut [Self] without copying. Read more
Source§

fn mut_slice_from_prefix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [Self], &mut [u8])>
where Self: Sized + AsBytes,

Interprets the prefix of the given bytes as a &mut [Self] with length equal to count without copying. Read more
Source§

fn mut_slice_from_suffix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [u8], &mut [Self])>
where Self: Sized + AsBytes,

Interprets the suffix of the given bytes as a &mut [Self] with length equal to count without copying. Read more
Source§

fn read_from(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from bytes. Read more
Source§

fn read_from_prefix(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the prefix of bytes. Read more
Source§

fn read_from_suffix(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the suffix of bytes. Read more
Source§

impl<O> FromZeroes for U16<O>

Source§

fn zero(&mut self)

Overwrites self with zeroes. Read more
Source§

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
Source§

impl<O: Hash> Hash for U16<O>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<O: ByteOrder> LowerHex for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: ByteOrder> Mul for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: U16<O>) -> U16<O>

Performs the * operation. Read more
Source§

impl<O: ByteOrder> MulAssign for U16<O>

Source§

fn mul_assign(&mut self, rhs: U16<O>)

Performs the *= operation. Read more
Source§

impl<O> Not for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the ! operator.
Source§

fn not(self) -> U16<O>

Performs the unary ! operation. Read more
Source§

impl<O: ByteOrder> Octal for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: ByteOrder> PartialEq<[u8; 2]> for U16<O>

Source§

fn eq(&self, other: &[u8; 2]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<O: ByteOrder> PartialEq<U16<O>> for [u8; 2]

Source§

fn eq(&self, other: &U16<O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<O: PartialEq> PartialEq for U16<O>

Source§

fn eq(&self, other: &U16<O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<O: ByteOrder> Rem for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: U16<O>) -> U16<O>

Performs the % operation. Read more
Source§

impl<O: ByteOrder> RemAssign for U16<O>

Source§

fn rem_assign(&mut self, rhs: U16<O>)

Performs the %= operation. Read more
Source§

impl<O: ByteOrder> Shl for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: U16<O>) -> U16<O>

Performs the << operation. Read more
Source§

impl<O: ByteOrder> ShlAssign for U16<O>

Source§

fn shl_assign(&mut self, rhs: U16<O>)

Performs the <<= operation. Read more
Source§

impl<O: ByteOrder> Shr for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: U16<O>) -> U16<O>

Performs the >> operation. Read more
Source§

impl<O: ByteOrder> ShrAssign for U16<O>

Source§

fn shr_assign(&mut self, rhs: U16<O>)

Performs the >>= operation. Read more
Source§

impl<O: ByteOrder> Sub for U16<O>

Source§

type Output = U16<O>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: U16<O>) -> U16<O>

Performs the - operation. Read more
Source§

impl<O: ByteOrder> SubAssign for U16<O>

Source§

fn sub_assign(&mut self, rhs: U16<O>)

Performs the -= operation. Read more
Source§

impl<O: ByteOrder, P: ByteOrder> TryFrom<U128<P>> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: U128<P>) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder, P: ByteOrder> TryFrom<U32<P>> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: U32<P>) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder, P: ByteOrder> TryFrom<U64<P>> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: U64<P>) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder> TryFrom<u128> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: u128) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder> TryFrom<u32> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: u32) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder> TryFrom<u64> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: u64) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder> TryFrom<usize> for U16<O>

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(x: usize) -> Result<U16<O>, TryFromIntError>

Performs the conversion.
Source§

impl<O: ByteOrder> UpperHex for U16<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: Copy> Copy for U16<O>

Source§

impl<O: Eq> Eq for U16<O>

Source§

impl<O> StructuralPartialEq for U16<O>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.