]>
Commit | Line | Data |
---|---|---|
041b39d2 XL |
1 | use lib::*; |
2 | ||
8faf50e0 XL |
3 | use de::{ |
4 | Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess, Visitor, | |
5 | }; | |
041b39d2 | 6 | |
5099ac24 | 7 | #[cfg(any(feature = "std", feature = "alloc", not(no_core_duration)))] |
041b39d2 XL |
8 | use de::MapAccess; |
9 | ||
5869c6ff | 10 | use seed::InPlaceSeed; |
041b39d2 XL |
11 | |
12 | #[cfg(any(feature = "std", feature = "alloc"))] | |
5869c6ff | 13 | use __private::size_hint; |
041b39d2 XL |
14 | |
15 | //////////////////////////////////////////////////////////////////////////////// | |
16 | ||
17 | struct UnitVisitor; | |
18 | ||
19 | impl<'de> Visitor<'de> for UnitVisitor { | |
20 | type Value = (); | |
21 | ||
22 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
23 | formatter.write_str("unit") | |
24 | } | |
25 | ||
8faf50e0 | 26 | fn visit_unit<E>(self) -> Result<Self::Value, E> |
041b39d2 XL |
27 | where |
28 | E: Error, | |
29 | { | |
30 | Ok(()) | |
31 | } | |
32 | } | |
33 | ||
34 | impl<'de> Deserialize<'de> for () { | |
8faf50e0 | 35 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
36 | where |
37 | D: Deserializer<'de>, | |
38 | { | |
39 | deserializer.deserialize_unit(UnitVisitor) | |
40 | } | |
41 | } | |
42 | ||
8faf50e0 XL |
43 | #[cfg(feature = "unstable")] |
44 | impl<'de> Deserialize<'de> for ! { | |
45 | fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error> | |
46 | where | |
47 | D: Deserializer<'de>, | |
48 | { | |
49 | Err(Error::custom("cannot deserialize `!`")) | |
50 | } | |
51 | } | |
52 | ||
041b39d2 XL |
53 | //////////////////////////////////////////////////////////////////////////////// |
54 | ||
55 | struct BoolVisitor; | |
56 | ||
57 | impl<'de> Visitor<'de> for BoolVisitor { | |
58 | type Value = bool; | |
59 | ||
60 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
61 | formatter.write_str("a boolean") | |
62 | } | |
63 | ||
8faf50e0 | 64 | fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> |
041b39d2 XL |
65 | where |
66 | E: Error, | |
67 | { | |
68 | Ok(v) | |
69 | } | |
70 | } | |
71 | ||
72 | impl<'de> Deserialize<'de> for bool { | |
8faf50e0 | 73 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
74 | where |
75 | D: Deserializer<'de>, | |
76 | { | |
77 | deserializer.deserialize_bool(BoolVisitor) | |
78 | } | |
79 | } | |
80 | ||
81 | //////////////////////////////////////////////////////////////////////////////// | |
82 | ||
041b39d2 | 83 | macro_rules! impl_deserialize_num { |
5099ac24 FG |
84 | ($primitive:ident, $nonzero:ident $(cfg($($cfg:tt)*))*, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { |
85 | impl_deserialize_num!($primitive, $deserialize $($method!($($val : $visit)*);)*); | |
86 | ||
87 | #[cfg(all(not(no_num_nonzero), $($($cfg)*)*))] | |
88 | impl<'de> Deserialize<'de> for num::$nonzero { | |
89 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
90 | where | |
91 | D: Deserializer<'de>, | |
92 | { | |
93 | struct NonZeroVisitor; | |
94 | ||
95 | impl<'de> Visitor<'de> for NonZeroVisitor { | |
96 | type Value = num::$nonzero; | |
97 | ||
98 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
99 | formatter.write_str(concat!("a nonzero ", stringify!($primitive))) | |
100 | } | |
101 | ||
102 | $($($method!(nonzero $primitive $val : $visit);)*)* | |
103 | } | |
104 | ||
105 | deserializer.$deserialize(NonZeroVisitor) | |
106 | } | |
107 | } | |
108 | }; | |
109 | ||
110 | ($primitive:ident, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { | |
111 | impl<'de> Deserialize<'de> for $primitive { | |
041b39d2 | 112 | #[inline] |
8faf50e0 | 113 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
114 | where |
115 | D: Deserializer<'de>, | |
116 | { | |
117 | struct PrimitiveVisitor; | |
118 | ||
119 | impl<'de> Visitor<'de> for PrimitiveVisitor { | |
5099ac24 | 120 | type Value = $primitive; |
041b39d2 XL |
121 | |
122 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
5099ac24 | 123 | formatter.write_str(stringify!($primitive)) |
041b39d2 XL |
124 | } |
125 | ||
5099ac24 | 126 | $($($method!($val : $visit);)*)* |
041b39d2 XL |
127 | } |
128 | ||
5869c6ff | 129 | deserializer.$deserialize(PrimitiveVisitor) |
041b39d2 XL |
130 | } |
131 | } | |
132 | }; | |
5869c6ff | 133 | } |
041b39d2 | 134 | |
5869c6ff XL |
135 | macro_rules! num_self { |
136 | ($ty:ident : $visit:ident) => { | |
137 | #[inline] | |
138 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
139 | where | |
140 | E: Error, | |
141 | { | |
142 | Ok(v) | |
143 | } | |
144 | }; | |
5099ac24 FG |
145 | |
146 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
147 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
148 | where | |
149 | E: Error, | |
150 | { | |
151 | if let Some(nonzero) = Self::Value::new(v) { | |
152 | Ok(nonzero) | |
153 | } else { | |
154 | Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) | |
155 | } | |
156 | } | |
157 | }; | |
5869c6ff | 158 | } |
041b39d2 | 159 | |
5869c6ff | 160 | macro_rules! num_as_self { |
5099ac24 FG |
161 | ($ty:ident : $visit:ident) => { |
162 | #[inline] | |
163 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
164 | where | |
165 | E: Error, | |
166 | { | |
167 | Ok(v as Self::Value) | |
168 | } | |
169 | }; | |
170 | ||
171 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
172 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
173 | where | |
174 | E: Error, | |
175 | { | |
176 | if let Some(nonzero) = Self::Value::new(v as $primitive) { | |
177 | Ok(nonzero) | |
178 | } else { | |
179 | Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) | |
5869c6ff | 180 | } |
5099ac24 | 181 | } |
041b39d2 | 182 | }; |
5869c6ff | 183 | } |
041b39d2 | 184 | |
5869c6ff | 185 | macro_rules! int_to_int { |
5099ac24 FG |
186 | ($ty:ident : $visit:ident) => { |
187 | #[inline] | |
188 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
189 | where | |
190 | E: Error, | |
191 | { | |
192 | if Self::Value::min_value() as i64 <= v as i64 | |
193 | && v as i64 <= Self::Value::max_value() as i64 | |
5869c6ff | 194 | { |
5099ac24 FG |
195 | Ok(v as Self::Value) |
196 | } else { | |
197 | Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) | |
198 | } | |
199 | } | |
200 | }; | |
201 | ||
202 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
203 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
204 | where | |
205 | E: Error, | |
206 | { | |
207 | if $primitive::min_value() as i64 <= v as i64 | |
208 | && v as i64 <= $primitive::max_value() as i64 | |
209 | { | |
210 | if let Some(nonzero) = Self::Value::new(v as $primitive) { | |
211 | return Ok(nonzero); | |
5869c6ff XL |
212 | } |
213 | } | |
5099ac24 FG |
214 | Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
215 | } | |
041b39d2 XL |
216 | }; |
217 | } | |
218 | ||
5869c6ff | 219 | macro_rules! int_to_uint { |
5099ac24 FG |
220 | ($ty:ident : $visit:ident) => { |
221 | #[inline] | |
222 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
223 | where | |
224 | E: Error, | |
225 | { | |
226 | if 0 <= v && v as u64 <= Self::Value::max_value() as u64 { | |
227 | Ok(v as Self::Value) | |
228 | } else { | |
229 | Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) | |
230 | } | |
231 | } | |
232 | }; | |
233 | ||
234 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
235 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
236 | where | |
237 | E: Error, | |
238 | { | |
239 | if 0 < v && v as u64 <= $primitive::max_value() as u64 { | |
240 | if let Some(nonzero) = Self::Value::new(v as $primitive) { | |
241 | return Ok(nonzero); | |
5869c6ff XL |
242 | } |
243 | } | |
5099ac24 FG |
244 | Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) |
245 | } | |
5869c6ff XL |
246 | }; |
247 | } | |
041b39d2 | 248 | |
5869c6ff | 249 | macro_rules! uint_to_self { |
5099ac24 FG |
250 | ($ty:ident : $visit:ident) => { |
251 | #[inline] | |
252 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
253 | where | |
254 | E: Error, | |
255 | { | |
256 | if v as u64 <= Self::Value::max_value() as u64 { | |
257 | Ok(v as Self::Value) | |
258 | } else { | |
259 | Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) | |
260 | } | |
261 | } | |
262 | }; | |
263 | ||
264 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
265 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
266 | where | |
267 | E: Error, | |
268 | { | |
269 | if v as u64 <= $primitive::max_value() as u64 { | |
270 | if let Some(nonzero) = Self::Value::new(v as $primitive) { | |
271 | return Ok(nonzero); | |
5869c6ff XL |
272 | } |
273 | } | |
5099ac24 FG |
274 | Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) |
275 | } | |
5869c6ff XL |
276 | }; |
277 | } | |
041b39d2 | 278 | |
5869c6ff | 279 | impl_deserialize_num! { |
5099ac24 | 280 | i8, NonZeroI8 cfg(not(no_num_nonzero_signed)), deserialize_i8 |
5869c6ff XL |
281 | num_self!(i8:visit_i8); |
282 | int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
283 | uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
284 | } | |
041b39d2 | 285 | |
5869c6ff | 286 | impl_deserialize_num! { |
5099ac24 | 287 | i16, NonZeroI16 cfg(not(no_num_nonzero_signed)), deserialize_i16 |
5869c6ff XL |
288 | num_self!(i16:visit_i16); |
289 | num_as_self!(i8:visit_i8); | |
290 | int_to_int!(i32:visit_i32 i64:visit_i64); | |
291 | uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
292 | } | |
8faf50e0 | 293 | |
5869c6ff | 294 | impl_deserialize_num! { |
5099ac24 | 295 | i32, NonZeroI32 cfg(not(no_num_nonzero_signed)), deserialize_i32 |
5869c6ff XL |
296 | num_self!(i32:visit_i32); |
297 | num_as_self!(i8:visit_i8 i16:visit_i16); | |
298 | int_to_int!(i64:visit_i64); | |
299 | uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
300 | } | |
8faf50e0 | 301 | |
5869c6ff | 302 | impl_deserialize_num! { |
5099ac24 | 303 | i64, NonZeroI64 cfg(not(no_num_nonzero_signed)), deserialize_i64 |
5869c6ff XL |
304 | num_self!(i64:visit_i64); |
305 | num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32); | |
306 | uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
307 | } | |
8faf50e0 | 308 | |
5869c6ff | 309 | impl_deserialize_num! { |
5099ac24 | 310 | isize, NonZeroIsize cfg(not(no_num_nonzero_signed)), deserialize_i64 |
5869c6ff XL |
311 | num_as_self!(i8:visit_i8 i16:visit_i16); |
312 | int_to_int!(i32:visit_i32 i64:visit_i64); | |
313 | uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
314 | } | |
8faf50e0 | 315 | |
5869c6ff | 316 | impl_deserialize_num! { |
5099ac24 | 317 | u8, NonZeroU8, deserialize_u8 |
5869c6ff XL |
318 | num_self!(u8:visit_u8); |
319 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
320 | uint_to_self!(u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
321 | } | |
8faf50e0 | 322 | |
5869c6ff | 323 | impl_deserialize_num! { |
5099ac24 | 324 | u16, NonZeroU16, deserialize_u16 |
5869c6ff XL |
325 | num_self!(u16:visit_u16); |
326 | num_as_self!(u8:visit_u8); | |
327 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
328 | uint_to_self!(u32:visit_u32 u64:visit_u64); | |
329 | } | |
8faf50e0 | 330 | |
5869c6ff | 331 | impl_deserialize_num! { |
5099ac24 | 332 | u32, NonZeroU32, deserialize_u32 |
5869c6ff XL |
333 | num_self!(u32:visit_u32); |
334 | num_as_self!(u8:visit_u8 u16:visit_u16); | |
335 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
336 | uint_to_self!(u64:visit_u64); | |
337 | } | |
8faf50e0 | 338 | |
5869c6ff | 339 | impl_deserialize_num! { |
5099ac24 | 340 | u64, NonZeroU64, deserialize_u64 |
5869c6ff XL |
341 | num_self!(u64:visit_u64); |
342 | num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32); | |
343 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
344 | } | |
8faf50e0 | 345 | |
5869c6ff | 346 | impl_deserialize_num! { |
5099ac24 | 347 | usize, NonZeroUsize, deserialize_u64 |
5869c6ff XL |
348 | num_as_self!(u8:visit_u8 u16:visit_u16); |
349 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
350 | uint_to_self!(u32:visit_u32 u64:visit_u64); | |
351 | } | |
8faf50e0 | 352 | |
5869c6ff XL |
353 | impl_deserialize_num! { |
354 | f32, deserialize_f32 | |
355 | num_self!(f32:visit_f32); | |
356 | num_as_self!(f64:visit_f64); | |
357 | num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
358 | num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
359 | } | |
8faf50e0 | 360 | |
5869c6ff XL |
361 | impl_deserialize_num! { |
362 | f64, deserialize_f64 | |
363 | num_self!(f64:visit_f64); | |
364 | num_as_self!(f32:visit_f32); | |
365 | num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
366 | num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
367 | } | |
8faf50e0 | 368 | |
5869c6ff | 369 | serde_if_integer128! { |
5099ac24 FG |
370 | macro_rules! num_128 { |
371 | ($ty:ident : $visit:ident) => { | |
372 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
373 | where | |
374 | E: Error, | |
375 | { | |
376 | if v as i128 >= Self::Value::min_value() as i128 | |
377 | && v as u128 <= Self::Value::max_value() as u128 | |
378 | { | |
379 | Ok(v as Self::Value) | |
380 | } else { | |
381 | Err(Error::invalid_value( | |
382 | Unexpected::Other(stringify!($ty)), | |
383 | &self, | |
384 | )) | |
385 | } | |
386 | } | |
387 | }; | |
388 | ||
389 | (nonzero $primitive:ident $ty:ident : $visit:ident) => { | |
390 | fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> | |
391 | where | |
392 | E: Error, | |
393 | { | |
394 | if v as i128 >= $primitive::min_value() as i128 | |
395 | && v as u128 <= $primitive::max_value() as u128 | |
396 | { | |
397 | if let Some(nonzero) = Self::Value::new(v as $primitive) { | |
398 | Ok(nonzero) | |
399 | } else { | |
400 | Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) | |
401 | } | |
402 | } else { | |
403 | Err(Error::invalid_value( | |
404 | Unexpected::Other(stringify!($ty)), | |
405 | &self, | |
406 | )) | |
407 | } | |
408 | } | |
409 | }; | |
410 | } | |
411 | ||
5869c6ff | 412 | impl_deserialize_num! { |
5099ac24 | 413 | i128, NonZeroI128 cfg(not(no_num_nonzero_signed)), deserialize_i128 |
5869c6ff XL |
414 | num_self!(i128:visit_i128); |
415 | num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
416 | num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
5099ac24 | 417 | num_128!(u128:visit_u128); |
5869c6ff XL |
418 | } |
419 | ||
420 | impl_deserialize_num! { | |
5099ac24 | 421 | u128, NonZeroU128, deserialize_u128 |
5869c6ff XL |
422 | num_self!(u128:visit_u128); |
423 | num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); | |
424 | int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); | |
5099ac24 | 425 | num_128!(i128:visit_i128); |
8faf50e0 XL |
426 | } |
427 | } | |
428 | ||
041b39d2 XL |
429 | //////////////////////////////////////////////////////////////////////////////// |
430 | ||
431 | struct CharVisitor; | |
432 | ||
433 | impl<'de> Visitor<'de> for CharVisitor { | |
434 | type Value = char; | |
435 | ||
436 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
437 | formatter.write_str("a character") | |
438 | } | |
439 | ||
440 | #[inline] | |
8faf50e0 | 441 | fn visit_char<E>(self, v: char) -> Result<Self::Value, E> |
041b39d2 XL |
442 | where |
443 | E: Error, | |
444 | { | |
445 | Ok(v) | |
446 | } | |
447 | ||
448 | #[inline] | |
8faf50e0 | 449 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
041b39d2 XL |
450 | where |
451 | E: Error, | |
452 | { | |
453 | let mut iter = v.chars(); | |
454 | match (iter.next(), iter.next()) { | |
455 | (Some(c), None) => Ok(c), | |
456 | _ => Err(Error::invalid_value(Unexpected::Str(v), &self)), | |
457 | } | |
458 | } | |
459 | } | |
460 | ||
461 | impl<'de> Deserialize<'de> for char { | |
462 | #[inline] | |
8faf50e0 | 463 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
464 | where |
465 | D: Deserializer<'de>, | |
466 | { | |
467 | deserializer.deserialize_char(CharVisitor) | |
468 | } | |
469 | } | |
470 | ||
471 | //////////////////////////////////////////////////////////////////////////////// | |
472 | ||
473 | #[cfg(any(feature = "std", feature = "alloc"))] | |
474 | struct StringVisitor; | |
ff7c6d11 XL |
475 | #[cfg(any(feature = "std", feature = "alloc"))] |
476 | struct StringInPlaceVisitor<'a>(&'a mut String); | |
041b39d2 XL |
477 | |
478 | #[cfg(any(feature = "std", feature = "alloc"))] | |
479 | impl<'de> Visitor<'de> for StringVisitor { | |
480 | type Value = String; | |
481 | ||
482 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
483 | formatter.write_str("a string") | |
484 | } | |
485 | ||
8faf50e0 | 486 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
041b39d2 XL |
487 | where |
488 | E: Error, | |
489 | { | |
490 | Ok(v.to_owned()) | |
491 | } | |
492 | ||
8faf50e0 | 493 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
041b39d2 XL |
494 | where |
495 | E: Error, | |
496 | { | |
497 | Ok(v) | |
498 | } | |
499 | ||
8faf50e0 | 500 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
041b39d2 XL |
501 | where |
502 | E: Error, | |
503 | { | |
504 | match str::from_utf8(v) { | |
505 | Ok(s) => Ok(s.to_owned()), | |
506 | Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | |
507 | } | |
508 | } | |
509 | ||
8faf50e0 | 510 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
041b39d2 XL |
511 | where |
512 | E: Error, | |
513 | { | |
514 | match String::from_utf8(v) { | |
515 | Ok(s) => Ok(s), | |
ff7c6d11 XL |
516 | Err(e) => Err(Error::invalid_value( |
517 | Unexpected::Bytes(&e.into_bytes()), | |
518 | &self, | |
519 | )), | |
520 | } | |
521 | } | |
522 | } | |
523 | ||
524 | #[cfg(any(feature = "std", feature = "alloc"))] | |
525 | impl<'a, 'de> Visitor<'de> for StringInPlaceVisitor<'a> { | |
526 | type Value = (); | |
527 | ||
528 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
529 | formatter.write_str("a string") | |
530 | } | |
531 | ||
8faf50e0 | 532 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
ff7c6d11 XL |
533 | where |
534 | E: Error, | |
535 | { | |
536 | self.0.clear(); | |
537 | self.0.push_str(v); | |
538 | Ok(()) | |
539 | } | |
540 | ||
8faf50e0 | 541 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
ff7c6d11 XL |
542 | where |
543 | E: Error, | |
544 | { | |
545 | *self.0 = v; | |
546 | Ok(()) | |
547 | } | |
548 | ||
8faf50e0 | 549 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
ff7c6d11 XL |
550 | where |
551 | E: Error, | |
552 | { | |
553 | match str::from_utf8(v) { | |
554 | Ok(s) => { | |
555 | self.0.clear(); | |
556 | self.0.push_str(s); | |
557 | Ok(()) | |
558 | } | |
559 | Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | |
560 | } | |
561 | } | |
562 | ||
8faf50e0 | 563 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
ff7c6d11 XL |
564 | where |
565 | E: Error, | |
566 | { | |
567 | match String::from_utf8(v) { | |
568 | Ok(s) => { | |
569 | *self.0 = s; | |
570 | Ok(()) | |
571 | } | |
572 | Err(e) => Err(Error::invalid_value( | |
573 | Unexpected::Bytes(&e.into_bytes()), | |
574 | &self, | |
575 | )), | |
041b39d2 XL |
576 | } |
577 | } | |
578 | } | |
579 | ||
580 | #[cfg(any(feature = "std", feature = "alloc"))] | |
581 | impl<'de> Deserialize<'de> for String { | |
8faf50e0 | 582 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
583 | where |
584 | D: Deserializer<'de>, | |
585 | { | |
586 | deserializer.deserialize_string(StringVisitor) | |
587 | } | |
ff7c6d11 XL |
588 | |
589 | fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> | |
590 | where | |
591 | D: Deserializer<'de>, | |
592 | { | |
593 | deserializer.deserialize_string(StringInPlaceVisitor(place)) | |
594 | } | |
041b39d2 XL |
595 | } |
596 | ||
597 | //////////////////////////////////////////////////////////////////////////////// | |
598 | ||
599 | struct StrVisitor; | |
600 | ||
601 | impl<'a> Visitor<'a> for StrVisitor { | |
602 | type Value = &'a str; | |
603 | ||
604 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
605 | formatter.write_str("a borrowed string") | |
606 | } | |
607 | ||
608 | fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> | |
609 | where | |
610 | E: Error, | |
611 | { | |
612 | Ok(v) // so easy | |
613 | } | |
614 | ||
615 | fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> | |
616 | where | |
617 | E: Error, | |
618 | { | |
619 | str::from_utf8(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) | |
620 | } | |
621 | } | |
622 | ||
623 | impl<'de: 'a, 'a> Deserialize<'de> for &'a str { | |
624 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
625 | where | |
626 | D: Deserializer<'de>, | |
627 | { | |
628 | deserializer.deserialize_str(StrVisitor) | |
629 | } | |
630 | } | |
631 | ||
632 | //////////////////////////////////////////////////////////////////////////////// | |
633 | ||
634 | struct BytesVisitor; | |
635 | ||
636 | impl<'a> Visitor<'a> for BytesVisitor { | |
637 | type Value = &'a [u8]; | |
638 | ||
639 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
640 | formatter.write_str("a borrowed byte array") | |
641 | } | |
642 | ||
643 | fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> | |
644 | where | |
645 | E: Error, | |
646 | { | |
647 | Ok(v) | |
648 | } | |
649 | ||
650 | fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> | |
651 | where | |
652 | E: Error, | |
653 | { | |
654 | Ok(v.as_bytes()) | |
655 | } | |
656 | } | |
657 | ||
658 | impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] { | |
659 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
660 | where | |
661 | D: Deserializer<'de>, | |
662 | { | |
663 | deserializer.deserialize_bytes(BytesVisitor) | |
664 | } | |
665 | } | |
666 | ||
667 | //////////////////////////////////////////////////////////////////////////////// | |
668 | ||
353b0b11 | 669 | #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
041b39d2 XL |
670 | struct CStringVisitor; |
671 | ||
353b0b11 | 672 | #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
041b39d2 XL |
673 | impl<'de> Visitor<'de> for CStringVisitor { |
674 | type Value = CString; | |
675 | ||
676 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
677 | formatter.write_str("byte array") | |
678 | } | |
679 | ||
8faf50e0 | 680 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
681 | where |
682 | A: SeqAccess<'de>, | |
683 | { | |
684 | let len = size_hint::cautious(seq.size_hint()); | |
685 | let mut values = Vec::with_capacity(len); | |
686 | ||
687 | while let Some(value) = try!(seq.next_element()) { | |
688 | values.push(value); | |
689 | } | |
690 | ||
691 | CString::new(values).map_err(Error::custom) | |
692 | } | |
693 | ||
8faf50e0 | 694 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
041b39d2 XL |
695 | where |
696 | E: Error, | |
697 | { | |
698 | CString::new(v).map_err(Error::custom) | |
699 | } | |
700 | ||
8faf50e0 | 701 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
041b39d2 XL |
702 | where |
703 | E: Error, | |
704 | { | |
705 | CString::new(v).map_err(Error::custom) | |
706 | } | |
707 | ||
8faf50e0 | 708 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
041b39d2 XL |
709 | where |
710 | E: Error, | |
711 | { | |
712 | CString::new(v).map_err(Error::custom) | |
713 | } | |
714 | ||
8faf50e0 | 715 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
041b39d2 XL |
716 | where |
717 | E: Error, | |
718 | { | |
719 | CString::new(v).map_err(Error::custom) | |
720 | } | |
721 | } | |
722 | ||
353b0b11 | 723 | #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] |
041b39d2 | 724 | impl<'de> Deserialize<'de> for CString { |
8faf50e0 | 725 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
726 | where |
727 | D: Deserializer<'de>, | |
728 | { | |
729 | deserializer.deserialize_byte_buf(CStringVisitor) | |
730 | } | |
731 | } | |
732 | ||
3b2f2976 | 733 | macro_rules! forwarded_impl { |
83c7162d XL |
734 | ( |
735 | $(#[doc = $doc:tt])* | |
9ffffee4 | 736 | ($($id:ident),*), $ty:ty, $func:expr |
83c7162d XL |
737 | ) => { |
738 | $(#[doc = $doc])* | |
3b2f2976 XL |
739 | impl<'de $(, $id : Deserialize<'de>,)*> Deserialize<'de> for $ty { |
740 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
741 | where | |
742 | D: Deserializer<'de>, | |
743 | { | |
744 | Deserialize::deserialize(deserializer).map($func) | |
745 | } | |
746 | } | |
041b39d2 XL |
747 | } |
748 | } | |
749 | ||
353b0b11 FG |
750 | #[cfg(all( |
751 | any(feature = "std", all(not(no_core_cstr), feature = "alloc")), | |
752 | not(no_de_boxed_c_str) | |
753 | ))] | |
3b2f2976 XL |
754 | forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str); |
755 | ||
5099ac24 | 756 | #[cfg(not(no_core_reverse))] |
dc9dc135 XL |
757 | forwarded_impl!((T), Reverse<T>, Reverse); |
758 | ||
041b39d2 XL |
759 | //////////////////////////////////////////////////////////////////////////////// |
760 | ||
761 | struct OptionVisitor<T> { | |
762 | marker: PhantomData<T>, | |
763 | } | |
764 | ||
765 | impl<'de, T> Visitor<'de> for OptionVisitor<T> | |
766 | where | |
767 | T: Deserialize<'de>, | |
768 | { | |
769 | type Value = Option<T>; | |
770 | ||
771 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
772 | formatter.write_str("option") | |
773 | } | |
774 | ||
775 | #[inline] | |
8faf50e0 | 776 | fn visit_unit<E>(self) -> Result<Self::Value, E> |
041b39d2 XL |
777 | where |
778 | E: Error, | |
779 | { | |
780 | Ok(None) | |
781 | } | |
782 | ||
783 | #[inline] | |
8faf50e0 | 784 | fn visit_none<E>(self) -> Result<Self::Value, E> |
041b39d2 XL |
785 | where |
786 | E: Error, | |
787 | { | |
788 | Ok(None) | |
789 | } | |
790 | ||
791 | #[inline] | |
8faf50e0 | 792 | fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
041b39d2 XL |
793 | where |
794 | D: Deserializer<'de>, | |
795 | { | |
796 | T::deserialize(deserializer).map(Some) | |
797 | } | |
8faf50e0 | 798 | |
8faf50e0 XL |
799 | fn __private_visit_untagged_option<D>(self, deserializer: D) -> Result<Self::Value, ()> |
800 | where | |
801 | D: Deserializer<'de>, | |
802 | { | |
803 | Ok(T::deserialize(deserializer).ok()) | |
804 | } | |
041b39d2 XL |
805 | } |
806 | ||
807 | impl<'de, T> Deserialize<'de> for Option<T> | |
808 | where | |
809 | T: Deserialize<'de>, | |
810 | { | |
8faf50e0 | 811 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
812 | where |
813 | D: Deserializer<'de>, | |
814 | { | |
ff7c6d11 XL |
815 | deserializer.deserialize_option(OptionVisitor { |
816 | marker: PhantomData, | |
817 | }) | |
041b39d2 | 818 | } |
ff7c6d11 XL |
819 | |
820 | // The Some variant's repr is opaque, so we can't play cute tricks with its | |
821 | // tag to have deserialize_in_place build the content in place unconditionally. | |
822 | // | |
823 | // FIXME: investigate whether branching on the old value being Some to | |
824 | // deserialize_in_place the value is profitable (probably data-dependent?) | |
041b39d2 XL |
825 | } |
826 | ||
827 | //////////////////////////////////////////////////////////////////////////////// | |
828 | ||
2c00a5a8 | 829 | struct PhantomDataVisitor<T: ?Sized> { |
041b39d2 XL |
830 | marker: PhantomData<T>, |
831 | } | |
832 | ||
2c00a5a8 | 833 | impl<'de, T: ?Sized> Visitor<'de> for PhantomDataVisitor<T> { |
041b39d2 XL |
834 | type Value = PhantomData<T>; |
835 | ||
836 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
837 | formatter.write_str("unit") | |
838 | } | |
839 | ||
840 | #[inline] | |
8faf50e0 | 841 | fn visit_unit<E>(self) -> Result<Self::Value, E> |
041b39d2 XL |
842 | where |
843 | E: Error, | |
844 | { | |
845 | Ok(PhantomData) | |
846 | } | |
847 | } | |
848 | ||
2c00a5a8 | 849 | impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> { |
8faf50e0 | 850 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
851 | where |
852 | D: Deserializer<'de>, | |
853 | { | |
ff7c6d11 XL |
854 | let visitor = PhantomDataVisitor { |
855 | marker: PhantomData, | |
856 | }; | |
041b39d2 XL |
857 | deserializer.deserialize_unit_struct("PhantomData", visitor) |
858 | } | |
859 | } | |
860 | ||
861 | //////////////////////////////////////////////////////////////////////////////// | |
862 | ||
863 | #[cfg(any(feature = "std", feature = "alloc"))] | |
864 | macro_rules! seq_impl { | |
865 | ( | |
9ffffee4 | 866 | $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, |
041b39d2 | 867 | $access:ident, |
ff7c6d11 | 868 | $clear:expr, |
041b39d2 | 869 | $with_capacity:expr, |
ff7c6d11 | 870 | $reserve:expr, |
041b39d2 XL |
871 | $insert:expr |
872 | ) => { | |
873 | impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*> | |
874 | where | |
875 | T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, | |
876 | $($typaram: $bound1 $(+ $bound2)*,)* | |
877 | { | |
878 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
879 | where | |
880 | D: Deserializer<'de>, | |
881 | { | |
882 | struct SeqVisitor<T $(, $typaram)*> { | |
883 | marker: PhantomData<$ty<T $(, $typaram)*>>, | |
884 | } | |
885 | ||
886 | impl<'de, T $(, $typaram)*> Visitor<'de> for SeqVisitor<T $(, $typaram)*> | |
887 | where | |
888 | T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, | |
889 | $($typaram: $bound1 $(+ $bound2)*,)* | |
890 | { | |
891 | type Value = $ty<T $(, $typaram)*>; | |
892 | ||
893 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
894 | formatter.write_str("a sequence") | |
895 | } | |
896 | ||
897 | #[inline] | |
898 | fn visit_seq<A>(self, mut $access: A) -> Result<Self::Value, A::Error> | |
899 | where | |
900 | A: SeqAccess<'de>, | |
901 | { | |
902 | let mut values = $with_capacity; | |
903 | ||
904 | while let Some(value) = try!($access.next_element()) { | |
905 | $insert(&mut values, value); | |
906 | } | |
907 | ||
908 | Ok(values) | |
909 | } | |
910 | } | |
911 | ||
912 | let visitor = SeqVisitor { marker: PhantomData }; | |
913 | deserializer.deserialize_seq(visitor) | |
914 | } | |
ff7c6d11 XL |
915 | |
916 | fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> | |
917 | where | |
918 | D: Deserializer<'de>, | |
919 | { | |
920 | struct SeqInPlaceVisitor<'a, T: 'a $(, $typaram: 'a)*>(&'a mut $ty<T $(, $typaram)*>); | |
921 | ||
922 | impl<'a, 'de, T $(, $typaram)*> Visitor<'de> for SeqInPlaceVisitor<'a, T $(, $typaram)*> | |
923 | where | |
924 | T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, | |
925 | $($typaram: $bound1 $(+ $bound2)*,)* | |
926 | { | |
927 | type Value = (); | |
928 | ||
929 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
930 | formatter.write_str("a sequence") | |
931 | } | |
932 | ||
933 | #[inline] | |
8faf50e0 | 934 | fn visit_seq<A>(mut self, mut $access: A) -> Result<Self::Value, A::Error> |
ff7c6d11 XL |
935 | where |
936 | A: SeqAccess<'de>, | |
937 | { | |
938 | $clear(&mut self.0); | |
939 | $reserve(&mut self.0, size_hint::cautious($access.size_hint())); | |
940 | ||
941 | // FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList) | |
942 | while let Some(value) = try!($access.next_element()) { | |
943 | $insert(&mut self.0, value); | |
944 | } | |
945 | ||
946 | Ok(()) | |
947 | } | |
948 | } | |
949 | ||
950 | deserializer.deserialize_seq(SeqInPlaceVisitor(place)) | |
951 | } | |
041b39d2 XL |
952 | } |
953 | } | |
954 | } | |
955 | ||
ff7c6d11 XL |
956 | // Dummy impl of reserve |
957 | #[cfg(any(feature = "std", feature = "alloc"))] | |
958 | fn nop_reserve<T>(_seq: T, _n: usize) {} | |
959 | ||
041b39d2 XL |
960 | #[cfg(any(feature = "std", feature = "alloc"))] |
961 | seq_impl!( | |
962 | BinaryHeap<T: Ord>, | |
963 | seq, | |
ff7c6d11 | 964 | BinaryHeap::clear, |
041b39d2 | 965 | BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())), |
ff7c6d11 | 966 | BinaryHeap::reserve, |
e1599b0c XL |
967 | BinaryHeap::push |
968 | ); | |
041b39d2 XL |
969 | |
970 | #[cfg(any(feature = "std", feature = "alloc"))] | |
971 | seq_impl!( | |
972 | BTreeSet<T: Eq + Ord>, | |
973 | seq, | |
ff7c6d11 | 974 | BTreeSet::clear, |
041b39d2 | 975 | BTreeSet::new(), |
ff7c6d11 | 976 | nop_reserve, |
e1599b0c XL |
977 | BTreeSet::insert |
978 | ); | |
041b39d2 XL |
979 | |
980 | #[cfg(any(feature = "std", feature = "alloc"))] | |
981 | seq_impl!( | |
982 | LinkedList<T>, | |
983 | seq, | |
ff7c6d11 | 984 | LinkedList::clear, |
041b39d2 | 985 | LinkedList::new(), |
ff7c6d11 XL |
986 | nop_reserve, |
987 | LinkedList::push_back | |
988 | ); | |
041b39d2 XL |
989 | |
990 | #[cfg(feature = "std")] | |
991 | seq_impl!( | |
992 | HashSet<T: Eq + Hash, S: BuildHasher + Default>, | |
993 | seq, | |
ff7c6d11 | 994 | HashSet::clear, |
041b39d2 | 995 | HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()), |
ff7c6d11 | 996 | HashSet::reserve, |
041b39d2 XL |
997 | HashSet::insert); |
998 | ||
041b39d2 XL |
999 | #[cfg(any(feature = "std", feature = "alloc"))] |
1000 | seq_impl!( | |
1001 | VecDeque<T>, | |
1002 | seq, | |
ff7c6d11 | 1003 | VecDeque::clear, |
041b39d2 | 1004 | VecDeque::with_capacity(size_hint::cautious(seq.size_hint())), |
ff7c6d11 XL |
1005 | VecDeque::reserve, |
1006 | VecDeque::push_back | |
1007 | ); | |
041b39d2 XL |
1008 | |
1009 | //////////////////////////////////////////////////////////////////////////////// | |
1010 | ||
0731742a XL |
1011 | #[cfg(any(feature = "std", feature = "alloc"))] |
1012 | impl<'de, T> Deserialize<'de> for Vec<T> | |
1013 | where | |
1014 | T: Deserialize<'de>, | |
1015 | { | |
1016 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1017 | where | |
1018 | D: Deserializer<'de>, | |
1019 | { | |
1020 | struct VecVisitor<T> { | |
1021 | marker: PhantomData<T>, | |
1022 | } | |
1023 | ||
1024 | impl<'de, T> Visitor<'de> for VecVisitor<T> | |
1025 | where | |
1026 | T: Deserialize<'de>, | |
1027 | { | |
1028 | type Value = Vec<T>; | |
1029 | ||
1030 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1031 | formatter.write_str("a sequence") | |
1032 | } | |
1033 | ||
1034 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | |
1035 | where | |
1036 | A: SeqAccess<'de>, | |
1037 | { | |
1038 | let mut values = Vec::with_capacity(size_hint::cautious(seq.size_hint())); | |
1039 | ||
1040 | while let Some(value) = try!(seq.next_element()) { | |
1041 | values.push(value); | |
1042 | } | |
1043 | ||
1044 | Ok(values) | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | let visitor = VecVisitor { | |
1049 | marker: PhantomData, | |
1050 | }; | |
1051 | deserializer.deserialize_seq(visitor) | |
1052 | } | |
1053 | ||
1054 | fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> | |
1055 | where | |
1056 | D: Deserializer<'de>, | |
1057 | { | |
1058 | struct VecInPlaceVisitor<'a, T: 'a>(&'a mut Vec<T>); | |
1059 | ||
1060 | impl<'a, 'de, T> Visitor<'de> for VecInPlaceVisitor<'a, T> | |
1061 | where | |
1062 | T: Deserialize<'de>, | |
1063 | { | |
1064 | type Value = (); | |
1065 | ||
1066 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1067 | formatter.write_str("a sequence") | |
1068 | } | |
1069 | ||
1070 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> | |
1071 | where | |
1072 | A: SeqAccess<'de>, | |
1073 | { | |
1074 | let hint = size_hint::cautious(seq.size_hint()); | |
1075 | if let Some(additional) = hint.checked_sub(self.0.len()) { | |
1076 | self.0.reserve(additional); | |
1077 | } | |
1078 | ||
1079 | for i in 0..self.0.len() { | |
1080 | let next = { | |
1081 | let next_place = InPlaceSeed(&mut self.0[i]); | |
1082 | try!(seq.next_element_seed(next_place)) | |
1083 | }; | |
1084 | if next.is_none() { | |
1085 | self.0.truncate(i); | |
1086 | return Ok(()); | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | while let Some(value) = try!(seq.next_element()) { | |
1091 | self.0.push(value); | |
1092 | } | |
1093 | ||
1094 | Ok(()) | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | deserializer.deserialize_seq(VecInPlaceVisitor(place)) | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | //////////////////////////////////////////////////////////////////////////////// | |
1103 | ||
041b39d2 XL |
1104 | struct ArrayVisitor<A> { |
1105 | marker: PhantomData<A>, | |
1106 | } | |
ff7c6d11 | 1107 | struct ArrayInPlaceVisitor<'a, A: 'a>(&'a mut A); |
041b39d2 XL |
1108 | |
1109 | impl<A> ArrayVisitor<A> { | |
1110 | fn new() -> Self { | |
ff7c6d11 XL |
1111 | ArrayVisitor { |
1112 | marker: PhantomData, | |
1113 | } | |
041b39d2 XL |
1114 | } |
1115 | } | |
1116 | ||
1117 | impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> { | |
1118 | type Value = [T; 0]; | |
1119 | ||
1120 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1121 | formatter.write_str("an empty array") | |
1122 | } | |
1123 | ||
1124 | #[inline] | |
8faf50e0 | 1125 | fn visit_seq<A>(self, _: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
1126 | where |
1127 | A: SeqAccess<'de>, | |
1128 | { | |
1129 | Ok([]) | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | // Does not require T: Deserialize<'de>. | |
1134 | impl<'de, T> Deserialize<'de> for [T; 0] { | |
8faf50e0 | 1135 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1136 | where |
1137 | D: Deserializer<'de>, | |
1138 | { | |
1139 | deserializer.deserialize_tuple(0, ArrayVisitor::<[T; 0]>::new()) | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | macro_rules! array_impls { | |
dc9dc135 | 1144 | ($($len:expr => ($($n:tt)+))+) => { |
041b39d2 XL |
1145 | $( |
1146 | impl<'de, T> Visitor<'de> for ArrayVisitor<[T; $len]> | |
1147 | where | |
1148 | T: Deserialize<'de>, | |
1149 | { | |
1150 | type Value = [T; $len]; | |
1151 | ||
1152 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1153 | formatter.write_str(concat!("an array of length ", $len)) | |
1154 | } | |
1155 | ||
1156 | #[inline] | |
8faf50e0 | 1157 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
1158 | where |
1159 | A: SeqAccess<'de>, | |
1160 | { | |
dc9dc135 XL |
1161 | Ok([$( |
1162 | match try!(seq.next_element()) { | |
041b39d2 XL |
1163 | Some(val) => val, |
1164 | None => return Err(Error::invalid_length($n, &self)), | |
dc9dc135 XL |
1165 | } |
1166 | ),+]) | |
041b39d2 XL |
1167 | } |
1168 | } | |
1169 | ||
ff7c6d11 XL |
1170 | impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; $len]> |
1171 | where | |
1172 | T: Deserialize<'de>, | |
1173 | { | |
1174 | type Value = (); | |
1175 | ||
1176 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1177 | formatter.write_str(concat!("an array of length ", $len)) | |
1178 | } | |
1179 | ||
1180 | #[inline] | |
8faf50e0 | 1181 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
ff7c6d11 XL |
1182 | where |
1183 | A: SeqAccess<'de>, | |
1184 | { | |
1185 | let mut fail_idx = None; | |
1186 | for (idx, dest) in self.0[..].iter_mut().enumerate() { | |
1187 | if try!(seq.next_element_seed(InPlaceSeed(dest))).is_none() { | |
1188 | fail_idx = Some(idx); | |
1189 | break; | |
1190 | } | |
1191 | } | |
1192 | if let Some(idx) = fail_idx { | |
1193 | return Err(Error::invalid_length(idx, &self)); | |
1194 | } | |
1195 | Ok(()) | |
1196 | } | |
1197 | } | |
1198 | ||
041b39d2 XL |
1199 | impl<'de, T> Deserialize<'de> for [T; $len] |
1200 | where | |
1201 | T: Deserialize<'de>, | |
1202 | { | |
8faf50e0 | 1203 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1204 | where |
1205 | D: Deserializer<'de>, | |
1206 | { | |
1207 | deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new()) | |
1208 | } | |
ff7c6d11 XL |
1209 | |
1210 | fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> | |
1211 | where | |
1212 | D: Deserializer<'de>, | |
1213 | { | |
1214 | deserializer.deserialize_tuple($len, ArrayInPlaceVisitor(place)) | |
1215 | } | |
041b39d2 XL |
1216 | } |
1217 | )+ | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | array_impls! { | |
dc9dc135 XL |
1222 | 1 => (0) |
1223 | 2 => (0 1) | |
1224 | 3 => (0 1 2) | |
1225 | 4 => (0 1 2 3) | |
1226 | 5 => (0 1 2 3 4) | |
1227 | 6 => (0 1 2 3 4 5) | |
1228 | 7 => (0 1 2 3 4 5 6) | |
1229 | 8 => (0 1 2 3 4 5 6 7) | |
1230 | 9 => (0 1 2 3 4 5 6 7 8) | |
1231 | 10 => (0 1 2 3 4 5 6 7 8 9) | |
1232 | 11 => (0 1 2 3 4 5 6 7 8 9 10) | |
1233 | 12 => (0 1 2 3 4 5 6 7 8 9 10 11) | |
1234 | 13 => (0 1 2 3 4 5 6 7 8 9 10 11 12) | |
1235 | 14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13) | |
1236 | 15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14) | |
1237 | 16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) | |
1238 | 17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) | |
1239 | 18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17) | |
1240 | 19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) | |
1241 | 20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) | |
1242 | 21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) | |
1243 | 22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21) | |
1244 | 23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22) | |
1245 | 24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23) | |
1246 | 25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) | |
1247 | 26 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25) | |
1248 | 27 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26) | |
1249 | 28 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27) | |
1250 | 29 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28) | |
1251 | 30 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29) | |
1252 | 31 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30) | |
1253 | 32 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) | |
041b39d2 XL |
1254 | } |
1255 | ||
1256 | //////////////////////////////////////////////////////////////////////////////// | |
1257 | ||
1258 | macro_rules! tuple_impls { | |
ff7c6d11 | 1259 | ($($len:tt => ($($n:tt $name:ident)+))+) => { |
041b39d2 | 1260 | $( |
ff7c6d11 XL |
1261 | impl<'de, $($name: Deserialize<'de>),+> Deserialize<'de> for ($($name,)+) { |
1262 | #[inline] | |
8faf50e0 | 1263 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
ff7c6d11 XL |
1264 | where |
1265 | D: Deserializer<'de>, | |
1266 | { | |
1267 | struct TupleVisitor<$($name,)+> { | |
1268 | marker: PhantomData<($($name,)+)>, | |
1269 | } | |
041b39d2 | 1270 | |
ff7c6d11 XL |
1271 | impl<'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleVisitor<$($name,)+> { |
1272 | type Value = ($($name,)+); | |
041b39d2 | 1273 | |
ff7c6d11 XL |
1274 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1275 | formatter.write_str(concat!("a tuple of size ", $len)) | |
1276 | } | |
041b39d2 | 1277 | |
ff7c6d11 XL |
1278 | #[inline] |
1279 | #[allow(non_snake_case)] | |
8faf50e0 | 1280 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
ff7c6d11 XL |
1281 | where |
1282 | A: SeqAccess<'de>, | |
1283 | { | |
1284 | $( | |
1285 | let $name = match try!(seq.next_element()) { | |
1286 | Some(value) => value, | |
1287 | None => return Err(Error::invalid_length($n, &self)), | |
1288 | }; | |
1289 | )+ | |
041b39d2 | 1290 | |
ff7c6d11 XL |
1291 | Ok(($($name,)+)) |
1292 | } | |
1293 | } | |
041b39d2 | 1294 | |
ff7c6d11 | 1295 | deserializer.deserialize_tuple($len, TupleVisitor { marker: PhantomData }) |
041b39d2 | 1296 | } |
041b39d2 | 1297 | |
041b39d2 | 1298 | #[inline] |
ff7c6d11 | 1299 | fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error> |
041b39d2 XL |
1300 | where |
1301 | D: Deserializer<'de>, | |
1302 | { | |
ff7c6d11 XL |
1303 | struct TupleInPlaceVisitor<'a, $($name: 'a,)+>(&'a mut ($($name,)+)); |
1304 | ||
1305 | impl<'a, 'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleInPlaceVisitor<'a, $($name,)+> { | |
1306 | type Value = (); | |
1307 | ||
1308 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1309 | formatter.write_str(concat!("a tuple of size ", $len)) | |
1310 | } | |
1311 | ||
1312 | #[inline] | |
1313 | #[allow(non_snake_case)] | |
8faf50e0 | 1314 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
ff7c6d11 XL |
1315 | where |
1316 | A: SeqAccess<'de>, | |
1317 | { | |
1318 | $( | |
1319 | if try!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() { | |
1320 | return Err(Error::invalid_length($n, &self)); | |
1321 | } | |
1322 | )+ | |
1323 | ||
1324 | Ok(()) | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | deserializer.deserialize_tuple($len, TupleInPlaceVisitor(place)) | |
041b39d2 XL |
1329 | } |
1330 | } | |
1331 | )+ | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | tuple_impls! { | |
ff7c6d11 XL |
1336 | 1 => (0 T0) |
1337 | 2 => (0 T0 1 T1) | |
1338 | 3 => (0 T0 1 T1 2 T2) | |
1339 | 4 => (0 T0 1 T1 2 T2 3 T3) | |
1340 | 5 => (0 T0 1 T1 2 T2 3 T3 4 T4) | |
1341 | 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5) | |
1342 | 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6) | |
1343 | 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7) | |
1344 | 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8) | |
1345 | 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9) | |
1346 | 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10) | |
1347 | 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11) | |
1348 | 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12) | |
1349 | 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13) | |
1350 | 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14) | |
1351 | 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15) | |
041b39d2 XL |
1352 | } |
1353 | ||
1354 | //////////////////////////////////////////////////////////////////////////////// | |
1355 | ||
1356 | #[cfg(any(feature = "std", feature = "alloc"))] | |
1357 | macro_rules! map_impl { | |
1358 | ( | |
9ffffee4 | 1359 | $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, |
041b39d2 | 1360 | $access:ident, |
041b39d2 XL |
1361 | $with_capacity:expr |
1362 | ) => { | |
1363 | impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*> | |
1364 | where | |
1365 | K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, | |
1366 | V: Deserialize<'de>, | |
1367 | $($typaram: $bound1 $(+ $bound2)*),* | |
1368 | { | |
1369 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1370 | where | |
1371 | D: Deserializer<'de>, | |
1372 | { | |
1373 | struct MapVisitor<K, V $(, $typaram)*> { | |
1374 | marker: PhantomData<$ty<K, V $(, $typaram)*>>, | |
1375 | } | |
1376 | ||
1377 | impl<'de, K, V $(, $typaram)*> Visitor<'de> for MapVisitor<K, V $(, $typaram)*> | |
1378 | where | |
1379 | K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, | |
1380 | V: Deserialize<'de>, | |
1381 | $($typaram: $bound1 $(+ $bound2)*),* | |
1382 | { | |
1383 | type Value = $ty<K, V $(, $typaram)*>; | |
1384 | ||
1385 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1386 | formatter.write_str("a map") | |
1387 | } | |
1388 | ||
1389 | #[inline] | |
1390 | fn visit_map<A>(self, mut $access: A) -> Result<Self::Value, A::Error> | |
1391 | where | |
1392 | A: MapAccess<'de>, | |
1393 | { | |
1394 | let mut values = $with_capacity; | |
1395 | ||
1396 | while let Some((key, value)) = try!($access.next_entry()) { | |
1397 | values.insert(key, value); | |
1398 | } | |
1399 | ||
1400 | Ok(values) | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | let visitor = MapVisitor { marker: PhantomData }; | |
1405 | deserializer.deserialize_map(visitor) | |
1406 | } | |
1407 | } | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | #[cfg(any(feature = "std", feature = "alloc"))] | |
1412 | map_impl!( | |
1413 | BTreeMap<K: Ord, V>, | |
1414 | map, | |
041b39d2 XL |
1415 | BTreeMap::new()); |
1416 | ||
1417 | #[cfg(feature = "std")] | |
1418 | map_impl!( | |
1419 | HashMap<K: Eq + Hash, V, S: BuildHasher + Default>, | |
1420 | map, | |
041b39d2 XL |
1421 | HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default())); |
1422 | ||
1423 | //////////////////////////////////////////////////////////////////////////////// | |
1424 | ||
1425 | #[cfg(feature = "std")] | |
abe05a73 | 1426 | macro_rules! parse_ip_impl { |
8faf50e0 | 1427 | ($expecting:tt $ty:ty; $size:tt) => { |
041b39d2 XL |
1428 | impl<'de> Deserialize<'de> for $ty { |
1429 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1430 | where | |
1431 | D: Deserializer<'de>, | |
1432 | { | |
abe05a73 | 1433 | if deserializer.is_human_readable() { |
5869c6ff | 1434 | deserializer.deserialize_str(FromStrVisitor::new($expecting)) |
abe05a73 XL |
1435 | } else { |
1436 | <[u8; $size]>::deserialize(deserializer).map(<$ty>::from) | |
1437 | } | |
1438 | } | |
1439 | } | |
83c7162d | 1440 | }; |
abe05a73 XL |
1441 | } |
1442 | ||
1443 | #[cfg(feature = "std")] | |
1444 | macro_rules! variant_identifier { | |
1445 | ( | |
9ffffee4 FG |
1446 | $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) |
1447 | $expecting_message:expr, | |
1448 | $variants_name:ident | |
abe05a73 XL |
1449 | ) => { |
1450 | enum $name_kind { | |
9ffffee4 | 1451 | $($variant),* |
abe05a73 XL |
1452 | } |
1453 | ||
9ffffee4 | 1454 | static $variants_name: &'static [&'static str] = &[$(stringify!($variant)),*]; |
abe05a73 XL |
1455 | |
1456 | impl<'de> Deserialize<'de> for $name_kind { | |
1457 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1458 | where | |
1459 | D: Deserializer<'de>, | |
1460 | { | |
1461 | struct KindVisitor; | |
1462 | ||
1463 | impl<'de> Visitor<'de> for KindVisitor { | |
1464 | type Value = $name_kind; | |
1465 | ||
1466 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1467 | formatter.write_str($expecting_message) | |
1468 | } | |
1469 | ||
1b1a35ee | 1470 | fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
abe05a73 XL |
1471 | where |
1472 | E: Error, | |
1473 | { | |
1474 | match value { | |
1475 | $( | |
1476 | $index => Ok($name_kind :: $variant), | |
1477 | )* | |
1b1a35ee | 1478 | _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),), |
abe05a73 XL |
1479 | } |
1480 | } | |
1481 | ||
1482 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | |
1483 | where | |
1484 | E: Error, | |
1485 | { | |
1486 | match value { | |
1487 | $( | |
1488 | stringify!($variant) => Ok($name_kind :: $variant), | |
1489 | )* | |
1490 | _ => Err(Error::unknown_variant(value, $variants_name)), | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> | |
1495 | where | |
1496 | E: Error, | |
1497 | { | |
1498 | match value { | |
1499 | $( | |
1500 | $bytes => Ok($name_kind :: $variant), | |
1501 | )* | |
1502 | _ => { | |
1503 | match str::from_utf8(value) { | |
1504 | Ok(value) => Err(Error::unknown_variant(value, $variants_name)), | |
1505 | Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)), | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | deserializer.deserialize_identifier(KindVisitor) | |
1513 | } | |
1514 | } | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | #[cfg(feature = "std")] | |
1519 | macro_rules! deserialize_enum { | |
1520 | ( | |
9ffffee4 FG |
1521 | $name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) |
1522 | $expecting_message:expr, | |
1523 | $deserializer:expr | |
abe05a73 | 1524 | ) => { |
9ffffee4 FG |
1525 | variant_identifier! { |
1526 | $name_kind ($($variant; $bytes; $index),*) | |
abe05a73 XL |
1527 | $expecting_message, |
1528 | VARIANTS | |
1529 | } | |
1530 | ||
1531 | struct EnumVisitor; | |
1532 | impl<'de> Visitor<'de> for EnumVisitor { | |
1533 | type Value = $name; | |
1534 | ||
1535 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1536 | formatter.write_str(concat!("a ", stringify!($name))) | |
1537 | } | |
1538 | ||
1539 | ||
1540 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> | |
1541 | where | |
1542 | A: EnumAccess<'de>, | |
1543 | { | |
1544 | match try!(data.variant()) { | |
1545 | $( | |
1546 | ($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant), | |
1547 | )* | |
1548 | } | |
1549 | } | |
1550 | } | |
1551 | $deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor) | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | #[cfg(feature = "std")] | |
1556 | impl<'de> Deserialize<'de> for net::IpAddr { | |
1557 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1558 | where | |
1559 | D: Deserializer<'de>, | |
1560 | { | |
1561 | if deserializer.is_human_readable() { | |
5869c6ff | 1562 | deserializer.deserialize_str(FromStrVisitor::new("IP address")) |
abe05a73 XL |
1563 | } else { |
1564 | use lib::net::IpAddr; | |
0731742a | 1565 | deserialize_enum! { |
abe05a73 XL |
1566 | IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
1567 | "`V4` or `V6`", | |
1568 | deserializer | |
041b39d2 XL |
1569 | } |
1570 | } | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | #[cfg(feature = "std")] | |
8faf50e0 | 1575 | parse_ip_impl!("IPv4 address" net::Ipv4Addr; 4); |
041b39d2 XL |
1576 | |
1577 | #[cfg(feature = "std")] | |
8faf50e0 | 1578 | parse_ip_impl!("IPv6 address" net::Ipv6Addr; 16); |
041b39d2 XL |
1579 | |
1580 | #[cfg(feature = "std")] | |
abe05a73 | 1581 | macro_rules! parse_socket_impl { |
8faf50e0 | 1582 | ($expecting:tt $ty:ty, $new:expr) => { |
abe05a73 XL |
1583 | impl<'de> Deserialize<'de> for $ty { |
1584 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1585 | where | |
1586 | D: Deserializer<'de>, | |
1587 | { | |
1588 | if deserializer.is_human_readable() { | |
5869c6ff | 1589 | deserializer.deserialize_str(FromStrVisitor::new($expecting)) |
abe05a73 XL |
1590 | } else { |
1591 | <(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port)) | |
1592 | } | |
1593 | } | |
1594 | } | |
83c7162d | 1595 | }; |
abe05a73 | 1596 | } |
041b39d2 XL |
1597 | |
1598 | #[cfg(feature = "std")] | |
abe05a73 XL |
1599 | impl<'de> Deserialize<'de> for net::SocketAddr { |
1600 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1601 | where | |
1602 | D: Deserializer<'de>, | |
1603 | { | |
1604 | if deserializer.is_human_readable() { | |
5869c6ff | 1605 | deserializer.deserialize_str(FromStrVisitor::new("socket address")) |
abe05a73 XL |
1606 | } else { |
1607 | use lib::net::SocketAddr; | |
0731742a | 1608 | deserialize_enum! { |
abe05a73 XL |
1609 | SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
1610 | "`V4` or `V6`", | |
1611 | deserializer | |
1612 | } | |
1613 | } | |
1614 | } | |
1615 | } | |
041b39d2 XL |
1616 | |
1617 | #[cfg(feature = "std")] | |
8faf50e0 | 1618 | parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, net::SocketAddrV4::new); |
041b39d2 XL |
1619 | |
1620 | #[cfg(feature = "std")] | |
8faf50e0 | 1621 | parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new( |
83c7162d | 1622 | ip, port, 0, 0 |
ff7c6d11 | 1623 | )); |
041b39d2 XL |
1624 | |
1625 | //////////////////////////////////////////////////////////////////////////////// | |
1626 | ||
1627 | #[cfg(feature = "std")] | |
1628 | struct PathVisitor; | |
1629 | ||
1630 | #[cfg(feature = "std")] | |
1631 | impl<'a> Visitor<'a> for PathVisitor { | |
1632 | type Value = &'a Path; | |
1633 | ||
1634 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1635 | formatter.write_str("a borrowed path") | |
1636 | } | |
1637 | ||
1638 | fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> | |
1639 | where | |
1640 | E: Error, | |
1641 | { | |
1642 | Ok(v.as_ref()) | |
1643 | } | |
1644 | ||
1645 | fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> | |
1646 | where | |
1647 | E: Error, | |
1648 | { | |
1649 | str::from_utf8(v) | |
1650 | .map(AsRef::as_ref) | |
1651 | .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | #[cfg(feature = "std")] | |
1656 | impl<'de: 'a, 'a> Deserialize<'de> for &'a Path { | |
1657 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1658 | where | |
1659 | D: Deserializer<'de>, | |
1660 | { | |
1661 | deserializer.deserialize_str(PathVisitor) | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | #[cfg(feature = "std")] | |
1666 | struct PathBufVisitor; | |
1667 | ||
1668 | #[cfg(feature = "std")] | |
1669 | impl<'de> Visitor<'de> for PathBufVisitor { | |
1670 | type Value = PathBuf; | |
1671 | ||
1672 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1673 | formatter.write_str("path string") | |
1674 | } | |
1675 | ||
8faf50e0 | 1676 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
041b39d2 XL |
1677 | where |
1678 | E: Error, | |
1679 | { | |
1680 | Ok(From::from(v)) | |
1681 | } | |
1682 | ||
8faf50e0 | 1683 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
041b39d2 XL |
1684 | where |
1685 | E: Error, | |
1686 | { | |
1687 | Ok(From::from(v)) | |
1688 | } | |
f035d41b XL |
1689 | |
1690 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> | |
1691 | where | |
1692 | E: Error, | |
1693 | { | |
1694 | str::from_utf8(v) | |
1695 | .map(From::from) | |
1696 | .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) | |
1697 | } | |
1698 | ||
1699 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> | |
1700 | where | |
1701 | E: Error, | |
1702 | { | |
1703 | String::from_utf8(v) | |
1704 | .map(From::from) | |
1705 | .map_err(|e| Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)) | |
1706 | } | |
041b39d2 XL |
1707 | } |
1708 | ||
1709 | #[cfg(feature = "std")] | |
1710 | impl<'de> Deserialize<'de> for PathBuf { | |
8faf50e0 | 1711 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1712 | where |
1713 | D: Deserializer<'de>, | |
1714 | { | |
1715 | deserializer.deserialize_string(PathBufVisitor) | |
1716 | } | |
1717 | } | |
1718 | ||
5099ac24 | 1719 | #[cfg(all(feature = "std", not(no_de_boxed_path)))] |
f035d41b XL |
1720 | forwarded_impl!((), Box<Path>, PathBuf::into_boxed_path); |
1721 | ||
041b39d2 XL |
1722 | //////////////////////////////////////////////////////////////////////////////// |
1723 | ||
1724 | // If this were outside of the serde crate, it would just use: | |
1725 | // | |
1726 | // #[derive(Deserialize)] | |
1727 | // #[serde(variant_identifier)] | |
1728 | #[cfg(all(feature = "std", any(unix, windows)))] | |
0731742a | 1729 | variant_identifier! { |
abe05a73 XL |
1730 | OsStringKind (Unix; b"Unix"; 0, Windows; b"Windows"; 1) |
1731 | "`Unix` or `Windows`", | |
1732 | OSSTR_VARIANTS | |
041b39d2 XL |
1733 | } |
1734 | ||
1735 | #[cfg(all(feature = "std", any(unix, windows)))] | |
1736 | struct OsStringVisitor; | |
1737 | ||
1738 | #[cfg(all(feature = "std", any(unix, windows)))] | |
1739 | impl<'de> Visitor<'de> for OsStringVisitor { | |
1740 | type Value = OsString; | |
1741 | ||
1742 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1743 | formatter.write_str("os string") | |
1744 | } | |
1745 | ||
1746 | #[cfg(unix)] | |
8faf50e0 | 1747 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
1748 | where |
1749 | A: EnumAccess<'de>, | |
1750 | { | |
1751 | use std::os::unix::ffi::OsStringExt; | |
1752 | ||
1753 | match try!(data.variant()) { | |
1754 | (OsStringKind::Unix, v) => v.newtype_variant().map(OsString::from_vec), | |
ff7c6d11 XL |
1755 | (OsStringKind::Windows, _) => Err(Error::custom( |
1756 | "cannot deserialize Windows OS string on Unix", | |
1757 | )), | |
041b39d2 XL |
1758 | } |
1759 | } | |
1760 | ||
1761 | #[cfg(windows)] | |
8faf50e0 | 1762 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
1763 | where |
1764 | A: EnumAccess<'de>, | |
1765 | { | |
1766 | use std::os::windows::ffi::OsStringExt; | |
1767 | ||
1768 | match try!(data.variant()) { | |
8faf50e0 XL |
1769 | (OsStringKind::Windows, v) => v |
1770 | .newtype_variant::<Vec<u16>>() | |
ff7c6d11 XL |
1771 | .map(|vec| OsString::from_wide(&vec)), |
1772 | (OsStringKind::Unix, _) => Err(Error::custom( | |
1773 | "cannot deserialize Unix OS string on Windows", | |
1774 | )), | |
041b39d2 XL |
1775 | } |
1776 | } | |
1777 | } | |
1778 | ||
1779 | #[cfg(all(feature = "std", any(unix, windows)))] | |
1780 | impl<'de> Deserialize<'de> for OsString { | |
8faf50e0 | 1781 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1782 | where |
1783 | D: Deserializer<'de>, | |
1784 | { | |
1785 | deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor) | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | //////////////////////////////////////////////////////////////////////////////// | |
1790 | ||
1791 | #[cfg(any(feature = "std", feature = "alloc"))] | |
3b2f2976 | 1792 | forwarded_impl!((T), Box<T>, Box::new); |
041b39d2 XL |
1793 | |
1794 | #[cfg(any(feature = "std", feature = "alloc"))] | |
3b2f2976 | 1795 | forwarded_impl!((T), Box<[T]>, Vec::into_boxed_slice); |
041b39d2 XL |
1796 | |
1797 | #[cfg(any(feature = "std", feature = "alloc"))] | |
3b2f2976 | 1798 | forwarded_impl!((), Box<str>, String::into_boxed_str); |
041b39d2 | 1799 | |
5099ac24 | 1800 | #[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))] |
83c7162d XL |
1801 | forwarded_impl! { |
1802 | /// This impl requires the [`"rc"`] Cargo feature of Serde. | |
1803 | /// | |
1804 | /// Deserializing a data structure containing `Arc` will not attempt to | |
1805 | /// deduplicate `Arc` references to the same data. Every deserialized `Arc` | |
1806 | /// will end up with a strong count of 1. | |
1807 | /// | |
1808 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1809 | (T), Arc<T>, Arc::new | |
1810 | } | |
041b39d2 | 1811 | |
5099ac24 | 1812 | #[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))] |
83c7162d XL |
1813 | forwarded_impl! { |
1814 | /// This impl requires the [`"rc"`] Cargo feature of Serde. | |
1815 | /// | |
1816 | /// Deserializing a data structure containing `Rc` will not attempt to | |
1817 | /// deduplicate `Rc` references to the same data. Every deserialized `Rc` | |
1818 | /// will end up with a strong count of 1. | |
1819 | /// | |
1820 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1821 | (T), Rc<T>, Rc::new | |
1822 | } | |
041b39d2 XL |
1823 | |
1824 | #[cfg(any(feature = "std", feature = "alloc"))] | |
1825 | impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T> | |
1826 | where | |
1827 | T: ToOwned, | |
1828 | T::Owned: Deserialize<'de>, | |
1829 | { | |
1830 | #[inline] | |
8faf50e0 | 1831 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1832 | where |
1833 | D: Deserializer<'de>, | |
1834 | { | |
1835 | T::Owned::deserialize(deserializer).map(Cow::Owned) | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | //////////////////////////////////////////////////////////////////////////////// | |
ea8adc8c | 1840 | |
8faf50e0 XL |
1841 | /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting |
1842 | /// `Weak<T>` has a reference count of 0 and cannot be upgraded. | |
1843 | /// | |
1844 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1845 | #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] | |
1846 | impl<'de, T: ?Sized> Deserialize<'de> for RcWeak<T> | |
1847 | where | |
1848 | T: Deserialize<'de>, | |
1849 | { | |
1850 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1851 | where | |
1852 | D: Deserializer<'de>, | |
1853 | { | |
1854 | try!(Option::<T>::deserialize(deserializer)); | |
1855 | Ok(RcWeak::new()) | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting | |
1860 | /// `Weak<T>` has a reference count of 0 and cannot be upgraded. | |
1861 | /// | |
1862 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1863 | #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] | |
1864 | impl<'de, T: ?Sized> Deserialize<'de> for ArcWeak<T> | |
1865 | where | |
1866 | T: Deserialize<'de>, | |
1867 | { | |
1868 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1869 | where | |
1870 | D: Deserializer<'de>, | |
1871 | { | |
1872 | try!(Option::<T>::deserialize(deserializer)); | |
1873 | Ok(ArcWeak::new()) | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | //////////////////////////////////////////////////////////////////////////////// | |
1878 | ||
5099ac24 FG |
1879 | #[cfg(all( |
1880 | not(no_de_rc_dst), | |
1881 | feature = "rc", | |
1882 | any(feature = "std", feature = "alloc") | |
1883 | ))] | |
ea8adc8c | 1884 | macro_rules! box_forwarded_impl { |
83c7162d XL |
1885 | ( |
1886 | $(#[doc = $doc:tt])* | |
1887 | $t:ident | |
1888 | ) => { | |
1889 | $(#[doc = $doc])* | |
ea8adc8c XL |
1890 | impl<'de, T: ?Sized> Deserialize<'de> for $t<T> |
1891 | where | |
1892 | Box<T>: Deserialize<'de>, | |
1893 | { | |
1894 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1895 | where | |
1896 | D: Deserializer<'de>, | |
1897 | { | |
1898 | Box::deserialize(deserializer).map(Into::into) | |
1899 | } | |
1900 | } | |
83c7162d | 1901 | }; |
ea8adc8c XL |
1902 | } |
1903 | ||
5099ac24 FG |
1904 | #[cfg(all( |
1905 | not(no_de_rc_dst), | |
1906 | feature = "rc", | |
1907 | any(feature = "std", feature = "alloc") | |
1908 | ))] | |
83c7162d XL |
1909 | box_forwarded_impl! { |
1910 | /// This impl requires the [`"rc"`] Cargo feature of Serde. | |
1911 | /// | |
1912 | /// Deserializing a data structure containing `Rc` will not attempt to | |
1913 | /// deduplicate `Rc` references to the same data. Every deserialized `Rc` | |
1914 | /// will end up with a strong count of 1. | |
1915 | /// | |
1916 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1917 | Rc | |
1918 | } | |
ea8adc8c | 1919 | |
5099ac24 FG |
1920 | #[cfg(all( |
1921 | not(no_de_rc_dst), | |
1922 | feature = "rc", | |
1923 | any(feature = "std", feature = "alloc") | |
1924 | ))] | |
83c7162d XL |
1925 | box_forwarded_impl! { |
1926 | /// This impl requires the [`"rc"`] Cargo feature of Serde. | |
1927 | /// | |
1928 | /// Deserializing a data structure containing `Arc` will not attempt to | |
1929 | /// deduplicate `Arc` references to the same data. Every deserialized `Arc` | |
1930 | /// will end up with a strong count of 1. | |
1931 | /// | |
1932 | /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc | |
1933 | Arc | |
1934 | } | |
ea8adc8c XL |
1935 | |
1936 | //////////////////////////////////////////////////////////////////////////////// | |
041b39d2 XL |
1937 | |
1938 | impl<'de, T> Deserialize<'de> for Cell<T> | |
1939 | where | |
1940 | T: Deserialize<'de> + Copy, | |
1941 | { | |
8faf50e0 | 1942 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1943 | where |
1944 | D: Deserializer<'de>, | |
1945 | { | |
1946 | T::deserialize(deserializer).map(Cell::new) | |
1947 | } | |
1948 | } | |
1949 | ||
3b2f2976 | 1950 | forwarded_impl!((T), RefCell<T>, RefCell::new); |
041b39d2 XL |
1951 | |
1952 | #[cfg(feature = "std")] | |
3b2f2976 | 1953 | forwarded_impl!((T), Mutex<T>, Mutex::new); |
041b39d2 XL |
1954 | |
1955 | #[cfg(feature = "std")] | |
3b2f2976 | 1956 | forwarded_impl!((T), RwLock<T>, RwLock::new); |
041b39d2 XL |
1957 | |
1958 | //////////////////////////////////////////////////////////////////////////////// | |
1959 | ||
1960 | // This is a cleaned-up version of the impl generated by: | |
1961 | // | |
1962 | // #[derive(Deserialize)] | |
1963 | // #[serde(deny_unknown_fields)] | |
1964 | // struct Duration { | |
1965 | // secs: u64, | |
1966 | // nanos: u32, | |
1967 | // } | |
5099ac24 | 1968 | #[cfg(any(feature = "std", not(no_core_duration)))] |
041b39d2 XL |
1969 | impl<'de> Deserialize<'de> for Duration { |
1970 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
1971 | where | |
1972 | D: Deserializer<'de>, | |
1973 | { | |
1974 | // If this were outside of the serde crate, it would just use: | |
1975 | // | |
1976 | // #[derive(Deserialize)] | |
1977 | // #[serde(field_identifier, rename_all = "lowercase")] | |
1978 | enum Field { | |
1979 | Secs, | |
1980 | Nanos, | |
fc512014 | 1981 | } |
041b39d2 XL |
1982 | |
1983 | impl<'de> Deserialize<'de> for Field { | |
8faf50e0 | 1984 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
1985 | where |
1986 | D: Deserializer<'de>, | |
1987 | { | |
1988 | struct FieldVisitor; | |
1989 | ||
1990 | impl<'de> Visitor<'de> for FieldVisitor { | |
1991 | type Value = Field; | |
1992 | ||
1993 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
1994 | formatter.write_str("`secs` or `nanos`") | |
1995 | } | |
1996 | ||
8faf50e0 | 1997 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
041b39d2 XL |
1998 | where |
1999 | E: Error, | |
2000 | { | |
2001 | match value { | |
2002 | "secs" => Ok(Field::Secs), | |
2003 | "nanos" => Ok(Field::Nanos), | |
2004 | _ => Err(Error::unknown_field(value, FIELDS)), | |
2005 | } | |
2006 | } | |
2007 | ||
8faf50e0 | 2008 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
041b39d2 XL |
2009 | where |
2010 | E: Error, | |
2011 | { | |
2012 | match value { | |
2013 | b"secs" => Ok(Field::Secs), | |
2014 | b"nanos" => Ok(Field::Nanos), | |
2015 | _ => { | |
5869c6ff | 2016 | let value = ::__private::from_utf8_lossy(value); |
064997fb | 2017 | Err(Error::unknown_field(&*value, FIELDS)) |
041b39d2 XL |
2018 | } |
2019 | } | |
2020 | } | |
2021 | } | |
2022 | ||
2023 | deserializer.deserialize_identifier(FieldVisitor) | |
2024 | } | |
2025 | } | |
2026 | ||
5869c6ff XL |
2027 | fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E> |
2028 | where | |
2029 | E: Error, | |
2030 | { | |
2031 | static NANOS_PER_SEC: u32 = 1_000_000_000; | |
2032 | match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { | |
2033 | Some(_) => Ok(()), | |
2034 | None => Err(E::custom("overflow deserializing Duration")), | |
2035 | } | |
2036 | } | |
2037 | ||
041b39d2 XL |
2038 | struct DurationVisitor; |
2039 | ||
2040 | impl<'de> Visitor<'de> for DurationVisitor { | |
2041 | type Value = Duration; | |
2042 | ||
2043 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2044 | formatter.write_str("struct Duration") | |
2045 | } | |
2046 | ||
8faf50e0 | 2047 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
2048 | where |
2049 | A: SeqAccess<'de>, | |
2050 | { | |
2051 | let secs: u64 = match try!(seq.next_element()) { | |
2052 | Some(value) => value, | |
2053 | None => { | |
2054 | return Err(Error::invalid_length(0, &self)); | |
2055 | } | |
2056 | }; | |
2057 | let nanos: u32 = match try!(seq.next_element()) { | |
2058 | Some(value) => value, | |
2059 | None => { | |
2060 | return Err(Error::invalid_length(1, &self)); | |
2061 | } | |
2062 | }; | |
5869c6ff | 2063 | try!(check_overflow(secs, nanos)); |
041b39d2 XL |
2064 | Ok(Duration::new(secs, nanos)) |
2065 | } | |
2066 | ||
8faf50e0 | 2067 | fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
2068 | where |
2069 | A: MapAccess<'de>, | |
2070 | { | |
2071 | let mut secs: Option<u64> = None; | |
2072 | let mut nanos: Option<u32> = None; | |
2073 | while let Some(key) = try!(map.next_key()) { | |
2074 | match key { | |
2075 | Field::Secs => { | |
2076 | if secs.is_some() { | |
2077 | return Err(<A::Error as Error>::duplicate_field("secs")); | |
2078 | } | |
2079 | secs = Some(try!(map.next_value())); | |
2080 | } | |
2081 | Field::Nanos => { | |
2082 | if nanos.is_some() { | |
2083 | return Err(<A::Error as Error>::duplicate_field("nanos")); | |
2084 | } | |
2085 | nanos = Some(try!(map.next_value())); | |
2086 | } | |
2087 | } | |
2088 | } | |
2089 | let secs = match secs { | |
2090 | Some(secs) => secs, | |
2091 | None => return Err(<A::Error as Error>::missing_field("secs")), | |
2092 | }; | |
2093 | let nanos = match nanos { | |
2094 | Some(nanos) => nanos, | |
2095 | None => return Err(<A::Error as Error>::missing_field("nanos")), | |
2096 | }; | |
5869c6ff | 2097 | try!(check_overflow(secs, nanos)); |
041b39d2 XL |
2098 | Ok(Duration::new(secs, nanos)) |
2099 | } | |
2100 | } | |
2101 | ||
2102 | const FIELDS: &'static [&'static str] = &["secs", "nanos"]; | |
2103 | deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor) | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | //////////////////////////////////////////////////////////////////////////////// | |
2108 | ||
2109 | #[cfg(feature = "std")] | |
2110 | impl<'de> Deserialize<'de> for SystemTime { | |
2111 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2112 | where | |
2113 | D: Deserializer<'de>, | |
2114 | { | |
2115 | // Reuse duration | |
2116 | enum Field { | |
2117 | Secs, | |
2118 | Nanos, | |
fc512014 | 2119 | } |
041b39d2 XL |
2120 | |
2121 | impl<'de> Deserialize<'de> for Field { | |
8faf50e0 | 2122 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
2123 | where |
2124 | D: Deserializer<'de>, | |
2125 | { | |
2126 | struct FieldVisitor; | |
2127 | ||
2128 | impl<'de> Visitor<'de> for FieldVisitor { | |
2129 | type Value = Field; | |
2130 | ||
2131 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2132 | formatter.write_str("`secs_since_epoch` or `nanos_since_epoch`") | |
2133 | } | |
2134 | ||
8faf50e0 | 2135 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
041b39d2 XL |
2136 | where |
2137 | E: Error, | |
2138 | { | |
2139 | match value { | |
2140 | "secs_since_epoch" => Ok(Field::Secs), | |
2141 | "nanos_since_epoch" => Ok(Field::Nanos), | |
2142 | _ => Err(Error::unknown_field(value, FIELDS)), | |
2143 | } | |
2144 | } | |
2145 | ||
8faf50e0 | 2146 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
041b39d2 XL |
2147 | where |
2148 | E: Error, | |
2149 | { | |
2150 | match value { | |
2151 | b"secs_since_epoch" => Ok(Field::Secs), | |
2152 | b"nanos_since_epoch" => Ok(Field::Nanos), | |
2153 | _ => { | |
2154 | let value = String::from_utf8_lossy(value); | |
2155 | Err(Error::unknown_field(&value, FIELDS)) | |
2156 | } | |
2157 | } | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | deserializer.deserialize_identifier(FieldVisitor) | |
2162 | } | |
2163 | } | |
2164 | ||
6a06907d XL |
2165 | fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E> |
2166 | where | |
2167 | E: Error, | |
2168 | { | |
2169 | static NANOS_PER_SEC: u32 = 1_000_000_000; | |
2170 | match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { | |
2171 | Some(_) => Ok(()), | |
2172 | None => Err(E::custom("overflow deserializing SystemTime epoch offset")), | |
2173 | } | |
2174 | } | |
2175 | ||
041b39d2 XL |
2176 | struct DurationVisitor; |
2177 | ||
2178 | impl<'de> Visitor<'de> for DurationVisitor { | |
2179 | type Value = Duration; | |
2180 | ||
2181 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2182 | formatter.write_str("struct SystemTime") | |
2183 | } | |
2184 | ||
8faf50e0 | 2185 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
2186 | where |
2187 | A: SeqAccess<'de>, | |
2188 | { | |
2189 | let secs: u64 = match try!(seq.next_element()) { | |
2190 | Some(value) => value, | |
2191 | None => { | |
2192 | return Err(Error::invalid_length(0, &self)); | |
2193 | } | |
2194 | }; | |
2195 | let nanos: u32 = match try!(seq.next_element()) { | |
2196 | Some(value) => value, | |
2197 | None => { | |
2198 | return Err(Error::invalid_length(1, &self)); | |
2199 | } | |
2200 | }; | |
6a06907d | 2201 | try!(check_overflow(secs, nanos)); |
041b39d2 XL |
2202 | Ok(Duration::new(secs, nanos)) |
2203 | } | |
2204 | ||
8faf50e0 | 2205 | fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
2206 | where |
2207 | A: MapAccess<'de>, | |
2208 | { | |
2209 | let mut secs: Option<u64> = None; | |
2210 | let mut nanos: Option<u32> = None; | |
2211 | while let Some(key) = try!(map.next_key()) { | |
2212 | match key { | |
2213 | Field::Secs => { | |
2214 | if secs.is_some() { | |
ff7c6d11 XL |
2215 | return Err(<A::Error as Error>::duplicate_field( |
2216 | "secs_since_epoch", | |
2217 | )); | |
041b39d2 XL |
2218 | } |
2219 | secs = Some(try!(map.next_value())); | |
2220 | } | |
2221 | Field::Nanos => { | |
2222 | if nanos.is_some() { | |
ff7c6d11 XL |
2223 | return Err(<A::Error as Error>::duplicate_field( |
2224 | "nanos_since_epoch", | |
2225 | )); | |
041b39d2 XL |
2226 | } |
2227 | nanos = Some(try!(map.next_value())); | |
2228 | } | |
2229 | } | |
2230 | } | |
2231 | let secs = match secs { | |
2232 | Some(secs) => secs, | |
2233 | None => return Err(<A::Error as Error>::missing_field("secs_since_epoch")), | |
2234 | }; | |
2235 | let nanos = match nanos { | |
2236 | Some(nanos) => nanos, | |
2237 | None => return Err(<A::Error as Error>::missing_field("nanos_since_epoch")), | |
2238 | }; | |
6a06907d | 2239 | try!(check_overflow(secs, nanos)); |
041b39d2 XL |
2240 | Ok(Duration::new(secs, nanos)) |
2241 | } | |
2242 | } | |
2243 | ||
2244 | const FIELDS: &'static [&'static str] = &["secs_since_epoch", "nanos_since_epoch"]; | |
2245 | let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor)); | |
5099ac24 | 2246 | #[cfg(not(no_systemtime_checked_add))] |
6a06907d XL |
2247 | let ret = UNIX_EPOCH |
2248 | .checked_add(duration) | |
2249 | .ok_or_else(|| D::Error::custom("overflow deserializing SystemTime")); | |
5099ac24 | 2250 | #[cfg(no_systemtime_checked_add)] |
6a06907d XL |
2251 | let ret = Ok(UNIX_EPOCH + duration); |
2252 | ret | |
041b39d2 XL |
2253 | } |
2254 | } | |
2255 | ||
2256 | //////////////////////////////////////////////////////////////////////////////// | |
2257 | ||
2258 | // Similar to: | |
2259 | // | |
2260 | // #[derive(Deserialize)] | |
2261 | // #[serde(deny_unknown_fields)] | |
2262 | // struct Range { | |
2263 | // start: u64, | |
2264 | // end: u32, | |
2265 | // } | |
b7449926 | 2266 | impl<'de, Idx> Deserialize<'de> for Range<Idx> |
041b39d2 XL |
2267 | where |
2268 | Idx: Deserialize<'de>, | |
2269 | { | |
2270 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2271 | where | |
2272 | D: Deserializer<'de>, | |
2273 | { | |
2b03887a | 2274 | let (start, end) = try!(deserializer.deserialize_struct( |
b7449926 XL |
2275 | "Range", |
2276 | range::FIELDS, | |
2277 | range::RangeVisitor { | |
2278 | expecting: "struct Range", | |
2279 | phantom: PhantomData, | |
2280 | }, | |
2b03887a | 2281 | )); |
b7449926 XL |
2282 | Ok(start..end) |
2283 | } | |
2284 | } | |
041b39d2 | 2285 | |
5099ac24 | 2286 | #[cfg(not(no_range_inclusive))] |
b7449926 XL |
2287 | impl<'de, Idx> Deserialize<'de> for RangeInclusive<Idx> |
2288 | where | |
2289 | Idx: Deserialize<'de>, | |
2290 | { | |
2291 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2292 | where | |
2293 | D: Deserializer<'de>, | |
2294 | { | |
2b03887a | 2295 | let (start, end) = try!(deserializer.deserialize_struct( |
b7449926 XL |
2296 | "RangeInclusive", |
2297 | range::FIELDS, | |
2298 | range::RangeVisitor { | |
2299 | expecting: "struct RangeInclusive", | |
2300 | phantom: PhantomData, | |
2301 | }, | |
2b03887a | 2302 | )); |
b7449926 XL |
2303 | Ok(RangeInclusive::new(start, end)) |
2304 | } | |
2305 | } | |
041b39d2 | 2306 | |
b7449926 XL |
2307 | mod range { |
2308 | use lib::*; | |
041b39d2 | 2309 | |
b7449926 | 2310 | use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; |
041b39d2 | 2311 | |
b7449926 XL |
2312 | pub const FIELDS: &'static [&'static str] = &["start", "end"]; |
2313 | ||
2314 | // If this were outside of the serde crate, it would just use: | |
2315 | // | |
2316 | // #[derive(Deserialize)] | |
2317 | // #[serde(field_identifier, rename_all = "lowercase")] | |
2318 | enum Field { | |
2319 | Start, | |
2320 | End, | |
2321 | } | |
2322 | ||
2323 | impl<'de> Deserialize<'de> for Field { | |
2324 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2325 | where | |
2326 | D: Deserializer<'de>, | |
2327 | { | |
2328 | struct FieldVisitor; | |
2329 | ||
2330 | impl<'de> Visitor<'de> for FieldVisitor { | |
2331 | type Value = Field; | |
2332 | ||
2333 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2334 | formatter.write_str("`start` or `end`") | |
2335 | } | |
2336 | ||
2337 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | |
2338 | where | |
2339 | E: Error, | |
2340 | { | |
2341 | match value { | |
2342 | "start" => Ok(Field::Start), | |
2343 | "end" => Ok(Field::End), | |
2344 | _ => Err(Error::unknown_field(value, FIELDS)), | |
041b39d2 | 2345 | } |
b7449926 | 2346 | } |
041b39d2 | 2347 | |
b7449926 XL |
2348 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
2349 | where | |
2350 | E: Error, | |
2351 | { | |
2352 | match value { | |
2353 | b"start" => Ok(Field::Start), | |
2354 | b"end" => Ok(Field::End), | |
2355 | _ => { | |
5869c6ff | 2356 | let value = ::__private::from_utf8_lossy(value); |
064997fb | 2357 | Err(Error::unknown_field(&*value, FIELDS)) |
041b39d2 XL |
2358 | } |
2359 | } | |
2360 | } | |
041b39d2 | 2361 | } |
b7449926 XL |
2362 | |
2363 | deserializer.deserialize_identifier(FieldVisitor) | |
041b39d2 | 2364 | } |
b7449926 | 2365 | } |
041b39d2 | 2366 | |
b7449926 XL |
2367 | pub struct RangeVisitor<Idx> { |
2368 | pub expecting: &'static str, | |
2369 | pub phantom: PhantomData<Idx>, | |
2370 | } | |
2371 | ||
2372 | impl<'de, Idx> Visitor<'de> for RangeVisitor<Idx> | |
2373 | where | |
2374 | Idx: Deserialize<'de>, | |
2375 | { | |
2376 | type Value = (Idx, Idx); | |
2377 | ||
2378 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2379 | formatter.write_str(self.expecting) | |
041b39d2 XL |
2380 | } |
2381 | ||
b7449926 | 2382 | fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
041b39d2 | 2383 | where |
b7449926 | 2384 | A: SeqAccess<'de>, |
041b39d2 | 2385 | { |
b7449926 XL |
2386 | let start: Idx = match try!(seq.next_element()) { |
2387 | Some(value) => value, | |
2388 | None => { | |
2389 | return Err(Error::invalid_length(0, &self)); | |
2390 | } | |
2391 | }; | |
2392 | let end: Idx = match try!(seq.next_element()) { | |
2393 | Some(value) => value, | |
2394 | None => { | |
2395 | return Err(Error::invalid_length(1, &self)); | |
2396 | } | |
2397 | }; | |
2398 | Ok((start, end)) | |
2399 | } | |
041b39d2 | 2400 | |
b7449926 XL |
2401 | fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
2402 | where | |
2403 | A: MapAccess<'de>, | |
2404 | { | |
2405 | let mut start: Option<Idx> = None; | |
2406 | let mut end: Option<Idx> = None; | |
2407 | while let Some(key) = try!(map.next_key()) { | |
2408 | match key { | |
2409 | Field::Start => { | |
2410 | if start.is_some() { | |
2411 | return Err(<A::Error as Error>::duplicate_field("start")); | |
041b39d2 | 2412 | } |
b7449926 XL |
2413 | start = Some(try!(map.next_value())); |
2414 | } | |
2415 | Field::End => { | |
2416 | if end.is_some() { | |
2417 | return Err(<A::Error as Error>::duplicate_field("end")); | |
041b39d2 | 2418 | } |
b7449926 | 2419 | end = Some(try!(map.next_value())); |
041b39d2 XL |
2420 | } |
2421 | } | |
041b39d2 | 2422 | } |
b7449926 XL |
2423 | let start = match start { |
2424 | Some(start) => start, | |
2425 | None => return Err(<A::Error as Error>::missing_field("start")), | |
2426 | }; | |
2427 | let end = match end { | |
2428 | Some(end) => end, | |
2429 | None => return Err(<A::Error as Error>::missing_field("end")), | |
2430 | }; | |
2431 | Ok((start, end)) | |
041b39d2 | 2432 | } |
041b39d2 XL |
2433 | } |
2434 | } | |
2435 | ||
2436 | //////////////////////////////////////////////////////////////////////////////// | |
2437 | ||
5099ac24 | 2438 | #[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))] |
dc9dc135 XL |
2439 | impl<'de, T> Deserialize<'de> for Bound<T> |
2440 | where | |
2441 | T: Deserialize<'de>, | |
2442 | { | |
2443 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2444 | where | |
2445 | D: Deserializer<'de>, | |
2446 | { | |
2447 | enum Field { | |
2448 | Unbounded, | |
2449 | Included, | |
2450 | Excluded, | |
2451 | } | |
2452 | ||
2453 | impl<'de> Deserialize<'de> for Field { | |
2454 | #[inline] | |
2455 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2456 | where | |
2457 | D: Deserializer<'de>, | |
2458 | { | |
2459 | struct FieldVisitor; | |
2460 | ||
2461 | impl<'de> Visitor<'de> for FieldVisitor { | |
2462 | type Value = Field; | |
2463 | ||
2464 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2465 | formatter.write_str("`Unbounded`, `Included` or `Excluded`") | |
2466 | } | |
2467 | ||
1b1a35ee | 2468 | fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
dc9dc135 XL |
2469 | where |
2470 | E: Error, | |
2471 | { | |
2472 | match value { | |
2473 | 0 => Ok(Field::Unbounded), | |
2474 | 1 => Ok(Field::Included), | |
2475 | 2 => Ok(Field::Excluded), | |
fc512014 | 2476 | _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), |
dc9dc135 XL |
2477 | } |
2478 | } | |
2479 | ||
2480 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | |
2481 | where | |
2482 | E: Error, | |
2483 | { | |
2484 | match value { | |
2485 | "Unbounded" => Ok(Field::Unbounded), | |
2486 | "Included" => Ok(Field::Included), | |
2487 | "Excluded" => Ok(Field::Excluded), | |
2488 | _ => Err(Error::unknown_variant(value, VARIANTS)), | |
2489 | } | |
2490 | } | |
2491 | ||
2492 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> | |
2493 | where | |
2494 | E: Error, | |
2495 | { | |
2496 | match value { | |
2497 | b"Unbounded" => Ok(Field::Unbounded), | |
2498 | b"Included" => Ok(Field::Included), | |
2499 | b"Excluded" => Ok(Field::Excluded), | |
2500 | _ => match str::from_utf8(value) { | |
2501 | Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), | |
2502 | Err(_) => { | |
2503 | Err(Error::invalid_value(Unexpected::Bytes(value), &self)) | |
2504 | } | |
2505 | }, | |
2506 | } | |
2507 | } | |
2508 | } | |
2509 | ||
2510 | deserializer.deserialize_identifier(FieldVisitor) | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | struct BoundVisitor<T>(PhantomData<Bound<T>>); | |
2515 | ||
2516 | impl<'de, T> Visitor<'de> for BoundVisitor<T> | |
2517 | where | |
2518 | T: Deserialize<'de>, | |
2519 | { | |
2520 | type Value = Bound<T>; | |
2521 | ||
2522 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2523 | formatter.write_str("enum Bound") | |
2524 | } | |
2525 | ||
2526 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> | |
2527 | where | |
2528 | A: EnumAccess<'de>, | |
2529 | { | |
2530 | match try!(data.variant()) { | |
2531 | (Field::Unbounded, v) => v.unit_variant().map(|()| Bound::Unbounded), | |
2532 | (Field::Included, v) => v.newtype_variant().map(Bound::Included), | |
2533 | (Field::Excluded, v) => v.newtype_variant().map(Bound::Excluded), | |
2534 | } | |
2535 | } | |
2536 | } | |
2537 | ||
2538 | const VARIANTS: &'static [&'static str] = &["Unbounded", "Included", "Excluded"]; | |
2539 | ||
2540 | deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData)) | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | //////////////////////////////////////////////////////////////////////////////// | |
2545 | ||
041b39d2 XL |
2546 | impl<'de, T, E> Deserialize<'de> for Result<T, E> |
2547 | where | |
2548 | T: Deserialize<'de>, | |
2549 | E: Deserialize<'de>, | |
2550 | { | |
8faf50e0 | 2551 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
2552 | where |
2553 | D: Deserializer<'de>, | |
2554 | { | |
2555 | // If this were outside of the serde crate, it would just use: | |
2556 | // | |
2557 | // #[derive(Deserialize)] | |
2558 | // #[serde(variant_identifier)] | |
2559 | enum Field { | |
2560 | Ok, | |
2561 | Err, | |
2562 | } | |
2563 | ||
2564 | impl<'de> Deserialize<'de> for Field { | |
2565 | #[inline] | |
8faf50e0 | 2566 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
041b39d2 XL |
2567 | where |
2568 | D: Deserializer<'de>, | |
2569 | { | |
2570 | struct FieldVisitor; | |
2571 | ||
2572 | impl<'de> Visitor<'de> for FieldVisitor { | |
2573 | type Value = Field; | |
2574 | ||
2575 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2576 | formatter.write_str("`Ok` or `Err`") | |
2577 | } | |
2578 | ||
1b1a35ee | 2579 | fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> |
041b39d2 XL |
2580 | where |
2581 | E: Error, | |
2582 | { | |
2583 | match value { | |
2584 | 0 => Ok(Field::Ok), | |
2585 | 1 => Ok(Field::Err), | |
fc512014 | 2586 | _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), |
041b39d2 XL |
2587 | } |
2588 | } | |
2589 | ||
8faf50e0 | 2590 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
041b39d2 XL |
2591 | where |
2592 | E: Error, | |
2593 | { | |
2594 | match value { | |
2595 | "Ok" => Ok(Field::Ok), | |
2596 | "Err" => Ok(Field::Err), | |
2597 | _ => Err(Error::unknown_variant(value, VARIANTS)), | |
2598 | } | |
2599 | } | |
2600 | ||
8faf50e0 | 2601 | fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
041b39d2 XL |
2602 | where |
2603 | E: Error, | |
2604 | { | |
2605 | match value { | |
2606 | b"Ok" => Ok(Field::Ok), | |
2607 | b"Err" => Ok(Field::Err), | |
ff7c6d11 XL |
2608 | _ => match str::from_utf8(value) { |
2609 | Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), | |
2610 | Err(_) => { | |
2611 | Err(Error::invalid_value(Unexpected::Bytes(value), &self)) | |
041b39d2 | 2612 | } |
ff7c6d11 | 2613 | }, |
041b39d2 XL |
2614 | } |
2615 | } | |
2616 | } | |
2617 | ||
2618 | deserializer.deserialize_identifier(FieldVisitor) | |
2619 | } | |
2620 | } | |
2621 | ||
2622 | struct ResultVisitor<T, E>(PhantomData<Result<T, E>>); | |
2623 | ||
2624 | impl<'de, T, E> Visitor<'de> for ResultVisitor<T, E> | |
2625 | where | |
2626 | T: Deserialize<'de>, | |
2627 | E: Deserialize<'de>, | |
2628 | { | |
2629 | type Value = Result<T, E>; | |
2630 | ||
2631 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2632 | formatter.write_str("enum Result") | |
2633 | } | |
2634 | ||
8faf50e0 | 2635 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
041b39d2 XL |
2636 | where |
2637 | A: EnumAccess<'de>, | |
2638 | { | |
2639 | match try!(data.variant()) { | |
2640 | (Field::Ok, v) => v.newtype_variant().map(Ok), | |
2641 | (Field::Err, v) => v.newtype_variant().map(Err), | |
2642 | } | |
2643 | } | |
2644 | } | |
2645 | ||
2646 | const VARIANTS: &'static [&'static str] = &["Ok", "Err"]; | |
2647 | ||
2648 | deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData)) | |
2649 | } | |
2650 | } | |
abe05a73 XL |
2651 | |
2652 | //////////////////////////////////////////////////////////////////////////////// | |
2653 | ||
abe05a73 XL |
2654 | impl<'de, T> Deserialize<'de> for Wrapping<T> |
2655 | where | |
ff7c6d11 | 2656 | T: Deserialize<'de>, |
abe05a73 | 2657 | { |
8faf50e0 | 2658 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
abe05a73 XL |
2659 | where |
2660 | D: Deserializer<'de>, | |
2661 | { | |
2662 | Deserialize::deserialize(deserializer).map(Wrapping) | |
2663 | } | |
2664 | } | |
e1599b0c | 2665 | |
5099ac24 | 2666 | #[cfg(all(feature = "std", not(no_std_atomic)))] |
e1599b0c | 2667 | macro_rules! atomic_impl { |
9ffffee4 | 2668 | ($($ty:ident $size:expr)*) => { |
e1599b0c | 2669 | $( |
9ffffee4 | 2670 | #[cfg(any(no_target_has_atomic, target_has_atomic = $size))] |
e1599b0c XL |
2671 | impl<'de> Deserialize<'de> for $ty { |
2672 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
2673 | where | |
2674 | D: Deserializer<'de>, | |
2675 | { | |
2676 | Deserialize::deserialize(deserializer).map(Self::new) | |
2677 | } | |
2678 | } | |
2679 | )* | |
2680 | }; | |
2681 | } | |
2682 | ||
5099ac24 | 2683 | #[cfg(all(feature = "std", not(no_std_atomic)))] |
e1599b0c | 2684 | atomic_impl! { |
9ffffee4 FG |
2685 | AtomicBool "8" |
2686 | AtomicI8 "8" | |
2687 | AtomicI16 "16" | |
2688 | AtomicI32 "32" | |
2689 | AtomicIsize "ptr" | |
2690 | AtomicU8 "8" | |
2691 | AtomicU16 "16" | |
2692 | AtomicU32 "32" | |
2693 | AtomicUsize "ptr" | |
e1599b0c XL |
2694 | } |
2695 | ||
5099ac24 | 2696 | #[cfg(all(feature = "std", not(no_std_atomic64)))] |
e1599b0c | 2697 | atomic_impl! { |
9ffffee4 FG |
2698 | AtomicI64 "64" |
2699 | AtomicU64 "64" | |
e1599b0c | 2700 | } |
5869c6ff XL |
2701 | |
2702 | #[cfg(feature = "std")] | |
2703 | struct FromStrVisitor<T> { | |
2704 | expecting: &'static str, | |
2705 | ty: PhantomData<T>, | |
2706 | } | |
2707 | ||
2708 | #[cfg(feature = "std")] | |
2709 | impl<T> FromStrVisitor<T> { | |
2710 | fn new(expecting: &'static str) -> Self { | |
2711 | FromStrVisitor { | |
2712 | expecting: expecting, | |
2713 | ty: PhantomData, | |
2714 | } | |
2715 | } | |
2716 | } | |
2717 | ||
2718 | #[cfg(feature = "std")] | |
2719 | impl<'de, T> Visitor<'de> for FromStrVisitor<T> | |
2720 | where | |
2721 | T: str::FromStr, | |
2722 | T::Err: fmt::Display, | |
2723 | { | |
2724 | type Value = T; | |
2725 | ||
2726 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | |
2727 | formatter.write_str(self.expecting) | |
2728 | } | |
2729 | ||
2730 | fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> | |
2731 | where | |
2732 | E: Error, | |
2733 | { | |
2734 | s.parse().map_err(Error::custom) | |
2735 | } | |
2736 | } |