[go: up one dir, main page]

pack1/
lib.rs

1#![no_std]
2#![warn(missing_docs)]
3#![forbid(unsafe_code)]
4#![warn(clippy::missing_const_for_fn)]
5#![warn(clippy::missing_inline_in_public_items)]
6
7//! Byte array newtypes for different primitive types.
8//!
9//! Because they're newtypes of byte arrays, they always have alignment 1.
10//!
11//! Each type has `new` and `get` functions, as well as [`From`] impls. The
12//! `new` and `get` functions are `const fn` with int types, but not yet with
13//! floating types.
14//!
15//! The intended usage of this crate is that you can use these these types in a
16//! `repr(C)` struct, along with manual padding, and then have each field at the
17//! exact byte offset you desire (and with the necessary endian-ness), without
18//! the normal `repr(packed)` problem that it interferes with references.
19
20macro_rules! int_fmt {
21  ($i:ident, $base:ty) => {
22    impl core::fmt::Binary for $i {
23      #[inline]
24      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        core::fmt::Binary::fmt(&<$base>::from(*self), f)
26      }
27    }
28    impl core::fmt::Debug for $i {
29      #[inline]
30      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31        core::fmt::Debug::fmt(&<$base>::from(*self), f)
32      }
33    }
34    impl core::fmt::Display for $i {
35      #[inline]
36      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        core::fmt::Display::fmt(&<$base>::from(*self), f)
38      }
39    }
40    impl core::fmt::LowerExp for $i {
41      #[inline]
42      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        core::fmt::LowerExp::fmt(&<$base>::from(*self), f)
44      }
45    }
46    impl core::fmt::LowerHex for $i {
47      #[inline]
48      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49        core::fmt::LowerHex::fmt(&<$base>::from(*self), f)
50      }
51    }
52    impl core::fmt::Octal for $i {
53      #[inline]
54      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
55        core::fmt::Octal::fmt(&<$base>::from(*self), f)
56      }
57    }
58    impl core::fmt::UpperExp for $i {
59      #[inline]
60      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
61        core::fmt::UpperExp::fmt(&<$base>::from(*self), f)
62      }
63    }
64    impl core::fmt::UpperHex for $i {
65      #[inline]
66      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        core::fmt::UpperHex::fmt(&<$base>::from(*self), f)
68      }
69    }
70  };
71}
72
73macro_rules! float_fmt {
74  ($i:ident, $base:ty) => {
75    impl core::fmt::Debug for $i {
76      #[inline]
77      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78        core::fmt::Debug::fmt(&<$base>::from(*self), f)
79      }
80    }
81    impl core::fmt::Display for $i {
82      #[inline]
83      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
84        core::fmt::Display::fmt(&<$base>::from(*self), f)
85      }
86    }
87    impl core::fmt::LowerExp for $i {
88      #[inline]
89      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
90        core::fmt::LowerExp::fmt(&<$base>::from(*self), f)
91      }
92    }
93    impl core::fmt::UpperExp for $i {
94      #[inline]
95      fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
96        core::fmt::UpperExp::fmt(&<$base>::from(*self), f)
97      }
98    }
99  };
100}
101
102mod _f32;
103mod _f64;
104mod _i16;
105mod _i32;
106mod _i64;
107mod _u16;
108mod _u32;
109mod _u64;
110
111pub use self::{_f32::*, _f64::*, _i16::*, _i32::*, _i64::*, _u16::*, _u32::*, _u64::*};