abio 0.3.0

Safe abstractions for interfacing with the native operating system ABI.
Documentation
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Module for working with endian-aware byte sequences.
//!
//! # I/O Interface
//!
//! The [`ByteOrder`] trait, and this does not perform any actual I/O on files,
//! sockets or other related operating system resources. The methods provided by this
//! module operate on in-memory buffers with endianness in mind.
use core::fmt::Debug;
use core::hash::Hash;

use crate::integer::Integer;
use crate::source::Chunk;
use crate::{Error, Result};

/// Copies $size bytes from a number $n to a &mut [u8] $dst. $ty represents the
/// numeric type of $n and $which must be either to_be or to_le, depending on
/// which endianness one wants to use when writing to $dst.
///
/// This macro is only safe to call when $ty is a numeric type and $size ==
/// size_of::<$ty>() and where $dst is a &mut [u8].
macro_rules! unsafe_write_num_bytes {
    ($ty:ty, $size:expr, $value:expr, $dst:expr, $which:ident) => {{
        if $dst.len() < $size {
            Err($crate::Error::out_of_bounds($size, $dst.len()))
        } else {
            unsafe {
                // N.B. https://github.com/rust-lang/rust/issues/22776
                let bytes = *(&$value.$which() as *const _ as *const [u8; $size]);
                ::core::ptr::copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
                Ok(())
            }
        }
    }};
}

/// Reads a fixed size $integer with $endianness from a $bytes slice.
///
/// This macro takes the following arguments:
/// - $bytes:ident
/// - $integer:ty
/// - $endianness:tt
macro_rules! read_endian_bytes {
    ( $($bytes:ident, $integer:ty, $endianness:tt),* $(,)? ) => {
        $(
            match $crate::source::Chunk::from_bytes($bytes) {
                Ok(chunk) => Ok(<$integer>::$endianness(chunk.into_array())),
                Err(err) => Err(err),
            }
        )*
    }
}

/// An internal trait that encompasses common trait bounds for the [`ByteOrder`]
/// trait. This trait implements [`Sealed`], preventing downstream crates from
/// implementing this trait.
///
/// Implementing this trait outside of this crate is not supported as it contains the
/// `sealed::Sealed` trait, which is a private trait used to "seal" this trait from
/// being implemented outside of this crate.
// pub trait ByteOrderSealed =;

/// A trait to define the endianness, or byte order, of some contiguous region of
/// memory represented as a byte slice.
///
/// # Runtime Flexibility
///
/// This trait provides the ability to dynamically choose the required endianness.
/// This enables handling data that may be in big endian, little endian, or a mix of
/// both. This is particularly beneficial when the byte order cannot be determined
/// until runtime.
///
/// # Considerations
///
/// Most machines default to [`LittleEndian`] byte order. However, this isn't
/// universal, and some machines use [`BigEndian`] byte order. In addition, there's
/// [`NetworkEndian`] byte order (which is synonymous with big endian). Many
/// prevalent network protocols employ "network endian" byte order for serialization.
/// Hence, it's crucial to ensure the appropriate byte order is chosen for your
/// specific use-case.
pub trait Endianness: Clone + Copy + Debug + Eq + Hash + Ord + PartialEq + PartialOrd {
    /// Read an aligned [`u8`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_u8(bytes: &[u8]) -> Result<u8>;

    /// Read an aligned [`u16`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_u16(bytes: &[u8]) -> Result<u16>;

    /// Read an aligned [`u32`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_u32(bytes: &[u8]) -> Result<u32>;

    /// Read an aligned [`u64`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_u64(bytes: &[u8]) -> Result<u64>;

    /// Read an aligned [`u128`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_u128(bytes: &[u8]) -> Result<u128>;

    /// Read an aligned [`i8`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_i8(bytes: &[u8]) -> Result<i8>;

    /// Read an aligned [`i16`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_i16(bytes: &[u8]) -> Result<i16>;

    /// Read an aligned [`i32`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_i32(bytes: &[u8]) -> Result<i32>;

    /// Read an aligned [`i64`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails due to an unsufficient number of
    /// bytes in the buffer. The byte slice must contain at least `size_of::<T>()`
    /// bytes where `T` is the return type.
    fn read_i64(bytes: &[u8]) -> Result<i64>;

    /// Read an aligned [`i128`] from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the read operation fails. The
    fn read_i128(bytes: &[u8]) -> Result<i128>;

    /// Write an aligned [`u8`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_u8(buf: &mut [u8], value: u8) -> Result<()>;

    /// Write an aligned [`u16`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_u16(buf: &mut [u8], value: u16) -> Result<()>;

    /// Write an aligned [`u32`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_u32(buf: &mut [u8], value: u32) -> Result<()>;

    /// Write an aligned [`u64`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_u64(buf: &mut [u8], value: u64) -> Result<()>;

    /// Write an aligned [`u128`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_u128(buf: &mut [u8], value: u128) -> Result<()>;

    /// Write an aligned [`i8`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_i8(buf: &mut [u8], value: i8) -> Result<()>;

    /// Write an aligned [`i16`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_i16(buf: &mut [u8], value: i16) -> Result<()>;

    /// Write an aligned [`i32`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_i32(buf: &mut [u8], value: i32) -> Result<()>;

    /// Write an aligned [`i64`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_i64(buf: &mut [u8], value: i64) -> Result<()>;

    /// Write an aligned [`i128`] value into a mutable byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the write operation fails. The main source of error is
    /// when `buf` does not contain enough bytes to construct the type represented by
    /// `value`.
    fn write_i128(buf: &mut [u8], value: i128) -> Result<()>;
}

impl Endianness for LittleEndian {
    #[inline]
    fn read_u8(bytes: &[u8]) -> Result<u8> {
        read_endian_bytes!(bytes, u8, from_le_bytes)
    }

    #[inline]
    fn read_u16(bytes: &[u8]) -> Result<u16> {
        read_endian_bytes!(bytes, u16, from_le_bytes)
    }

    #[inline]
    fn read_u32(bytes: &[u8]) -> Result<u32> {
        read_endian_bytes!(bytes, u32, from_le_bytes)
    }

    #[inline]
    fn read_u64(bytes: &[u8]) -> Result<u64> {
        read_endian_bytes!(bytes, u64, from_le_bytes)
    }

    #[inline]
    fn read_u128(bytes: &[u8]) -> Result<u128> {
        read_endian_bytes!(bytes, u128, from_le_bytes)
    }

    #[inline]
    fn read_i8(bytes: &[u8]) -> Result<i8> {
        read_endian_bytes!(bytes, i8, from_le_bytes)
    }

    #[inline]
    fn read_i16(bytes: &[u8]) -> Result<i16> {
        read_endian_bytes!(bytes, i16, from_le_bytes)
    }

    #[inline]
    fn read_i32(bytes: &[u8]) -> Result<i32> {
        read_endian_bytes!(bytes, i32, from_le_bytes)
    }

    #[inline]
    fn read_i64(bytes: &[u8]) -> Result<i64> {
        read_endian_bytes!(bytes, i64, from_le_bytes)
    }

    #[inline]
    fn read_i128(bytes: &[u8]) -> Result<i128> {
        read_endian_bytes!(bytes, i128, from_le_bytes)
    }

    #[inline]
    fn write_u8(buf: &mut [u8], value: u8) -> Result<()> {
        unsafe_write_num_bytes!(u8, 1, value, buf, to_le)
    }

    #[inline]
    fn write_u16(buf: &mut [u8], value: u16) -> Result<()> {
        unsafe_write_num_bytes!(u16, 2, value, buf, to_le)
    }

    #[inline]
    fn write_u32(buf: &mut [u8], value: u32) -> Result<()> {
        if buf.len() < 4 {
            Err(Error::out_of_bounds(4, buf.len()))
        } else {
            unsafe {
                // let bytes = *(&value.to_le() as *const _ as *const [u8; 4]);
                let src = value.to_le().as_ptr().cast::<u8>();
                ::core::ptr::copy_nonoverlapping(src, buf.as_mut_ptr(), 4);
                Ok(())
            }
        }
    }

    #[inline]
    fn write_u64(buf: &mut [u8], value: u64) -> Result<()> {
        unsafe_write_num_bytes!(u64, 8, value, buf, to_le)
    }

    #[inline]
    fn write_u128(buf: &mut [u8], value: u128) -> Result<()> {
        unsafe_write_num_bytes!(u128, 16, value, buf, to_le)
    }

    #[inline]
    fn write_i8(buf: &mut [u8], value: i8) -> Result<()> {
        unsafe_write_num_bytes!(i8, 1, value, buf, to_le)
    }

    #[inline]
    fn write_i16(buf: &mut [u8], value: i16) -> Result<()> {
        unsafe_write_num_bytes!(i16, 2, value, buf, to_le)
    }

    #[inline]
    fn write_i32(buf: &mut [u8], value: i32) -> Result<()> {
        unsafe_write_num_bytes!(i32, 4, value, buf, to_le)
    }

    #[inline]
    fn write_i64(buf: &mut [u8], value: i64) -> Result<()> {
        unsafe_write_num_bytes!(i64, 8, value, buf, to_le)
    }

    #[inline]
    fn write_i128(buf: &mut [u8], value: i128) -> Result<()> {
        unsafe_write_num_bytes!(i128, 16, value, buf, to_le)
    }
}

impl Endianness for BigEndian {
    #[inline]
    fn read_u8(bytes: &[u8]) -> Result<u8> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(u8::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_u16(bytes: &[u8]) -> Result<u16> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(u16::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_u32(bytes: &[u8]) -> Result<u32> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(u32::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_u64(bytes: &[u8]) -> Result<u64> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(u64::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_u128(bytes: &[u8]) -> Result<u128> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(u128::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_i8(bytes: &[u8]) -> Result<i8> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(i8::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_i16(bytes: &[u8]) -> Result<i16> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(i16::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_i32(bytes: &[u8]) -> Result<i32> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(i32::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_i64(bytes: &[u8]) -> Result<i64> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(i64::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn read_i128(bytes: &[u8]) -> Result<i128> {
        match Chunk::from_bytes(bytes) {
            Ok(chunk) => Ok(i128::from_be_bytes(chunk.into_array())),
            Err(err) => Err(err),
        }
    }

    #[inline]
    fn write_u8(buf: &mut [u8], value: u8) -> Result<()> {
        unsafe_write_num_bytes!(u8, 1, value, buf, to_be)
    }

    #[inline]
    fn write_u16(buf: &mut [u8], value: u16) -> Result<()> {
        unsafe_write_num_bytes!(u16, 2, value, buf, to_be)
    }

    #[inline]
    fn write_u32(buf: &mut [u8], value: u32) -> Result<()> {
        unsafe_write_num_bytes!(u32, 4, value, buf, to_be)
    }

    #[inline]
    fn write_u64(buf: &mut [u8], value: u64) -> Result<()> {
        unsafe_write_num_bytes!(u64, 8, value, buf, to_be)
    }

    #[inline]
    fn write_u128(buf: &mut [u8], value: u128) -> Result<()> {
        unsafe_write_num_bytes!(u128, 16, value, buf, to_be)
    }

    #[inline]
    fn write_i8(buf: &mut [u8], value: i8) -> Result<()> {
        unsafe_write_num_bytes!(i8, 1, value, buf, to_be)
    }

    #[inline]
    fn write_i16(buf: &mut [u8], value: i16) -> Result<()> {
        unsafe_write_num_bytes!(i16, 2, value, buf, to_be)
    }

    #[inline]
    fn write_i32(buf: &mut [u8], value: i32) -> Result<()> {
        unsafe_write_num_bytes!(i32, 4, value, buf, to_be)
    }

    #[inline]
    fn write_i64(buf: &mut [u8], value: i64) -> Result<()> {
        unsafe_write_num_bytes!(i64, 8, value, buf, to_be)
    }

    #[inline]
    fn write_i128(buf: &mut [u8], value: i128) -> Result<()> {
        unsafe_write_num_bytes!(i128, 16, value, buf, to_be)
    }
}

/// Little endian byte order serialization.
///
/// This is simply a type constructor that allows implementing the [`ByteOrder`]
/// trait for little endian data.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LittleEndian;

/// Type alias for [`LittleEndian`].
pub type LE = LittleEndian;

/// Big endian byte order serialization.
///
/// This is simply a type constructor that allows implementing the [`ByteOrder`]
/// trait for big endian data.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct BigEndian;

/// Type alias for [`BigEndian`].
pub type BE = BigEndian;

/// Type alias for this platform's native endian byte order.
#[cfg(target_endian = "big")]
pub type NativeEndian = BigEndian;

/// Type alias for this platform's native endian byte order.
#[cfg(target_endian = "little")]
pub type NativeEndian = LittleEndian;

/// .
pub const fn little_endian_codec() -> Endian {
    Endian::Little
}

/// Byte order serialization variant.
///
/// The [`Default`] implementation is calculated at compile time using the
/// `target_endian` cfg attribute value.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Endian {
    /// Big endian byte order.
    Big,
    /// Little endian byte order.
    Little,
}

impl Endian {
    /// Returns `true` if this instance represents [big endian][Big] byte
    /// order serialization.
    ///
    /// [Big]: Endian::Big
    #[must_use]
    #[doc(alias = "is_be")]
    #[inline]
    pub const fn is_big_endian(&self) -> bool {
        matches!(self, Self::Big)
    }

    /// Returns `true` if this instance represents [little endian][Little] byte
    /// order serialization.
    ///
    /// [Little]: Endian::Little
    #[must_use]
    #[doc(alias = "is_le")]
    #[inline]
    pub const fn is_little_endian(&self) -> bool {
        matches!(self, Self::Little)
    }
}

impl From<BigEndian> for Endian {
    #[inline]
    fn from(_: BigEndian) -> Endian {
        Endian::Big
    }
}

impl From<LittleEndian> for Endian {
    #[inline]
    fn from(_: LittleEndian) -> Endian {
        Endian::Little
    }
}