rkyv_test/
macros.rs

1#[cfg(feature = "copy")]
2macro_rules! default {
3    (#[inline] $($fn:tt)*) => { #[inline] default $($fn)* };
4    ($($fn:tt)*) => { default $($fn)* };
5}
6
7#[cfg(not(feature = "copy"))]
8macro_rules! default {
9    (#[inline] $($fn:tt)*) => { #[inline] $($fn)* };
10    ($($fn:tt)*) => { $($fn)* };
11}
12
13/// Returns a tuple of the field offset and a mutable pointer to the field of the given struct
14/// pointer.
15///
16/// # Examples
17/// ```
18/// use core::mem::MaybeUninit;
19/// use rkyv::out_field;
20///
21/// struct Example {
22///     a: i32,
23///     b: bool,
24/// }
25///
26/// let mut result = MaybeUninit::<Example>::zeroed();
27/// let out = result.as_mut_ptr();
28///
29/// let (a_off, a) = out_field!(out.a);
30/// unsafe { a.write(42); }
31/// let (b_off, b) = out_field!(out.b);
32/// unsafe { b.write(true); }
33///
34/// let result = unsafe { result.assume_init() };
35/// assert_eq!(result.a, 42);
36/// assert_eq!(result.b, true);
37/// ```
38#[macro_export]
39macro_rules! out_field {
40    ($out:ident.$field:tt) => {{
41        #[allow(unused_unsafe)]
42        unsafe {
43            let fo = ::core::ptr::addr_of_mut!((*$out).$field);
44            (fo.cast::<u8>().offset_from($out.cast::<u8>()) as usize, fo)
45        }
46    }};
47}
48
49/// Returns the unarchived value of the given archived primitive.
50///
51/// This macro is not needed for most use cases. Its primary purpose is to simultaneously:
52/// - Convert values from (potentially) different archived primitives to their native counterparts
53/// - Allow transformation in `const` contexts
54/// - Prevent linter warnings from unused `into()` calls
55///
56/// Users should feel free to use the more ergonomic `into()` where appropriate.
57#[macro_export]
58macro_rules! from_archived {
59    ($expr:expr) => {{
60        #[cfg(not(any(feature = "archive_le", feature = "archive_be")))]
61        {
62            $expr
63        }
64        #[cfg(any(feature = "archive_le", feature = "archive_be"))]
65        {
66            ($expr).value()
67        }
68    }};
69}
70
71#[cfg(any(feature = "archive_le", feature = "archive_be"))]
72pub use crate::rend::NativeEndian;
73
74/// Returns the archived value of the given archived primitive.
75///
76/// This macro is not needed for most use cases. Its primary purpose is to simultaneously:
77/// - Convert values from (potentially) different primitives to their archived counterparts
78/// - Allow transformation in `const` contexts
79/// - Prevent linter warnings from unused `into()` calls
80///
81/// Users should feel free to use the more ergonomic `into()` where appropriate.
82#[macro_export]
83macro_rules! to_archived {
84    ($expr:expr) => {{
85        #[cfg(not(any(feature = "archive_le", feature = "archive_be")))]
86        {
87            $expr
88        }
89        #[cfg(feature = "archive_le")]
90        {
91            $crate::macros::NativeEndian { value: $expr }.to_le()
92        }
93        #[cfg(feature = "archive_be")]
94        {
95            $crate::macros::NativeEndian { value: $expr }.to_be()
96        }
97    }};
98}
99
100#[cfg(not(any(feature = "size_16", feature = "size_32", feature = "size_64")))]
101core::compile_error!(r#"one of ["size_16", "size_32", or "size_64"] features must be enabled"#);
102
103#[cfg(all(feature = "size_16", feature = "size_32"))]
104core::compile_error!(
105    "\"size_16\" and \"size_32\" are mutually-exclusive features. You may need to set \
106    `default-features = false` or compile with `--no-default-features`."
107);
108#[cfg(all(feature = "size_16", feature = "size_64"))]
109core::compile_error!(
110    "\"size_16\" and \"size_64\" are mutually-exclusive features. You may need to set \
111    `default-features = false` or compile with `--no-default-features`."
112);
113#[cfg(all(feature = "size_32", feature = "size_64"))]
114core::compile_error!(
115    "\"size_32\" and \"size_64\" are mutually-exclusive features. You may need to set \
116    `default-features = false` or compile with `--no-default-features`."
117);
118
119#[cfg(feature = "size_16")]
120macro_rules! pick_size_type {
121    ($s16:ty, $s32:ty, $s64:ty) => {
122        $s16
123    };
124    ($s16:ty, $s32:ty, $s64:ty,) => {
125        pick_size_type!($s16, $s32, $s64)
126    };
127}
128
129#[cfg(feature = "size_32")]
130macro_rules! pick_size_type {
131    ($s16:ty, $s32:ty, $s64:ty) => {
132        $s32
133    };
134    ($s16:ty, $s32:ty, $s64:ty,) => {
135        pick_size_type!($s16, $s32, $s64)
136    };
137}
138
139#[cfg(feature = "size_64")]
140macro_rules! pick_size_type {
141    ($s16:ty, $s32:ty, $s64:ty) => {
142        $s64
143    };
144    ($s16:ty, $s32:ty, $s64:ty,) => {
145        pick_size_type!($s16, $s32, $s64)
146    };
147}