start adding fuzzing
[bigint-presentation-code.git] / register_allocator / src / macros.rs
1 macro_rules! validated_fields {
2 (
3 #[fields_ty = $fields_ty:ident $(, arbitrary = $Arbitrary:ident)? $(,)?]
4 $(#[$meta:meta])*
5 $vis:vis struct $ty:ident $body:tt
6 ) => {
7 $(#[$meta])*
8 #[derive(::serde::Serialize, ::serde::Deserialize$(, $Arbitrary)?)]
9 $vis struct $fields_ty $body
10
11 $(#[$meta])*
12 $vis struct $ty($fields_ty);
13
14 $(impl<'a> $Arbitrary<'a> for $ty {
15 fn arbitrary(u: &mut ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
16 Ok(<$fields_ty as ::arbitrary::Arbitrary>::arbitrary(u)?.try_into()?)
17 }
18 fn arbitrary_take_rest(u: ::arbitrary::Unstructured<'a>) -> ::arbitrary::Result<Self> {
19 Ok(<$fields_ty as ::arbitrary::Arbitrary>::arbitrary_take_rest(u)?.try_into()?)
20 }
21 fn size_hint(depth: usize) -> (usize, Option<usize>) {
22 <$fields_ty as ::arbitrary::Arbitrary>::size_hint(depth)
23 }
24 })?
25
26 impl TryFrom<$fields_ty> for $ty {
27 type Error = crate::error::Error;
28
29 fn try_from(fields: $fields_ty) -> Result<Self, Self::Error> {
30 $ty::new(fields)
31 }
32 }
33
34 impl From<$ty> for $fields_ty {
35 fn from(value: $ty) -> Self {
36 value.0
37 }
38 }
39
40 impl ::std::ops::Deref for $ty {
41 type Target = $fields_ty;
42
43 fn deref(&self) -> &Self::Target {
44 &self.0
45 }
46 }
47
48 impl $ty {
49 $vis const fn get(&self) -> &$fields_ty {
50 &self.0
51 }
52 }
53
54 impl ::serde::Serialize for $ty {
55 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
56 where
57 S: ::serde::Serializer,
58 {
59 self.0.serialize(serializer)
60 }
61 }
62
63 impl<'de> ::serde::Deserialize<'de> for $ty {
64 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
65 where
66 D: ::serde::Deserializer<'de>,
67 {
68 $fields_ty::deserialize(deserializer)?
69 .try_into()
70 .map_err(<D::Error as ::serde::de::Error>::custom)
71 }
72 }
73 };
74 }
75
76 macro_rules! const_unwrap_opt {
77 ($v:expr $(, $($msg:literal $(,)?)?)?) => {
78 match $v {
79 Some(v) => v,
80 None => panic!(concat!("tried to unwrap None", $($(": ", $msg,)?)?)),
81 }
82 };
83 }
84
85 macro_rules! const_unwrap_res {
86 ($v:expr $(, $($msg:literal $(,)?)?)?) => {
87 match $v {
88 Ok(v) => v,
89 Err(_e) => panic!(concat!("tried to unwrap Err", $($(": ", $msg,)?)?)),
90 }
91 };
92 }
93
94 macro_rules! const_try {
95 ($v:expr, Err($var:ident) => Err($err:expr) $(,)?) => {
96 match $v {
97 Ok(v) => v,
98 Err($var) => return Err($err),
99 }
100 };
101 ($v:expr $(,)?) => {
102 match $v {
103 Ok(v) => v,
104 Err(e) => return Err(e),
105 }
106 };
107 }
108
109 macro_rules! const_try_opt {
110 ($v:expr $(,)?) => {
111 match $v {
112 Some(v) => v,
113 None => return None,
114 }
115 };
116 }
117
118 macro_rules! nzu32_lit {
119 ($v:literal) => {{
120 const V: ::std::num::NonZeroU32 = match ::std::num::NonZeroU32::new($v) {
121 Some(v) => v,
122 None => panic!("literal must not be zero"),
123 };
124 V
125 }};
126 }