pub struct UnalignedU32(/* private fields */);Expand description
A u32 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
UnalignedU32 is guaranteed to have the same layout and bit validity as u32.
They are also guaranteed to have the same size.
Implementations§
Source§impl UnalignedU32
impl UnalignedU32
Sourcepub const fn new(value: u32) -> Self
pub const fn new(value: u32) -> Self
Creates a u32 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 = UnalignedU32::new(0x12345678u32);
let m = n.swap_bytes();
assert_eq!(m, 0x78563412);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 = UnalignedU32::new(0x1Au32);
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 = UnalignedU32::new(0x1Au32);
if cfg!(target_endian = "big") {
assert_eq!(UnalignedU32::from_be(n), n)
} else {
assert_eq!(UnalignedU32::from_be(n), n.swap_bytes())
}Trait Implementations§
Source§impl Add<u32> for UnalignedU32
impl Add<u32> for UnalignedU32
Source§impl Add for UnalignedU32
impl Add for UnalignedU32
Source§impl Clone for UnalignedU32
impl Clone for UnalignedU32
Source§fn clone(&self) -> UnalignedU32
fn clone(&self) -> UnalignedU32
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 UnalignedU32
impl Debug for UnalignedU32
Source§impl Display for UnalignedU32
impl Display for UnalignedU32
Source§impl Div<u32> for UnalignedU32
impl Div<u32> for UnalignedU32
Source§impl Div for UnalignedU32
impl Div for UnalignedU32
Source§impl From<UnalignedU32> for u32
impl From<UnalignedU32> for u32
Source§fn from(value: UnalignedU32) -> Self
fn from(value: UnalignedU32) -> Self
Converts to this type from the input type.
Source§impl From<u32> for UnalignedU32
impl From<u32> for UnalignedU32
Source§impl FromStr for UnalignedU32
impl FromStr for UnalignedU32
Source§impl Hash for UnalignedU32
impl Hash for UnalignedU32
Source§impl Mul<u32> for UnalignedU32
impl Mul<u32> for UnalignedU32
Source§impl Mul for UnalignedU32
impl Mul for UnalignedU32
Source§impl Ord for UnalignedU32
impl Ord for UnalignedU32
Source§fn cmp(&self, other: &UnalignedU32) -> Ordering
fn cmp(&self, other: &UnalignedU32) -> 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<u32> for UnalignedU32
impl PartialEq<u32> for UnalignedU32
Source§impl PartialEq for UnalignedU32
impl PartialEq for UnalignedU32
Source§impl PartialOrd<u32> for UnalignedU32
impl PartialOrd<u32> for UnalignedU32
Source§impl PartialOrd for UnalignedU32
impl PartialOrd for UnalignedU32
Source§impl Sub<u32> for UnalignedU32
impl Sub<u32> for UnalignedU32
Source§impl Sub for UnalignedU32
impl Sub for UnalignedU32
impl Copy for UnalignedU32
impl Eq for UnalignedU32
impl StructuralPartialEq for UnalignedU32
Auto Trait Implementations§
impl Freeze for UnalignedU32
impl RefUnwindSafe for UnalignedU32
impl Send for UnalignedU32
impl Sync for UnalignedU32
impl Unpin for UnalignedU32
impl UnsafeUnpin for UnalignedU32
impl UnwindSafe for UnalignedU32
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