pub struct UnalignedU16(/* private fields */);Expand description
A u16 that has an alignment requirement of 1 byte, i.e. is unaligned
This is useful in packed structures and slices of data contained in variable-length arrays in VPP messages.
§Layout
UnalignedU16 is guaranteed to have the same layout and bit validity as u16.
They are also guaranteed to have the same size.
Implementations§
Source§impl UnalignedU16
impl UnalignedU16
Sourcepub const fn new(value: u16) -> Self
pub const fn new(value: u16) -> Self
Creates a u16 that has an alignment requirement of 1 byte, i.e. is unaligned.
Sourcepub const fn swap_bytes(self) -> Self
pub const fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
§Examples
let n = UnalignedU16::new(0x1234u16);
let m = n.swap_bytes();
assert_eq!(m, 0x3412);Sourcepub const fn to_be(self) -> Self
pub const fn to_be(self) -> Self
Converts self to big endian from the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
§Examples
let n = UnalignedU16::new(0x1Au16);
if cfg!(target_endian = "big") {
assert_eq!(n.to_be(), n)
} else {
assert_eq!(n.to_be(), n.swap_bytes())
}Sourcepub const fn from_be(x: Self) -> Self
pub const fn from_be(x: Self) -> Self
Converts an integer from big endian to the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
§Examples
let n = UnalignedU16::new(0x1Au16);
if cfg!(target_endian = "big") {
assert_eq!(UnalignedU16::from_be(n), n)
} else {
assert_eq!(UnalignedU16::from_be(n), n.swap_bytes())
}Trait Implementations§
Source§impl Add<u16> for UnalignedU16
impl Add<u16> for UnalignedU16
Source§impl Add for UnalignedU16
impl Add for UnalignedU16
Source§impl Clone for UnalignedU16
impl Clone for UnalignedU16
Source§fn clone(&self) -> UnalignedU16
fn clone(&self) -> UnalignedU16
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for UnalignedU16
impl Debug for UnalignedU16
Source§impl Display for UnalignedU16
impl Display for UnalignedU16
Source§impl Div<u16> for UnalignedU16
impl Div<u16> for UnalignedU16
Source§impl Div for UnalignedU16
impl Div for UnalignedU16
Source§impl From<UnalignedU16> for u16
impl From<UnalignedU16> for u16
Source§fn from(value: UnalignedU16) -> Self
fn from(value: UnalignedU16) -> Self
Converts to this type from the input type.
Source§impl From<u16> for UnalignedU16
impl From<u16> for UnalignedU16
Source§impl FromStr for UnalignedU16
impl FromStr for UnalignedU16
Source§impl Hash for UnalignedU16
impl Hash for UnalignedU16
Source§impl Mul<u16> for UnalignedU16
impl Mul<u16> for UnalignedU16
Source§impl Mul for UnalignedU16
impl Mul for UnalignedU16
Source§impl Ord for UnalignedU16
impl Ord for UnalignedU16
Source§fn cmp(&self, other: &UnalignedU16) -> Ordering
fn cmp(&self, other: &UnalignedU16) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq<u16> for UnalignedU16
impl PartialEq<u16> for UnalignedU16
Source§impl PartialEq for UnalignedU16
impl PartialEq for UnalignedU16
Source§impl PartialOrd<u16> for UnalignedU16
impl PartialOrd<u16> for UnalignedU16
Source§impl PartialOrd for UnalignedU16
impl PartialOrd for UnalignedU16
Source§impl Sub<u16> for UnalignedU16
impl Sub<u16> for UnalignedU16
Source§impl Sub for UnalignedU16
impl Sub for UnalignedU16
impl Copy for UnalignedU16
impl Eq for UnalignedU16
impl StructuralPartialEq for UnalignedU16
Auto Trait Implementations§
impl Freeze for UnalignedU16
impl RefUnwindSafe for UnalignedU16
impl Send for UnalignedU16
impl Sync for UnalignedU16
impl Unpin for UnalignedU16
impl UnsafeUnpin for UnalignedU16
impl UnwindSafe for UnalignedU16
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
Mutably borrows from an owned value. Read more