Skip to main content

pack1/
_u32.rs

1/// Bytes for a [`u32`], aligned to 1, big-endian.
2#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
3#[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))]
4#[repr(transparent)]
5pub struct U32BE([u8; 4]);
6impl U32BE {
7  /// Constructs the value from a standard [`u32`].
8  #[inline]
9  #[must_use]
10  pub const fn new(u: u32) -> Self {
11    Self(u.to_be_bytes())
12  }
13  /// Turns the value into a standard [`u32`].
14  #[inline]
15  #[must_use]
16  pub const fn get(self) -> u32 {
17    u32::from_be_bytes(self.0)
18  }
19}
20impl From<u32> for U32BE {
21  #[inline]
22  #[must_use]
23  fn from(value: u32) -> Self {
24    Self::new(value)
25  }
26}
27impl From<U32BE> for u32 {
28  #[inline]
29  #[must_use]
30  fn from(value: U32BE) -> Self {
31    value.get()
32  }
33}
34int_fmt!(U32BE, u32);
35
36/// Bytes for a [`u32`], aligned to 1, little-endian.
37#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
38#[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))]
39#[repr(transparent)]
40pub struct U32LE([u8; 4]);
41impl U32LE {
42  /// Constructs the value from a standard [`u32`].
43  #[inline]
44  #[must_use]
45  pub const fn new(u: u32) -> Self {
46    Self(u.to_le_bytes())
47  }
48  /// Turns the value into a standard [`u32`].
49  #[inline]
50  #[must_use]
51  pub const fn get(self) -> u32 {
52    u32::from_le_bytes(self.0)
53  }
54}
55impl From<u32> for U32LE {
56  #[inline]
57  #[must_use]
58  fn from(value: u32) -> Self {
59    Self::new(value)
60  }
61}
62impl From<U32LE> for u32 {
63  #[inline]
64  #[must_use]
65  fn from(value: U32LE) -> Self {
66    value.get()
67  }
68}
69int_fmt!(U32LE, u32);
70
71/// Bytes for a [`u32`], aligned to 1, native-endian.
72#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
73#[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable, bytemuck::Pod))]
74#[repr(transparent)]
75pub struct U32NE([u8; 4]);
76impl U32NE {
77  /// Constructs the value from a standard [`u32`].
78  #[inline]
79  #[must_use]
80  pub const fn new(u: u32) -> Self {
81    Self(u.to_ne_bytes())
82  }
83  /// Turns the value into a standard [`u32`].
84  #[inline]
85  #[must_use]
86  pub const fn get(self) -> u32 {
87    u32::from_ne_bytes(self.0)
88  }
89}
90impl From<u32> for U32NE {
91  #[inline]
92  #[must_use]
93  fn from(value: u32) -> Self {
94    Self::new(value)
95  }
96}
97impl From<U32NE> for u32 {
98  #[inline]
99  #[must_use]
100  fn from(value: U32NE) -> Self {
101    value.get()
102  }
103}
104int_fmt!(U32NE, u32);