flatty_portable/
lib.rs

1#![no_std]
2
3mod bool_;
4mod float;
5mod impl_;
6mod int;
7#[cfg(test)]
8mod tests;
9
10use flatty_base::traits::Flat;
11
12/// Type that can be safely transfered between different machines.
13///
14/// # Safety
15///
16/// Implementing this trait must guarantee that `Self` has the same binary representation on any target platform this crate could be built for.
17pub unsafe trait Portable: Flat {}
18
19/// Trait for casting portable type to/from native counterparts.
20pub trait NativeCast: Portable + Copy {
21    type Native: Copy;
22    fn from_native(n: Self::Native) -> Self;
23    fn to_native(&self) -> Self::Native;
24}
25
26pub use bool_::Bool;
27pub use float::Float;
28pub use int::Int;
29
30/// Little-endian types.
31pub mod le {
32    use super::*;
33    pub use float::le::*;
34    pub use int::le::*;
35}
36
37/// Big-endian types.
38pub mod be {
39    use super::*;
40    pub use float::be::*;
41    pub use int::be::*;
42}
43
44pub mod traits {
45    pub use super::{NativeCast, Portable};
46}
47
48macro_rules! derive_display {
49    ($self:ty, $native:ty) => {
50        impl core::fmt::Debug for $self {
51            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
52                <$native as core::fmt::Debug>::fmt(&self.to_native(), f)
53            }
54        }
55        impl core::fmt::Display for $self {
56            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57                <$native as core::fmt::Display>::fmt(&self.to_native(), f)
58            }
59        }
60    };
61}
62
63pub(crate) use derive_display;