1 use lib::*;
2 
3 use ser::{Error, Serialize, SerializeTuple, Serializer};
4 
5 ////////////////////////////////////////////////////////////////////////////////
6 
7 macro_rules! primitive_impl {
8     ($ty:ident, $method:ident $($cast:tt)*) => {
9         impl Serialize for $ty {
10             #[inline]
11             fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
12             where
13                 S: Serializer,
14             {
15                 serializer.$method(*self $($cast)*)
16             }
17         }
18     }
19 }
20 
21 primitive_impl!(bool, serialize_bool);
22 primitive_impl!(isize, serialize_i64 as i64);
23 primitive_impl!(i8, serialize_i8);
24 primitive_impl!(i16, serialize_i16);
25 primitive_impl!(i32, serialize_i32);
26 primitive_impl!(i64, serialize_i64);
27 primitive_impl!(usize, serialize_u64 as u64);
28 primitive_impl!(u8, serialize_u8);
29 primitive_impl!(u16, serialize_u16);
30 primitive_impl!(u32, serialize_u32);
31 primitive_impl!(u64, serialize_u64);
32 primitive_impl!(f32, serialize_f32);
33 primitive_impl!(f64, serialize_f64);
34 primitive_impl!(char, serialize_char);
35 
36 serde_if_integer128! {
37     primitive_impl!(i128, serialize_i128);
38     primitive_impl!(u128, serialize_u128);
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 
43 impl Serialize for str {
44     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,45     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
46     where
47         S: Serializer,
48     {
49         serializer.serialize_str(self)
50     }
51 }
52 
53 #[cfg(any(feature = "std", feature = "alloc"))]
54 impl Serialize for String {
55     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,56     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57     where
58         S: Serializer,
59     {
60         serializer.serialize_str(self)
61     }
62 }
63 
64 impl<'a> Serialize for fmt::Arguments<'a> {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,65     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
66     where
67         S: Serializer,
68     {
69         serializer.collect_str(self)
70     }
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 
75 #[cfg(feature = "std")]
76 impl Serialize for CStr {
77     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,78     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
79     where
80         S: Serializer,
81     {
82         serializer.serialize_bytes(self.to_bytes())
83     }
84 }
85 
86 #[cfg(feature = "std")]
87 impl Serialize for CString {
88     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,89     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
90     where
91         S: Serializer,
92     {
93         serializer.serialize_bytes(self.to_bytes())
94     }
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 
99 impl<T> Serialize for Option<T>
100 where
101     T: Serialize,
102 {
103     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,104     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105     where
106         S: Serializer,
107     {
108         match *self {
109             Some(ref value) => serializer.serialize_some(value),
110             None => serializer.serialize_none(),
111         }
112     }
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 
117 impl<T: ?Sized> Serialize for PhantomData<T> {
118     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,119     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
120     where
121         S: Serializer,
122     {
123         serializer.serialize_unit_struct("PhantomData")
124     }
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 
129 // Does not require T: Serialize.
130 impl<T> Serialize for [T; 0] {
131     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,132     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
133     where
134         S: Serializer,
135     {
136         try!(serializer.serialize_tuple(0)).end()
137     }
138 }
139 
140 macro_rules! array_impls {
141     ($($len:tt)+) => {
142         $(
143             impl<T> Serialize for [T; $len]
144             where
145                 T: Serialize,
146             {
147                 #[inline]
148                 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
149                 where
150                     S: Serializer,
151                 {
152                     let mut seq = try!(serializer.serialize_tuple($len));
153                     for e in self {
154                         try!(seq.serialize_element(e));
155                     }
156                     seq.end()
157                 }
158             }
159         )+
160     }
161 }
162 
163 array_impls! {
164     01 02 03 04 05 06 07 08 09 10
165     11 12 13 14 15 16 17 18 19 20
166     21 22 23 24 25 26 27 28 29 30
167     31 32
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 
172 impl<T> Serialize for [T]
173 where
174     T: Serialize,
175 {
176     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,177     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
178     where
179         S: Serializer,
180     {
181         serializer.collect_seq(self)
182     }
183 }
184 
185 #[cfg(any(feature = "std", feature = "alloc"))]
186 macro_rules! seq_impl {
187     ($ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)* >) => {
188         impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
189         where
190             T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
191             $($typaram: $bound,)*
192         {
193             #[inline]
194             fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
195             where
196                 S: Serializer,
197             {
198                 serializer.collect_seq(self)
199             }
200         }
201     }
202 }
203 
204 #[cfg(any(feature = "std", feature = "alloc"))]
205 seq_impl!(BinaryHeap<T: Ord>);
206 
207 #[cfg(any(feature = "std", feature = "alloc"))]
208 seq_impl!(BTreeSet<T: Ord>);
209 
210 #[cfg(feature = "std")]
211 seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
212 
213 #[cfg(any(feature = "std", feature = "alloc"))]
214 seq_impl!(LinkedList<T>);
215 
216 #[cfg(any(feature = "std", feature = "alloc"))]
217 seq_impl!(Vec<T>);
218 
219 #[cfg(any(feature = "std", feature = "alloc"))]
220 seq_impl!(VecDeque<T>);
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 
224 impl<Idx> Serialize for Range<Idx>
225 where
226     Idx: Serialize,
227 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,228     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
229     where
230         S: Serializer,
231     {
232         use super::SerializeStruct;
233         let mut state = try!(serializer.serialize_struct("Range", 2));
234         try!(state.serialize_field("start", &self.start));
235         try!(state.serialize_field("end", &self.end));
236         state.end()
237     }
238 }
239 
240 ////////////////////////////////////////////////////////////////////////////////
241 
242 #[cfg(range_inclusive)]
243 impl<Idx> Serialize for RangeInclusive<Idx>
244 where
245     Idx: Serialize,
246 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,247     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
248     where
249         S: Serializer,
250     {
251         use super::SerializeStruct;
252         let mut state = try!(serializer.serialize_struct("RangeInclusive", 2));
253         try!(state.serialize_field("start", &self.start()));
254         try!(state.serialize_field("end", &self.end()));
255         state.end()
256     }
257 }
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 
261 #[cfg(any(ops_bound, collections_bound))]
262 impl<T> Serialize for Bound<T>
263 where
264     T: Serialize,
265 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,266     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
267     where
268         S: Serializer,
269     {
270         match *self {
271             Bound::Unbounded => serializer.serialize_unit_variant("Bound", 0, "Unbounded"),
272             Bound::Included(ref value) => {
273                 serializer.serialize_newtype_variant("Bound", 1, "Included", value)
274             }
275             Bound::Excluded(ref value) => {
276                 serializer.serialize_newtype_variant("Bound", 2, "Excluded", value)
277             }
278         }
279     }
280 }
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 
284 impl Serialize for () {
285     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,286     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
287     where
288         S: Serializer,
289     {
290         serializer.serialize_unit()
291     }
292 }
293 
294 #[cfg(feature = "unstable")]
295 impl Serialize for ! {
serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,296     fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
297     where
298         S: Serializer,
299     {
300         *self
301     }
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 
306 macro_rules! tuple_impls {
307     ($($len:expr => ($($n:tt $name:ident)+))+) => {
308         $(
309             impl<$($name),+> Serialize for ($($name,)+)
310             where
311                 $($name: Serialize,)+
312             {
313                 #[inline]
314                 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
315                 where
316                     S: Serializer,
317                 {
318                     let mut tuple = try!(serializer.serialize_tuple($len));
319                     $(
320                         try!(tuple.serialize_element(&self.$n));
321                     )+
322                     tuple.end()
323                 }
324             }
325         )+
326     }
327 }
328 
329 tuple_impls! {
330     1 => (0 T0)
331     2 => (0 T0 1 T1)
332     3 => (0 T0 1 T1 2 T2)
333     4 => (0 T0 1 T1 2 T2 3 T3)
334     5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
335     6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
336     7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
337     8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
338     9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
339     10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
340     11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
341     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)
342     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)
343     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)
344     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)
345     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)
346 }
347 
348 ////////////////////////////////////////////////////////////////////////////////
349 
350 #[cfg(any(feature = "std", feature = "alloc"))]
351 macro_rules! map_impl {
352     ($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)* >) => {
353         impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
354         where
355             K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
356             V: Serialize,
357             $($typaram: $bound,)*
358         {
359             #[inline]
360             fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
361             where
362                 S: Serializer,
363             {
364                 serializer.collect_map(self)
365             }
366         }
367     }
368 }
369 
370 #[cfg(any(feature = "std", feature = "alloc"))]
371 map_impl!(BTreeMap<K: Ord, V>);
372 
373 #[cfg(feature = "std")]
374 map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
375 
376 ////////////////////////////////////////////////////////////////////////////////
377 
378 macro_rules! deref_impl {
379     (
380         $(#[doc = $doc:tt])*
381         <$($desc:tt)+
382     ) => {
383         $(#[doc = $doc])*
384         impl <$($desc)+ {
385             #[inline]
386             fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
387             where
388                 S: Serializer,
389             {
390                 (**self).serialize(serializer)
391             }
392         }
393     };
394 }
395 
396 deref_impl!(<'a, T: ?Sized> Serialize for &'a T where T: Serialize);
397 deref_impl!(<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize);
398 
399 #[cfg(any(feature = "std", feature = "alloc"))]
400 deref_impl!(<T: ?Sized> Serialize for Box<T> where T: Serialize);
401 
402 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
403 deref_impl! {
404     /// This impl requires the [`"rc"`] Cargo feature of Serde.
405     ///
406     /// Serializing a data structure containing `Rc` will serialize a copy of
407     /// the contents of the `Rc` each time the `Rc` is referenced within the
408     /// data structure. Serialization will not attempt to deduplicate these
409     /// repeated data.
410     ///
411     /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
412     <T: ?Sized> Serialize for Rc<T> where T: Serialize
413 }
414 
415 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
416 deref_impl! {
417     /// This impl requires the [`"rc"`] Cargo feature of Serde.
418     ///
419     /// Serializing a data structure containing `Arc` will serialize a copy of
420     /// the contents of the `Arc` each time the `Arc` is referenced within the
421     /// data structure. Serialization will not attempt to deduplicate these
422     /// repeated data.
423     ///
424     /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
425     <T: ?Sized> Serialize for Arc<T> where T: Serialize
426 }
427 
428 #[cfg(any(feature = "std", feature = "alloc"))]
429 deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned);
430 
431 ////////////////////////////////////////////////////////////////////////////////
432 
433 /// This impl requires the [`"rc"`] Cargo feature of Serde.
434 ///
435 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
436 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
437 impl<T: ?Sized> Serialize for RcWeak<T>
438 where
439     T: Serialize,
440 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,441     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
442     where
443         S: Serializer,
444     {
445         self.upgrade().serialize(serializer)
446     }
447 }
448 
449 /// This impl requires the [`"rc"`] Cargo feature of Serde.
450 ///
451 /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
452 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
453 impl<T: ?Sized> Serialize for ArcWeak<T>
454 where
455     T: Serialize,
456 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,457     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
458     where
459         S: Serializer,
460     {
461         self.upgrade().serialize(serializer)
462     }
463 }
464 
465 ////////////////////////////////////////////////////////////////////////////////
466 
467 macro_rules! nonzero_integers {
468     ( $( $T: ident, )+ ) => {
469         $(
470             #[cfg(num_nonzero)]
471             impl Serialize for num::$T {
472                 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
473                 where
474                     S: Serializer,
475                 {
476                     self.get().serialize(serializer)
477                 }
478             }
479         )+
480     }
481 }
482 
483 nonzero_integers! {
484     // Not including signed NonZeroI* since they might be removed
485     NonZeroU8,
486     NonZeroU16,
487     NonZeroU32,
488     NonZeroU64,
489     NonZeroUsize,
490 }
491 
492 // Currently 128-bit integers do not work on Emscripten targets so we need an
493 // additional `#[cfg]`
494 serde_if_integer128! {
495     nonzero_integers! {
496         NonZeroU128,
497     }
498 }
499 
500 impl<T> Serialize for Cell<T>
501 where
502     T: Serialize + Copy,
503 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,504     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
505     where
506         S: Serializer,
507     {
508         self.get().serialize(serializer)
509     }
510 }
511 
512 impl<T> Serialize for RefCell<T>
513 where
514     T: Serialize,
515 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,516     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
517     where
518         S: Serializer,
519     {
520         match self.try_borrow() {
521             Ok(value) => value.serialize(serializer),
522             Err(_) => Err(S::Error::custom("already mutably borrowed")),
523         }
524     }
525 }
526 
527 #[cfg(feature = "std")]
528 impl<T> Serialize for Mutex<T>
529 where
530     T: Serialize,
531 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,532     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
533     where
534         S: Serializer,
535     {
536         match self.lock() {
537             Ok(locked) => locked.serialize(serializer),
538             Err(_) => Err(S::Error::custom("lock poison error while serializing")),
539         }
540     }
541 }
542 
543 #[cfg(feature = "std")]
544 impl<T> Serialize for RwLock<T>
545 where
546     T: Serialize,
547 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,548     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
549     where
550         S: Serializer,
551     {
552         match self.read() {
553             Ok(locked) => locked.serialize(serializer),
554             Err(_) => Err(S::Error::custom("lock poison error while serializing")),
555         }
556     }
557 }
558 
559 ////////////////////////////////////////////////////////////////////////////////
560 
561 impl<T, E> Serialize for Result<T, E>
562 where
563     T: Serialize,
564     E: Serialize,
565 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,566     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
567     where
568         S: Serializer,
569     {
570         match *self {
571             Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value),
572             Result::Err(ref value) => {
573                 serializer.serialize_newtype_variant("Result", 1, "Err", value)
574             }
575         }
576     }
577 }
578 
579 ////////////////////////////////////////////////////////////////////////////////
580 
581 #[cfg(any(core_duration, feature = "std"))]
582 impl Serialize for Duration {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,583     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
584     where
585         S: Serializer,
586     {
587         use super::SerializeStruct;
588         let mut state = try!(serializer.serialize_struct("Duration", 2));
589         try!(state.serialize_field("secs", &self.as_secs()));
590         try!(state.serialize_field("nanos", &self.subsec_nanos()));
591         state.end()
592     }
593 }
594 
595 ////////////////////////////////////////////////////////////////////////////////
596 
597 #[cfg(feature = "std")]
598 impl Serialize for SystemTime {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,599     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
600     where
601         S: Serializer,
602     {
603         use super::SerializeStruct;
604         let duration_since_epoch = self
605             .duration_since(UNIX_EPOCH)
606             .expect("SystemTime must be later than UNIX_EPOCH");
607         let mut state = try!(serializer.serialize_struct("SystemTime", 2));
608         try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
609         try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
610         state.end()
611     }
612 }
613 
614 ////////////////////////////////////////////////////////////////////////////////
615 
616 /// Serialize a value that implements `Display` as a string, when that string is
617 /// statically known to never have more than a constant `MAX_LEN` bytes.
618 ///
619 /// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
620 #[cfg(feature = "std")]
621 macro_rules! serialize_display_bounded_length {
622     ($value:expr, $max:expr, $serializer:expr) => {{
623         #[allow(deprecated)]
624         let mut buffer: [u8; $max] = unsafe { mem::uninitialized() };
625         let remaining_len = {
626             let mut remaining = &mut buffer[..];
627             write!(remaining, "{}", $value).unwrap();
628             remaining.len()
629         };
630         let written_len = buffer.len() - remaining_len;
631         let written = &buffer[..written_len];
632 
633         // write! only provides fmt::Formatter to Display implementations, which
634         // has methods write_str and write_char but no method to write arbitrary
635         // bytes. Therefore `written` must be valid UTF-8.
636         let written_str = unsafe { str::from_utf8_unchecked(written) };
637         $serializer.serialize_str(written_str)
638     }};
639 }
640 
641 #[cfg(feature = "std")]
642 impl Serialize for net::IpAddr {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,643     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
644     where
645         S: Serializer,
646     {
647         if serializer.is_human_readable() {
648             match *self {
649                 net::IpAddr::V4(ref a) => a.serialize(serializer),
650                 net::IpAddr::V6(ref a) => a.serialize(serializer),
651             }
652         } else {
653             match *self {
654                 net::IpAddr::V4(ref a) => {
655                     serializer.serialize_newtype_variant("IpAddr", 0, "V4", a)
656                 }
657                 net::IpAddr::V6(ref a) => {
658                     serializer.serialize_newtype_variant("IpAddr", 1, "V6", a)
659                 }
660             }
661         }
662     }
663 }
664 
665 #[cfg(feature = "std")]
666 impl Serialize for net::Ipv4Addr {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,667     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
668     where
669         S: Serializer,
670     {
671         if serializer.is_human_readable() {
672             const MAX_LEN: usize = 15;
673             debug_assert_eq!(MAX_LEN, "101.102.103.104".len());
674             serialize_display_bounded_length!(self, MAX_LEN, serializer)
675         } else {
676             self.octets().serialize(serializer)
677         }
678     }
679 }
680 
681 #[cfg(feature = "std")]
682 impl Serialize for net::Ipv6Addr {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,683     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
684     where
685         S: Serializer,
686     {
687         if serializer.is_human_readable() {
688             const MAX_LEN: usize = 39;
689             debug_assert_eq!(MAX_LEN, "1001:1002:1003:1004:1005:1006:1007:1008".len());
690             serialize_display_bounded_length!(self, MAX_LEN, serializer)
691         } else {
692             self.octets().serialize(serializer)
693         }
694     }
695 }
696 
697 #[cfg(feature = "std")]
698 impl Serialize for net::SocketAddr {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,699     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
700     where
701         S: Serializer,
702     {
703         if serializer.is_human_readable() {
704             match *self {
705                 net::SocketAddr::V4(ref addr) => addr.serialize(serializer),
706                 net::SocketAddr::V6(ref addr) => addr.serialize(serializer),
707             }
708         } else {
709             match *self {
710                 net::SocketAddr::V4(ref addr) => {
711                     serializer.serialize_newtype_variant("SocketAddr", 0, "V4", addr)
712                 }
713                 net::SocketAddr::V6(ref addr) => {
714                     serializer.serialize_newtype_variant("SocketAddr", 1, "V6", addr)
715                 }
716             }
717         }
718     }
719 }
720 
721 #[cfg(feature = "std")]
722 impl Serialize for net::SocketAddrV4 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,723     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
724     where
725         S: Serializer,
726     {
727         if serializer.is_human_readable() {
728             const MAX_LEN: usize = 21;
729             debug_assert_eq!(MAX_LEN, "101.102.103.104:65000".len());
730             serialize_display_bounded_length!(self, MAX_LEN, serializer)
731         } else {
732             (self.ip(), self.port()).serialize(serializer)
733         }
734     }
735 }
736 
737 #[cfg(feature = "std")]
738 impl Serialize for net::SocketAddrV6 {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,739     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
740     where
741         S: Serializer,
742     {
743         if serializer.is_human_readable() {
744             const MAX_LEN: usize = 47;
745             debug_assert_eq!(
746                 MAX_LEN,
747                 "[1001:1002:1003:1004:1005:1006:1007:1008]:65000".len()
748             );
749             serialize_display_bounded_length!(self, MAX_LEN, serializer)
750         } else {
751             (self.ip(), self.port()).serialize(serializer)
752         }
753     }
754 }
755 
756 ////////////////////////////////////////////////////////////////////////////////
757 
758 #[cfg(feature = "std")]
759 impl Serialize for Path {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,760     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
761     where
762         S: Serializer,
763     {
764         match self.to_str() {
765             Some(s) => s.serialize(serializer),
766             None => Err(Error::custom("path contains invalid UTF-8 characters")),
767         }
768     }
769 }
770 
771 #[cfg(feature = "std")]
772 impl Serialize for PathBuf {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,773     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
774     where
775         S: Serializer,
776     {
777         self.as_path().serialize(serializer)
778     }
779 }
780 
781 #[cfg(all(feature = "std", any(unix, windows)))]
782 impl Serialize for OsStr {
783     #[cfg(unix)]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,784     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
785     where
786         S: Serializer,
787     {
788         use std::os::unix::ffi::OsStrExt;
789         serializer.serialize_newtype_variant("OsString", 0, "Unix", self.as_bytes())
790     }
791 
792     #[cfg(windows)]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,793     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
794     where
795         S: Serializer,
796     {
797         use std::os::windows::ffi::OsStrExt;
798         let val = self.encode_wide().collect::<Vec<_>>();
799         serializer.serialize_newtype_variant("OsString", 1, "Windows", &val)
800     }
801 }
802 
803 #[cfg(all(feature = "std", any(unix, windows)))]
804 impl Serialize for OsString {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,805     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
806     where
807         S: Serializer,
808     {
809         self.as_os_str().serialize(serializer)
810     }
811 }
812 
813 ////////////////////////////////////////////////////////////////////////////////
814 
815 #[cfg(feature = "std")]
816 impl<T> Serialize for Wrapping<T>
817 where
818     T: Serialize,
819 {
820     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,821     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
822     where
823         S: Serializer,
824     {
825         self.0.serialize(serializer)
826     }
827 }
828 
829 #[cfg(core_reverse)]
830 impl<T> Serialize for Reverse<T>
831 where
832     T: Serialize,
833 {
834     #[inline]
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,835     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
836     where
837         S: Serializer,
838     {
839         self.0.serialize(serializer)
840     }
841 }
842 
843 ////////////////////////////////////////////////////////////////////////////////
844 
845 #[cfg(all(feature = "std", std_atomic))]
846 macro_rules! atomic_impl {
847     ($($ty:ident)*) => {
848         $(
849             impl Serialize for $ty {
850                 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
851                 where
852                     S: Serializer,
853                 {
854                     self.load(Ordering::SeqCst).serialize(serializer)
855                 }
856             }
857         )*
858     }
859 }
860 
861 #[cfg(all(feature = "std", std_atomic))]
862 atomic_impl! {
863     AtomicBool
864     AtomicI8 AtomicI16 AtomicI32 AtomicIsize
865     AtomicU8 AtomicU16 AtomicU32 AtomicUsize
866 }
867 
868 #[cfg(all(feature = "std", std_atomic64))]
869 atomic_impl! {
870     AtomicI64 AtomicU64
871 }
872