1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
macro_rules! unaligned_int {
(
$(
$( #[$docs:meta] )*
@[repr($repr:ty, $signedness:ident)]
$vis:vis struct $name:ident([u8; $num_bytes:literal])
);* $(;)?
) => {
$(
$( #[$docs] )*
#[derive(
::core::marker::Copy,
::core::clone::Clone,
::core::cmp::PartialEq,
::core::cmp::Eq,
)]
$vis struct $name([::core::primitive::u8; $num_bytes]);
unaligned_int!(
@impl
$( #[$docs] )*
@[repr($repr, $signedness)]
$vis struct $name([u8; $num_bytes])
);
impl $name {
/// The amount of bits required by this integer type.
pub const BITS: ::core::primitive::u32 = $num_bytes * 8_u32;
/// Returns the index position of the most significant byte.
#[inline]
#[allow(dead_code)] // Note: not used by power-of-two sized ints atm
pub(crate) const fn msb_pos() -> ::core::primitive::usize {
if ::core::cfg!(target_endian = "big") {
0_usize
} else {
$num_bytes - 1_usize
}
}
}
impl $name {
/// Returns the integer value as a byte array in native-endian order.
#[inline]
pub const fn to_ne_bytes(
self,
) -> [::core::primitive::u8; ::core::mem::size_of::<Self>()] {
self.0
}
/// Returns the integer value as a byte array in little-endian order.
#[inline]
pub fn to_le_bytes(self) -> [::core::primitive::u8; ::core::mem::size_of::<Self>()] {
$crate::utils::ne_bytes_to_le(self.to_ne_bytes())
}
/// Returns the integer value as a byte array in big-endian order.
#[inline]
pub fn to_be_bytes(self) -> [::core::primitive::u8; ::core::mem::size_of::<Self>()] {
$crate::utils::ne_bytes_to_be(self.to_ne_bytes())
}
/// Creates an unaligned signed integer from the given bytes in native-endian order.
#[inline]
pub const fn from_ne_bytes(
bytes: [::core::primitive::u8; ::core::mem::size_of::<Self>()],
) -> Self {
Self(bytes)
}
/// Creates an unaligned signed integer from the given bytes in little-endian order.
#[inline]
pub fn from_le_bytes(
bytes: [::core::primitive::u8; ::core::mem::size_of::<Self>()],
) -> Self {
Self::from_ne_bytes($crate::utils::le_bytes_to_ne(bytes))
}
/// Creates an unaligned signed integer from the given bytes in big-endian order.
#[inline]
pub fn from_be_bytes(
bytes: [::core::primitive::u8; ::core::mem::size_of::<Self>()],
) -> Self {
Self::from_ne_bytes($crate::utils::be_bytes_to_ne(bytes))
}
}
impl ::core::default::Default for $name {
#[inline]
fn default() -> Self {
Self([0x00_u8; ::core::mem::size_of::<Self>()])
}
}
impl ::core::cmp::PartialOrd for $name {
#[inline]
fn partial_cmp(&self, other: &Self) -> ::core::option::Option<::core::cmp::Ordering> {
<$repr as ::core::cmp::PartialOrd>::partial_cmp(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
#[inline]
fn lt(&self, other: &Self) -> ::core::primitive::bool {
<$repr as ::core::cmp::PartialOrd>::lt(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
#[inline]
fn le(&self, other: &Self) -> ::core::primitive::bool {
<$repr as ::core::cmp::PartialOrd>::le(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
#[inline]
fn gt(&self, other: &Self) -> ::core::primitive::bool {
<$repr as ::core::cmp::PartialOrd>::gt(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
#[inline]
fn ge(&self, other: &Self) -> ::core::primitive::bool {
<$repr as ::core::cmp::PartialOrd>::ge(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
}
impl ::core::cmp::Ord for $name {
#[inline]
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
<$repr as ::core::cmp::Ord>::cmp(
&<$repr as ::core::convert::From<Self>>::from(*self),
&<$repr as ::core::convert::From<Self>>::from(*other),
)
}
}
impl ::core::hash::Hash for $name {
#[inline]
fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
<$repr as ::core::hash::Hash>::hash(
&<$repr as ::core::convert::From<Self>>::from(*self),
state,
)
}
}
impl ::core::fmt::Debug for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::Debug>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::Display for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::Display>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::Binary for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::Binary>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::Octal for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::Octal>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::LowerHex for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::LowerHex>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::UpperHex for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::UpperHex>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::LowerExp for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::LowerExp>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
impl ::core::fmt::UpperExp for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
<$repr as ::core::fmt::UpperExp>::fmt(
&<$repr as ::core::convert::From<Self>>::from(*self),
f,
)
}
}
)*
};
(
@impl
$( #[$docs:meta] )*
@[repr($repr:ty, unsigned)]
$vis:vis struct $name:ident([u8; $num_bytes:literal])
) => {
impl $name {
/// The smallest value that can be represented by this integer type.
pub const MIN: Self = Self::from_ne_bytes([0x00_u8; $num_bytes]);
/// The largest value that can be represented by this integer type.
pub const MAX: Self = Self::from_ne_bytes([0xFF_u8; $num_bytes]);
}
impl $crate::UnalignedInteger for $name {
type Repr = $repr;
#[inline]
fn sign_ext_byte(self) -> ::core::primitive::u8 {
0x00_u8
}
}
};
(
@impl
$( #[$docs:meta] )*
@[repr($repr:ty, signed)]
$vis:vis struct $name:ident([u8; $num_bytes:literal])
) => {
impl $name {
/// The smallest value that can be represented by this integer type.
pub const MIN: Self = {
let mut bytes = [0x00_u8; $num_bytes];
bytes[Self::msb_pos()] = 0x80_u8;
Self(bytes)
};
/// The largest value that can be represented by this integer type.
pub const MAX: Self = {
let mut bytes = [0xFF_u8; $num_bytes];
bytes[Self::msb_pos()] = 0x7F_u8;
Self(bytes)
};
/// Returns `true` if `self` is positive.
#[inline]
pub(crate) const fn is_positive(self) -> ::core::primitive::bool {
(self.0[Self::msb_pos()] & 0x80_u8) == 0x00_u8
}
}
impl $crate::UnalignedInteger for $name {
type Repr = $repr;
#[inline]
fn sign_ext_byte(self) -> ::core::primitive::u8 {
$crate::utils::sign_ext_byte(self.is_positive())
}
}
}
}
unaligned_int! {
/// 16-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u16, unsigned)]
pub struct U16([u8; 2]);
/// 16-bit signed integer with alignment of 1.
@[repr(::core::primitive::i16, signed)]
pub struct I16([u8; 2]);
/// 24-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u32, unsigned)]
pub struct U24([u8; 3]);
/// 24-bit signed integer with alignment of 1.
@[repr(::core::primitive::i32, signed)]
pub struct I24([u8; 3]);
/// 32-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u32, unsigned)]
pub struct U32([u8; 4]);
/// 32-bit signed integer with alignment of 1.
@[repr(::core::primitive::i32, signed)]
pub struct I32([u8; 4]);
/// 40-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u64, unsigned)]
pub struct U40([u8; 5]);
/// 40-bit signed integer with alignment of 1.
@[repr(::core::primitive::i64, signed)]
pub struct I40([u8; 5]);
/// 48-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u64, unsigned)]
pub struct U48([u8; 6]);
/// 48-bit signed integer with alignment of 1.
@[repr(::core::primitive::i64, signed)]
pub struct I48([u8; 6]);
/// 56-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u64, unsigned)]
pub struct U56([u8; 7]);
/// 56-bit signed integer with alignment of 1.
@[repr(::core::primitive::i64, signed)]
pub struct I56([u8; 7]);
/// 64-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u64, unsigned)]
pub struct U64([u8; 8]);
/// 64-bit signed integer with alignment of 1.
@[repr(::core::primitive::i64, signed)]
pub struct I64([u8; 8]);
/// 72-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U72([u8; 9]);
/// 72-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I72([u8; 9]);
/// 80-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U80([u8; 10]);
/// 80-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I80([u8; 10]);
/// 88-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U88([u8; 11]);
/// 88-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I88([u8; 11]);
/// 96-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U96([u8; 12]);
/// 96-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I96([u8; 12]);
/// 104-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U104([u8; 13]);
/// 104-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I104([u8; 13]);
/// 112-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U112([u8; 14]);
/// 112-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I112([u8; 14]);
/// 120-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U120([u8; 15]);
/// 120-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I120([u8; 15]);
/// 128-bit unsigned integer with alignment of 1.
@[repr(::core::primitive::u128, unsigned)]
pub struct U128([u8; 16]);
/// 128-bit signed integer with alignment of 1.
@[repr(::core::primitive::i128, signed)]
pub struct I128([u8; 16]);
}