1 | // Super explicit first paragraph because this shows up at the top level and |
2 | // trips up people who are just looking for basic Serialize / Deserialize |
3 | // documentation. |
4 | // |
5 | /// Helper macro when implementing the `Deserializer` part of a new data format |
6 | /// for Serde. |
7 | /// |
8 | /// Some [`Deserializer`] implementations for self-describing formats do not |
9 | /// care what hint the [`Visitor`] gives them, they just want to blindly call |
10 | /// the [`Visitor`] method corresponding to the data they can tell is in the |
11 | /// input. This requires repetitive implementations of all the [`Deserializer`] |
12 | /// trait methods. |
13 | /// |
14 | /// ```edition2021 |
15 | /// # use serde::forward_to_deserialize_any; |
16 | /// # use serde::de::{value, Deserializer, Visitor}; |
17 | /// # |
18 | /// # struct MyDeserializer; |
19 | /// # |
20 | /// # impl<'de> Deserializer<'de> for MyDeserializer { |
21 | /// # type Error = value::Error; |
22 | /// # |
23 | /// # fn deserialize_any<V>(self, _: V) -> Result<V::Value, Self::Error> |
24 | /// # where |
25 | /// # V: Visitor<'de>, |
26 | /// # { |
27 | /// # unimplemented!() |
28 | /// # } |
29 | /// # |
30 | /// #[inline] |
31 | /// fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
32 | /// where |
33 | /// V: Visitor<'de>, |
34 | /// { |
35 | /// self.deserialize_any(visitor) |
36 | /// } |
37 | /// # |
38 | /// # forward_to_deserialize_any! { |
39 | /// # i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
40 | /// # bytes byte_buf option unit unit_struct newtype_struct seq tuple |
41 | /// # tuple_struct map struct enum identifier ignored_any |
42 | /// # } |
43 | /// # } |
44 | /// ``` |
45 | /// |
46 | /// The `forward_to_deserialize_any!` macro implements these simple forwarding |
47 | /// methods so that they forward directly to [`Deserializer::deserialize_any`]. |
48 | /// You can choose which methods to forward. |
49 | /// |
50 | /// ```edition2021 |
51 | /// # use serde::forward_to_deserialize_any; |
52 | /// # use serde::de::{value, Deserializer, Visitor}; |
53 | /// # |
54 | /// # struct MyDeserializer; |
55 | /// # |
56 | /// impl<'de> Deserializer<'de> for MyDeserializer { |
57 | /// # type Error = value::Error; |
58 | /// # |
59 | /// fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
60 | /// where |
61 | /// V: Visitor<'de>, |
62 | /// { |
63 | /// /* ... */ |
64 | /// # let _ = visitor; |
65 | /// # unimplemented!() |
66 | /// } |
67 | /// |
68 | /// forward_to_deserialize_any! { |
69 | /// bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
70 | /// bytes byte_buf option unit unit_struct newtype_struct seq tuple |
71 | /// tuple_struct map struct enum identifier ignored_any |
72 | /// } |
73 | /// } |
74 | /// ``` |
75 | /// |
76 | /// The macro assumes the convention that your `Deserializer` lifetime parameter |
77 | /// is called `'de` and that the `Visitor` type parameters on each method are |
78 | /// called `V`. A different type parameter and a different lifetime can be |
79 | /// specified explicitly if necessary. |
80 | /// |
81 | /// ```edition2021 |
82 | /// # use serde::forward_to_deserialize_any; |
83 | /// # use serde::de::{value, Deserializer, Visitor}; |
84 | /// # use std::marker::PhantomData; |
85 | /// # |
86 | /// # struct MyDeserializer<V>(PhantomData<V>); |
87 | /// # |
88 | /// # impl<'q, V> Deserializer<'q> for MyDeserializer<V> { |
89 | /// # type Error = value::Error; |
90 | /// # |
91 | /// # fn deserialize_any<W>(self, visitor: W) -> Result<W::Value, Self::Error> |
92 | /// # where |
93 | /// # W: Visitor<'q>, |
94 | /// # { |
95 | /// # unimplemented!() |
96 | /// # } |
97 | /// # |
98 | /// forward_to_deserialize_any! { |
99 | /// <W: Visitor<'q>> |
100 | /// bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
101 | /// bytes byte_buf option unit unit_struct newtype_struct seq tuple |
102 | /// tuple_struct map struct enum identifier ignored_any |
103 | /// } |
104 | /// # } |
105 | /// ``` |
106 | /// |
107 | /// [`Deserializer`]: trait.Deserializer.html |
108 | /// [`Visitor`]: de/trait.Visitor.html |
109 | /// [`Deserializer::deserialize_any`]: trait.Deserializer.html#tymethod.deserialize_any |
110 | #[macro_export (local_inner_macros)] |
111 | macro_rules! forward_to_deserialize_any { |
112 | (<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => { |
113 | $(forward_to_deserialize_any_helper!{$func<$lifetime, $visitor>})* |
114 | }; |
115 | // This case must be after the previous one. |
116 | ($($func:ident)*) => { |
117 | $(forward_to_deserialize_any_helper!{$func<'de, V>})* |
118 | }; |
119 | } |
120 | |
121 | #[doc (hidden)] |
122 | #[macro_export ] |
123 | macro_rules! forward_to_deserialize_any_method { |
124 | ($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => { |
125 | #[inline] |
126 | fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, Self::Error> |
127 | where |
128 | $v: $crate::de::Visitor<$l>, |
129 | { |
130 | $( |
131 | let _ = $arg; |
132 | )* |
133 | self.deserialize_any(visitor) |
134 | } |
135 | }; |
136 | } |
137 | |
138 | #[doc (hidden)] |
139 | #[macro_export (local_inner_macros)] |
140 | macro_rules! forward_to_deserialize_any_helper { |
141 | (bool<$l:tt, $v:ident>) => { |
142 | forward_to_deserialize_any_method!{deserialize_bool<$l, $v>()} |
143 | }; |
144 | (i8<$l:tt, $v:ident>) => { |
145 | forward_to_deserialize_any_method!{deserialize_i8<$l, $v>()} |
146 | }; |
147 | (i16<$l:tt, $v:ident>) => { |
148 | forward_to_deserialize_any_method!{deserialize_i16<$l, $v>()} |
149 | }; |
150 | (i32<$l:tt, $v:ident>) => { |
151 | forward_to_deserialize_any_method!{deserialize_i32<$l, $v>()} |
152 | }; |
153 | (i64<$l:tt, $v:ident>) => { |
154 | forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()} |
155 | }; |
156 | (i128<$l:tt, $v:ident>) => { |
157 | serde_if_integer128! { |
158 | forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()} |
159 | } |
160 | }; |
161 | (u8<$l:tt, $v:ident>) => { |
162 | forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()} |
163 | }; |
164 | (u16<$l:tt, $v:ident>) => { |
165 | forward_to_deserialize_any_method!{deserialize_u16<$l, $v>()} |
166 | }; |
167 | (u32<$l:tt, $v:ident>) => { |
168 | forward_to_deserialize_any_method!{deserialize_u32<$l, $v>()} |
169 | }; |
170 | (u64<$l:tt, $v:ident>) => { |
171 | forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()} |
172 | }; |
173 | (u128<$l:tt, $v:ident>) => { |
174 | serde_if_integer128! { |
175 | forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()} |
176 | } |
177 | }; |
178 | (f32<$l:tt, $v:ident>) => { |
179 | forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()} |
180 | }; |
181 | (f64<$l:tt, $v:ident>) => { |
182 | forward_to_deserialize_any_method!{deserialize_f64<$l, $v>()} |
183 | }; |
184 | (char<$l:tt, $v:ident>) => { |
185 | forward_to_deserialize_any_method!{deserialize_char<$l, $v>()} |
186 | }; |
187 | (str<$l:tt, $v:ident>) => { |
188 | forward_to_deserialize_any_method!{deserialize_str<$l, $v>()} |
189 | }; |
190 | (string<$l:tt, $v:ident>) => { |
191 | forward_to_deserialize_any_method!{deserialize_string<$l, $v>()} |
192 | }; |
193 | (bytes<$l:tt, $v:ident>) => { |
194 | forward_to_deserialize_any_method!{deserialize_bytes<$l, $v>()} |
195 | }; |
196 | (byte_buf<$l:tt, $v:ident>) => { |
197 | forward_to_deserialize_any_method!{deserialize_byte_buf<$l, $v>()} |
198 | }; |
199 | (option<$l:tt, $v:ident>) => { |
200 | forward_to_deserialize_any_method!{deserialize_option<$l, $v>()} |
201 | }; |
202 | (unit<$l:tt, $v:ident>) => { |
203 | forward_to_deserialize_any_method!{deserialize_unit<$l, $v>()} |
204 | }; |
205 | (unit_struct<$l:tt, $v:ident>) => { |
206 | forward_to_deserialize_any_method!{deserialize_unit_struct<$l, $v>(name: &'static str)} |
207 | }; |
208 | (newtype_struct<$l:tt, $v:ident>) => { |
209 | forward_to_deserialize_any_method!{deserialize_newtype_struct<$l, $v>(name: &'static str)} |
210 | }; |
211 | (seq<$l:tt, $v:ident>) => { |
212 | forward_to_deserialize_any_method!{deserialize_seq<$l, $v>()} |
213 | }; |
214 | (tuple<$l:tt, $v:ident>) => { |
215 | forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)} |
216 | }; |
217 | (tuple_struct<$l:tt, $v:ident>) => { |
218 | forward_to_deserialize_any_method!{deserialize_tuple_struct<$l, $v>(name: &'static str, len: usize)} |
219 | }; |
220 | (map<$l:tt, $v:ident>) => { |
221 | forward_to_deserialize_any_method!{deserialize_map<$l, $v>()} |
222 | }; |
223 | (struct<$l:tt, $v:ident>) => { |
224 | forward_to_deserialize_any_method!{deserialize_struct<$l, $v>(name: &'static str, fields: &'static [&'static str])} |
225 | }; |
226 | (enum<$l:tt, $v:ident>) => { |
227 | forward_to_deserialize_any_method!{deserialize_enum<$l, $v>(name: &'static str, variants: &'static [&'static str])} |
228 | }; |
229 | (identifier<$l:tt, $v:ident>) => { |
230 | forward_to_deserialize_any_method!{deserialize_identifier<$l, $v>()} |
231 | }; |
232 | (ignored_any<$l:tt, $v:ident>) => { |
233 | forward_to_deserialize_any_method!{deserialize_ignored_any<$l, $v>()} |
234 | }; |
235 | } |
236 | |