Skip to main content

rkyv/
primitive.rs

1//! Definitions of archived primitives and type aliases based on enabled
2//! features.
3
4// Unaligned big-endian
5#[cfg(all(feature = "unaligned", feature = "big_endian"))]
6use crate::rend::unaligned::{
7    char_ube, f32_ube, f64_ube, i128_ube, i16_ube, i32_ube, i64_ube, u128_ube,
8    u16_ube, u32_ube, u64_ube, NonZeroI128_ube, NonZeroI16_ube, NonZeroI32_ube,
9    NonZeroI64_ube, NonZeroU128_ube, NonZeroU16_ube, NonZeroU32_ube,
10    NonZeroU64_ube,
11};
12// Unaligned little-endian
13#[cfg(all(feature = "unaligned", not(feature = "big_endian")))]
14use crate::rend::unaligned::{
15    char_ule, f32_ule, f64_ule, i128_ule, i16_ule, i32_ule, i64_ule, u128_ule,
16    u16_ule, u32_ule, u64_ule, NonZeroI128_ule, NonZeroI16_ule, NonZeroI32_ule,
17    NonZeroI64_ule, NonZeroU128_ule, NonZeroU16_ule, NonZeroU32_ule,
18    NonZeroU64_ule,
19};
20// Aligned big-endian
21#[cfg(all(not(feature = "unaligned"), feature = "big_endian"))]
22use crate::rend::{
23    char_be, f32_be, f64_be, i128_be, i16_be, i32_be, i64_be, u128_be, u16_be,
24    u32_be, u64_be, NonZeroI128_be, NonZeroI16_be, NonZeroI32_be,
25    NonZeroI64_be, NonZeroU128_be, NonZeroU16_be, NonZeroU32_be, NonZeroU64_be,
26};
27// Aligned little-endian
28#[cfg(all(not(feature = "unaligned"), not(feature = "big_endian")))]
29use crate::rend::{
30    char_le, f32_le, f64_le, i128_le, i16_le, i32_le, i64_le, u128_le, u16_le,
31    u32_le, u64_le, NonZeroI128_le, NonZeroI16_le, NonZeroI32_le,
32    NonZeroI64_le, NonZeroU128_le, NonZeroU16_le, NonZeroU32_le, NonZeroU64_le,
33};
34
35#[rustfmt::skip]
36macro_rules! define_archived_type_alias {
37    ($archived:ident: $name:ident, $ty:ty) => {
38        #[doc = concat!(
39            "The archived version of `",
40            stringify!($name),
41            "`.",
42        )]
43        pub type $archived = $ty;
44    };
45}
46
47macro_rules! define_archived_primitive {
48    ($archived:ident: $name:ident, $le:ty, $be:ty) => {
49        #[cfg(not(feature = "big_endian"))]
50        define_archived_type_alias!($archived: $name, $le);
51        #[cfg(feature = "big_endian")]
52        define_archived_type_alias!($archived: $name, $be);
53    }
54}
55
56macro_rules! define_multibyte_primitive {
57    ($archived:ident: $name:ident, $le:ty, $ule:ty, $be:ty, $ube:ty) => {
58        #[cfg(not(feature = "unaligned"))]
59        define_archived_primitive!($archived: $name, $le, $be);
60        #[cfg(feature = "unaligned")]
61        define_archived_primitive!($archived: $name, $ule, $ube);
62    };
63}
64
65macro_rules! define_multibyte_primitives {
66    (
67        $($archived:ident: $name:ident, $le:ty, $ule:ty, $be:ty, $ube:ty);*
68        $(;)?
69    ) => {
70        $(
71            define_multibyte_primitive!($archived: $name, $le, $ule, $be, $ube);
72        )*
73    }
74}
75
76define_multibyte_primitives! {
77    ArchivedI16: i16, i16_le, i16_ule, i16_be, i16_ube;
78    ArchivedI32: i32, i32_le, i32_ule, i32_be, i32_ube;
79    ArchivedI64: i64, i64_le, i64_ule, i64_be, i64_ube;
80    ArchivedI128: i128, i128_le, i128_ule, i128_be, i128_ube;
81    ArchivedU16: u16, u16_le, u16_ule, u16_be, u16_ube;
82    ArchivedU32: u32, u32_le, u32_ule, u32_be, u32_ube;
83    ArchivedU64: u64, u64_le, u64_ule, u64_be, u64_ube;
84    ArchivedU128: u128, u128_le, u128_ule, u128_be, u128_ube;
85    ArchivedF32: f32, f32_le, f32_ule, f32_be, f32_ube;
86    ArchivedF64: f64, f64_le, f64_ule, f64_be, f64_ube;
87    ArchivedChar: char, char_le, char_ule, char_be, char_ube;
88}
89
90/// The native type that `isize` is converted to for archiving.
91///
92/// This will be `i16`, `i32`, or `i64` when the `pointer_width_16`,
93/// `pointer_width_32`, or `pointer_width_64` features are enabled,
94/// respectively. With no pointer width features enabled, it defaults to `i32`.
95pub type FixedIsize = match_pointer_width!(i16, i32, i64);
96
97/// The archived version of `isize` chosen based on the currently-enabled
98/// `pointer_width_*` feature.
99pub type ArchivedIsize =
100    match_pointer_width!(ArchivedI16, ArchivedI32, ArchivedI64);
101
102/// The native type that `usize` is converted to for archiving.
103///
104/// This will be `u16`, `u32`, or `u64` when the `pointer_width_16`,
105/// `pointer_width_32`, or `pointer_width_64` features are enabled,
106/// respectively. With no pointer width features enabled, it defaults to `u32`.
107pub type FixedUsize = match_pointer_width!(u16, u32, u64);
108
109/// The archived version of `isize` chosen based on the currently-enabled
110/// `pointer_width_*` feature.
111pub type ArchivedUsize =
112    match_pointer_width!(ArchivedU16, ArchivedU32, ArchivedU64);
113
114define_multibyte_primitives! {
115    ArchivedNonZeroI16:
116        NonZeroI16,
117        NonZeroI16_le,
118        NonZeroI16_ule,
119        NonZeroI16_be,
120        NonZeroI16_ube;
121
122    ArchivedNonZeroI32:
123        NonZeroI32,
124        NonZeroI32_le,
125        NonZeroI32_ule,
126        NonZeroI32_be,
127        NonZeroI32_ube;
128
129    ArchivedNonZeroI64:
130        NonZeroI64,
131        NonZeroI64_le,
132        NonZeroI64_ule,
133        NonZeroI64_be,
134        NonZeroI64_ube;
135
136    ArchivedNonZeroI128:
137        NonZeroI128,
138        NonZeroI128_le,
139        NonZeroI128_ule,
140        NonZeroI128_be,
141        NonZeroI128_ube;
142
143    ArchivedNonZeroU16:
144        NonZeroU16,
145        NonZeroU16_le,
146        NonZeroU16_ule,
147        NonZeroU16_be,
148        NonZeroU16_ube;
149
150    ArchivedNonZeroU32:
151        NonZeroU32,
152        NonZeroU32_le,
153        NonZeroU32_ule,
154        NonZeroU32_be,
155        NonZeroU32_ube;
156
157    ArchivedNonZeroU64:
158        NonZeroU64,
159        NonZeroU64_le,
160        NonZeroU64_ule,
161        NonZeroU64_be,
162        NonZeroU64_ube;
163
164    ArchivedNonZeroU128:
165        NonZeroU128,
166        NonZeroU128_le,
167        NonZeroU128_ule,
168        NonZeroU128_be,
169        NonZeroU128_ube;
170}
171
172/// The native type that `NonZeroIsize` is converted to for archiving.
173///
174/// This will be `NonZeroI16`, `NonZeroI32`, or `NonZeroI64` when the
175/// `pointer_width_16`, `pointer_width_32`, or `pointer_width_64` features are
176/// enabled, respectively. With no pointer width features enabled, it defaults
177/// to `NonZeroI32`.
178pub type FixedNonZeroIsize = match_pointer_width!(
179    ::core::num::NonZeroI16,
180    ::core::num::NonZeroI32,
181    ::core::num::NonZeroI64,
182);
183
184/// The archived version of `NonZeroIsize` chosen based on the currently-enabled
185/// `pointer_width_*` feature.
186pub type ArchivedNonZeroIsize = match_pointer_width!(
187    ArchivedNonZeroI16,
188    ArchivedNonZeroI32,
189    ArchivedNonZeroI64
190);
191
192/// The native type that `NonZeroUsize` is converted to for archiving.
193///
194/// This will be `NonZeroU16`, `NonZeroU32`, or `NonZeroU64` when the
195/// `pointer_width_16`, `pointer_width_32`, or `pointer_width_64` features are
196/// enabled, respectively. With no pointer width features enabled, it defaults
197/// to `NonZeroU32`.
198pub type FixedNonZeroUsize = match_pointer_width!(
199    ::core::num::NonZeroU16,
200    ::core::num::NonZeroU32,
201    ::core::num::NonZeroU64,
202);
203
204/// The archived version of `NonZeroUsize` chosen based on the currently-enabled
205/// `pointer_width_*` feature.
206pub type ArchivedNonZeroUsize = match_pointer_width!(
207    ArchivedNonZeroU16,
208    ArchivedNonZeroU32,
209    ArchivedNonZeroU64
210);