test_fuzz/
utils.rs

1//! **Warning:** The contents of `test_fuzz::utils` are provided for convenience and may be removed
2//! in future versions of `test-fuzz`.
3
4/// Skip values of type `$ty` when serializing. Initialize values of type `$ty` with `$expr` when
5/// deserializing.
6#[macro_export]
7macro_rules! dont_care {
8    ($ty:path, $expr:expr) => {
9        impl serde::Serialize for $ty {
10            fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
11            where
12                S: serde::Serializer,
13            {
14                ().serialize(serializer)
15            }
16        }
17
18        impl<'de> serde::Deserialize<'de> for $ty {
19            fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
20            where
21                D: serde::Deserializer<'de>,
22            {
23                <()>::deserialize(deserializer).map(|_| $expr)
24            }
25        }
26    };
27    ($ty:path) => {
28        $crate::dont_care!($ty, $ty);
29    };
30}
31
32/// Wrap `<$ty as ToOwned>::Owned` in a type `$ident` and implement `From` and `test_fuzz::Into`
33/// for `$ident` so that `convert = "&$ty, $ident"` can be used.
34#[macro_export]
35macro_rules! leak {
36    ($ty:ty, $ident:ident) => {
37        #[derive(Clone, std::fmt::Debug, serde::Deserialize, serde::Serialize)]
38        struct $ident(<$ty as ToOwned>::Owned);
39
40        impl From<&$ty> for $ident {
41            fn from(ty: &$ty) -> Self {
42                Self(ty.to_owned())
43            }
44        }
45
46        impl test_fuzz::Into<&$ty> for $ident {
47            fn into(self) -> &'static $ty {
48                Box::leak(Box::new(self.0))
49            }
50        }
51    };
52}
53
54pub mod serde_ref {
55    pub use super::deserialize_ref as deserialize;
56    pub use super::serialize_ref as serialize;
57}
58
59/// `serialize_ref` functions similar to `leak!`, but it is meant to be used with Serde's
60/// [`serialize_with`](https://serde.rs/field-attrs.html#serialize_with) field attribute.
61#[inline]
62pub fn serialize_ref<'a, S, T>(x: &&'a T, serializer: S) -> Result<S::Ok, S::Error>
63where
64    S: serde::Serializer,
65    T: serde::Serialize,
66{
67    use serde_combinators::{RefF, SerializeWith, Type};
68
69    <RefF<'a, Type<T>> as SerializeWith>::serialize(x, serializer)
70}
71
72/// `deserialize_ref` functions similar to `leak!`, but it is meant to be used with Serde's
73/// [`deserialize_with`](https://serde.rs/field-attrs.html#deserialize_with) field attribute.
74#[inline]
75pub fn deserialize_ref<'de, D, T>(deserializer: D) -> Result<&'static T, D::Error>
76where
77    D: serde::Deserializer<'de>,
78    T: serde::de::DeserializeOwned + std::fmt::Debug,
79{
80    use serde_combinators::{DeserializeWith, RefF, Type};
81
82    <RefF<'static, Type<T>> as DeserializeWith>::deserialize(deserializer)
83}
84
85pub mod serde_ref_mut {
86    pub use super::deserialize_ref_mut as deserialize;
87    pub use super::serialize_ref_mut as serialize;
88}
89
90/// `serialize_ref_mut` is similar to `serialize_ref`, except it operates on a mutable reference
91/// instead of an immutable one.
92pub fn serialize_ref_mut<'a, S, T>(x: &&'a mut T, serializer: S) -> Result<S::Ok, S::Error>
93where
94    S: serde::Serializer,
95    T: serde::Serialize,
96{
97    use serde_combinators::{RefMutF, SerializeWith, Type};
98
99    <RefMutF<'a, Type<T>> as SerializeWith>::serialize(x, serializer)
100}
101
102/// `deserialize_ref_mut` is similar to `deserialize_ref`, except it operates on a mutable reference
103/// instead of an immutable one.
104pub fn deserialize_ref_mut<'de, D, T>(deserializer: D) -> Result<&'static mut T, D::Error>
105where
106    D: serde::Deserializer<'de>,
107    T: serde::de::DeserializeOwned + std::fmt::Debug,
108{
109    use serde_combinators::{DeserializeWith, RefMutF, Type};
110
111    <RefMutF<'static, Type<T>> as DeserializeWith>::deserialize(deserializer)
112}