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