• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // These just test that serde_derive is able to produce code that compiles
2 // successfully when there are a variety of generics and non-(de)serializable
3 // types involved.
4 
5 #![deny(warnings)]
6 #![allow(
7     unknown_lints,
8     mixed_script_confusables,
9     clippy::derive_partial_eq_without_eq,
10     clippy::extra_unused_type_parameters,
11     clippy::items_after_statements,
12     clippy::missing_errors_doc,
13     clippy::missing_panics_doc,
14     clippy::must_use_candidate,
15     // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
16     clippy::nonstandard_macro_braces,
17     clippy::ptr_arg,
18     clippy::too_many_lines,
19     clippy::trivially_copy_pass_by_ref,
20     clippy::type_repetition_in_bounds
21 )]
22 
23 use serde::de::{Deserialize, DeserializeOwned, Deserializer};
24 use serde::ser::{Serialize, Serializer};
25 use serde_derive::{Deserialize, Serialize};
26 use std::borrow::Cow;
27 use std::marker::PhantomData;
28 use std::option::Option as StdOption;
29 use std::result::Result as StdResult;
30 
31 // Try to trip up the generated code if it fails to use fully qualified paths.
32 #[allow(dead_code)]
33 struct Result;
34 #[allow(dead_code)]
35 struct Ok;
36 #[allow(dead_code)]
37 struct Err;
38 #[allow(dead_code)]
39 struct Option;
40 #[allow(dead_code)]
41 struct Some;
42 #[allow(dead_code)]
43 struct None;
44 
45 //////////////////////////////////////////////////////////////////////////
46 
47 #[test]
test_gen()48 fn test_gen() {
49     #[derive(Serialize, Deserialize)]
50     struct With<T> {
51         t: T,
52         #[serde(serialize_with = "ser_x", deserialize_with = "de_x")]
53         x: X,
54     }
55     assert::<With<i32>>();
56 
57     #[derive(Serialize, Deserialize)]
58     struct WithTogether<T> {
59         t: T,
60         #[serde(with = "both_x")]
61         x: X,
62     }
63     assert::<WithTogether<i32>>();
64 
65     #[derive(Serialize, Deserialize)]
66     struct WithRef<'a, T: 'a> {
67         #[serde(skip_deserializing)]
68         t: StdOption<&'a T>,
69         #[serde(serialize_with = "ser_x", deserialize_with = "de_x")]
70         x: X,
71     }
72     assert::<WithRef<i32>>();
73 
74     #[derive(Serialize, Deserialize)]
75     struct PhantomX {
76         x: PhantomData<X>,
77     }
78     assert::<PhantomX>();
79 
80     #[derive(Serialize, Deserialize)]
81     struct PhantomT<T> {
82         t: PhantomData<T>,
83     }
84     assert::<PhantomT<X>>();
85 
86     #[derive(Serialize, Deserialize)]
87     struct NoBounds<T> {
88         t: T,
89         option: StdOption<T>,
90         boxed: Box<T>,
91         option_boxed: StdOption<Box<T>>,
92     }
93     assert::<NoBounds<i32>>();
94 
95     #[derive(Serialize, Deserialize)]
96     enum EnumWith<T> {
97         Unit,
98         Newtype(#[serde(serialize_with = "ser_x", deserialize_with = "de_x")] X),
99         Tuple(
100             T,
101             #[serde(serialize_with = "ser_x", deserialize_with = "de_x")] X,
102         ),
103         Struct {
104             t: T,
105             #[serde(serialize_with = "ser_x", deserialize_with = "de_x")]
106             x: X,
107         },
108     }
109     assert::<EnumWith<i32>>();
110 
111     #[derive(Serialize)]
112     struct MultipleRef<'a, 'b, 'c, T>
113     where
114         T: 'c,
115         'c: 'b,
116         'b: 'a,
117     {
118         t: T,
119         rrrt: &'a &'b &'c T,
120     }
121     assert_ser::<MultipleRef<i32>>();
122 
123     #[derive(Serialize, Deserialize)]
124     struct Newtype(#[serde(serialize_with = "ser_x", deserialize_with = "de_x")] X);
125     assert::<Newtype>();
126 
127     #[derive(Serialize, Deserialize)]
128     struct Tuple<T>(
129         T,
130         #[serde(serialize_with = "ser_x", deserialize_with = "de_x")] X,
131     );
132     assert::<Tuple<i32>>();
133 
134     #[derive(Serialize, Deserialize)]
135     enum TreeNode<D> {
136         Split {
137             left: Box<TreeNode<D>>,
138             right: Box<TreeNode<D>>,
139         },
140         Leaf {
141             data: D,
142         },
143     }
144     assert::<TreeNode<i32>>();
145 
146     #[derive(Serialize, Deserialize)]
147     struct ListNode<D> {
148         data: D,
149         next: Box<ListNode<D>>,
150     }
151     assert::<ListNode<i32>>();
152 
153     #[derive(Serialize, Deserialize)]
154     struct RecursiveA {
155         b: Box<RecursiveB>,
156     }
157     assert::<RecursiveA>();
158 
159     #[derive(Serialize, Deserialize)]
160     enum RecursiveB {
161         A(RecursiveA),
162     }
163     assert::<RecursiveB>();
164 
165     #[derive(Serialize, Deserialize)]
166     struct RecursiveGenericA<T> {
167         t: T,
168         b: Box<RecursiveGenericB<T>>,
169     }
170     assert::<RecursiveGenericA<i32>>();
171 
172     #[derive(Serialize, Deserialize)]
173     enum RecursiveGenericB<T> {
174         T(T),
175         A(RecursiveGenericA<T>),
176     }
177     assert::<RecursiveGenericB<i32>>();
178 
179     #[derive(Serialize)]
180     struct OptionStatic<'a> {
181         a: StdOption<&'a str>,
182         b: StdOption<&'static str>,
183     }
184     assert_ser::<OptionStatic>();
185 
186     #[derive(Serialize, Deserialize)]
187     #[serde(bound = "D: SerializeWith + DeserializeWith")]
188     struct WithTraits1<D, E> {
189         #[serde(
190             serialize_with = "SerializeWith::serialize_with",
191             deserialize_with = "DeserializeWith::deserialize_with"
192         )]
193         d: D,
194         #[serde(
195             serialize_with = "SerializeWith::serialize_with",
196             deserialize_with = "DeserializeWith::deserialize_with",
197             bound = "E: SerializeWith + DeserializeWith"
198         )]
199         e: E,
200     }
201     assert::<WithTraits1<X, X>>();
202 
203     #[derive(Serialize, Deserialize)]
204     #[serde(bound(serialize = "D: SerializeWith", deserialize = "D: DeserializeWith"))]
205     struct WithTraits2<D, E> {
206         #[serde(
207             serialize_with = "SerializeWith::serialize_with",
208             deserialize_with = "DeserializeWith::deserialize_with"
209         )]
210         d: D,
211         #[serde(
212             serialize_with = "SerializeWith::serialize_with",
213             bound(serialize = "E: SerializeWith")
214         )]
215         #[serde(
216             deserialize_with = "DeserializeWith::deserialize_with",
217             bound(deserialize = "E: DeserializeWith")
218         )]
219         e: E,
220     }
221     assert::<WithTraits2<X, X>>();
222 
223     #[derive(Serialize, Deserialize)]
224     #[serde(bound = "D: SerializeWith + DeserializeWith")]
225     enum VariantWithTraits1<D, E> {
226         #[serde(
227             serialize_with = "SerializeWith::serialize_with",
228             deserialize_with = "DeserializeWith::deserialize_with"
229         )]
230         D(D),
231         #[serde(
232             serialize_with = "SerializeWith::serialize_with",
233             deserialize_with = "DeserializeWith::deserialize_with",
234             bound = "E: SerializeWith + DeserializeWith"
235         )]
236         E(E),
237     }
238     assert::<VariantWithTraits1<X, X>>();
239 
240     #[derive(Serialize, Deserialize)]
241     #[serde(bound(serialize = "D: SerializeWith", deserialize = "D: DeserializeWith"))]
242     enum VariantWithTraits2<D, E> {
243         #[serde(
244             serialize_with = "SerializeWith::serialize_with",
245             deserialize_with = "DeserializeWith::deserialize_with"
246         )]
247         D(D),
248         #[serde(
249             serialize_with = "SerializeWith::serialize_with",
250             bound(serialize = "E: SerializeWith")
251         )]
252         #[serde(
253             deserialize_with = "DeserializeWith::deserialize_with",
254             bound(deserialize = "E: DeserializeWith")
255         )]
256         E(E),
257     }
258     assert::<VariantWithTraits2<X, X>>();
259 
260     type PhantomDataAlias<T> = PhantomData<T>;
261 
262     #[derive(Serialize, Deserialize)]
263     #[serde(bound = "")]
264     struct PhantomDataWrapper<T> {
265         #[serde(default)]
266         field: PhantomDataAlias<T>,
267     }
268     assert::<PhantomDataWrapper<X>>();
269 
270     #[derive(Serialize, Deserialize)]
271     struct CowStr<'a>(Cow<'a, str>);
272     assert::<CowStr>();
273 
274     #[derive(Serialize, Deserialize)]
275     #[serde(bound(deserialize = "T::Owned: DeserializeOwned"))]
276     struct CowT<'a, T: ?Sized + 'a + ToOwned>(Cow<'a, T>);
277     assert::<CowT<str>>();
278 
279     #[derive(Serialize, Deserialize)]
280     struct EmptyStruct {}
281     assert::<EmptyStruct>();
282 
283     #[derive(Serialize, Deserialize)]
284     enum EmptyEnumVariant {
285         EmptyStruct {},
286     }
287     assert::<EmptyEnumVariant>();
288 
289     #[derive(Serialize, Deserialize)]
290     struct NonAsciiIdents {
291         σ: f64,
292     }
293 
294     #[derive(Serialize, Deserialize)]
295     struct EmptyBraced {}
296 
297     #[derive(Serialize, Deserialize)]
298     #[serde(deny_unknown_fields)]
299     struct EmptyBracedDenyUnknown {}
300 
301     #[derive(Serialize, Deserialize)]
302     struct BracedSkipAll {
303         #[serde(skip_deserializing)]
304         f: u8,
305     }
306 
307     #[derive(Serialize, Deserialize)]
308     #[serde(deny_unknown_fields)]
309     struct BracedSkipAllDenyUnknown {
310         #[serde(skip_deserializing)]
311         f: u8,
312     }
313 
314     #[derive(Serialize, Deserialize)]
315     struct EmptyTuple();
316 
317     #[derive(Serialize, Deserialize)]
318     #[serde(deny_unknown_fields)]
319     struct EmptyTupleDenyUnknown();
320 
321     #[derive(Serialize, Deserialize)]
322     struct TupleSkipAll(#[serde(skip_deserializing)] u8);
323 
324     #[derive(Serialize, Deserialize)]
325     #[serde(deny_unknown_fields)]
326     struct TupleSkipAllDenyUnknown(#[serde(skip_deserializing)] u8);
327 
328     #[derive(Serialize, Deserialize)]
329     enum EmptyEnum {}
330 
331     #[derive(Serialize, Deserialize)]
332     #[serde(deny_unknown_fields)]
333     enum EmptyEnumDenyUnknown {}
334 
335     #[derive(Serialize, Deserialize)]
336     enum EnumSkipAll {
337         #[serde(skip_deserializing)]
338         #[allow(dead_code)]
339         Variant,
340     }
341 
342     #[derive(Serialize, Deserialize)]
343     enum EmptyVariants {
344         Braced {},
345         Tuple(),
346         BracedSkip {
347             #[serde(skip_deserializing)]
348             f: u8,
349         },
350         TupleSkip(#[serde(skip_deserializing)] u8),
351     }
352 
353     #[derive(Serialize, Deserialize)]
354     #[serde(deny_unknown_fields)]
355     enum EmptyVariantsDenyUnknown {
356         Braced {},
357         Tuple(),
358         BracedSkip {
359             #[serde(skip_deserializing)]
360             f: u8,
361         },
362         TupleSkip(#[serde(skip_deserializing)] u8),
363     }
364 
365     #[derive(Serialize, Deserialize)]
366     #[serde(deny_unknown_fields)]
367     struct UnitDenyUnknown;
368 
369     #[derive(Serialize, Deserialize)]
370     struct EmptyArray {
371         empty: [X; 0],
372     }
373 
374     enum Or<A, B> {
375         A(A),
376         B(B),
377     }
378 
379     #[derive(Serialize, Deserialize)]
380     #[serde(untagged, remote = "Or")]
381     enum OrDef<A, B> {
382         A(A),
383         B(B),
384     }
385 
386     struct Str<'a>(&'a str);
387 
388     #[derive(Serialize, Deserialize)]
389     #[serde(remote = "Str")]
390     struct StrDef<'a>(&'a str);
391 
392     #[derive(Serialize, Deserialize)]
393     struct Remote<'a> {
394         #[serde(with = "OrDef")]
395         or: Or<u8, bool>,
396         #[serde(borrow, with = "StrDef")]
397         s: Str<'a>,
398     }
399 
400     #[derive(Serialize, Deserialize)]
401     enum BorrowVariant<'a> {
402         #[serde(borrow, with = "StrDef")]
403         S(Str<'a>),
404     }
405 
406     mod vis {
407         use serde_derive::{Deserialize, Serialize};
408 
409         pub struct S;
410 
411         #[derive(Serialize, Deserialize)]
412         #[serde(remote = "S")]
413         pub struct SDef;
414     }
415 
416     // This would not work if SDef::serialize / deserialize are private.
417     #[derive(Serialize, Deserialize)]
418     struct RemoteVisibility {
419         #[serde(with = "vis::SDef")]
420         s: vis::S,
421     }
422 
423     #[derive(Serialize, Deserialize)]
424     #[serde(remote = "Self")]
425     struct RemoteSelf;
426 
427     #[derive(Serialize, Deserialize)]
428     enum ExternallyTaggedVariantWith {
429         #[serde(serialize_with = "ser_x")]
430         #[serde(deserialize_with = "de_x")]
431         #[allow(dead_code)]
432         Newtype(X),
433 
434         #[serde(serialize_with = "serialize_some_other_variant")]
435         #[serde(deserialize_with = "deserialize_some_other_variant")]
436         #[allow(dead_code)]
437         Tuple(String, u8),
438 
439         #[serde(serialize_with = "ser_x")]
440         #[serde(deserialize_with = "de_x")]
441         #[allow(dead_code)]
442         Struct1 { x: X },
443 
444         #[serde(serialize_with = "serialize_some_other_variant")]
445         #[serde(deserialize_with = "deserialize_some_other_variant")]
446         #[allow(dead_code)]
447         Struct { f1: String, f2: u8 },
448 
449         #[serde(serialize_with = "serialize_some_unit_variant")]
450         #[serde(deserialize_with = "deserialize_some_unit_variant")]
451         #[allow(dead_code)]
452         Unit,
453     }
454     assert_ser::<ExternallyTaggedVariantWith>();
455 
456     #[derive(Serialize, Deserialize)]
457     #[serde(tag = "t")]
458     enum InternallyTaggedVariantWith {
459         #[serde(serialize_with = "ser_x")]
460         #[serde(deserialize_with = "de_x")]
461         #[allow(dead_code)]
462         Newtype(X),
463 
464         #[serde(serialize_with = "ser_x")]
465         #[serde(deserialize_with = "de_x")]
466         #[allow(dead_code)]
467         Struct1 { x: X },
468 
469         #[serde(serialize_with = "serialize_some_other_variant")]
470         #[serde(deserialize_with = "deserialize_some_other_variant")]
471         #[allow(dead_code)]
472         Struct { f1: String, f2: u8 },
473 
474         #[serde(serialize_with = "serialize_some_unit_variant")]
475         #[serde(deserialize_with = "deserialize_some_unit_variant")]
476         #[allow(dead_code)]
477         Unit,
478     }
479     assert_ser::<InternallyTaggedVariantWith>();
480 
481     #[derive(Serialize, Deserialize)]
482     #[serde(tag = "t", content = "c")]
483     enum AdjacentlyTaggedVariantWith {
484         #[serde(serialize_with = "ser_x")]
485         #[serde(deserialize_with = "de_x")]
486         #[allow(dead_code)]
487         Newtype(X),
488 
489         #[serde(serialize_with = "serialize_some_other_variant")]
490         #[serde(deserialize_with = "deserialize_some_other_variant")]
491         #[allow(dead_code)]
492         Tuple(String, u8),
493 
494         #[serde(serialize_with = "ser_x")]
495         #[serde(deserialize_with = "de_x")]
496         #[allow(dead_code)]
497         Struct1 { x: X },
498 
499         #[serde(serialize_with = "serialize_some_other_variant")]
500         #[serde(deserialize_with = "deserialize_some_other_variant")]
501         #[allow(dead_code)]
502         Struct { f1: String, f2: u8 },
503 
504         #[serde(serialize_with = "serialize_some_unit_variant")]
505         #[serde(deserialize_with = "deserialize_some_unit_variant")]
506         #[allow(dead_code)]
507         Unit,
508     }
509     assert_ser::<AdjacentlyTaggedVariantWith>();
510 
511     #[derive(Serialize, Deserialize)]
512     #[serde(untagged)]
513     enum UntaggedVariantWith {
514         #[serde(serialize_with = "ser_x")]
515         #[serde(deserialize_with = "de_x")]
516         #[allow(dead_code)]
517         Newtype(X),
518 
519         #[serde(serialize_with = "serialize_some_other_variant")]
520         #[serde(deserialize_with = "deserialize_some_other_variant")]
521         #[allow(dead_code)]
522         Tuple(String, u8),
523 
524         #[serde(serialize_with = "ser_x")]
525         #[serde(deserialize_with = "de_x")]
526         #[allow(dead_code)]
527         Struct1 { x: X },
528 
529         #[serde(serialize_with = "serialize_some_other_variant")]
530         #[serde(deserialize_with = "deserialize_some_other_variant")]
531         #[allow(dead_code)]
532         Struct { f1: String, f2: u8 },
533 
534         #[serde(serialize_with = "serialize_some_unit_variant")]
535         #[serde(deserialize_with = "deserialize_some_unit_variant")]
536         #[allow(dead_code)]
537         Unit,
538     }
539     assert_ser::<UntaggedVariantWith>();
540 
541     #[derive(Serialize, Deserialize)]
542     struct FlattenWith {
543         #[serde(flatten, serialize_with = "ser_x", deserialize_with = "de_x")]
544         x: X,
545     }
546     assert::<FlattenWith>();
547 
548     #[derive(Serialize, Deserialize)]
549     #[serde(deny_unknown_fields)]
550     struct FlattenDenyUnknown<T> {
551         #[serde(flatten)]
552         t: T,
553     }
554 
555     #[derive(Serialize, Deserialize)]
556     struct StaticStrStruct<'a> {
557         a: &'a str,
558         b: &'static str,
559     }
560 
561     #[derive(Serialize, Deserialize)]
562     struct StaticStrTupleStruct<'a>(&'a str, &'static str);
563 
564     #[derive(Serialize, Deserialize)]
565     struct StaticStrNewtypeStruct(&'static str);
566 
567     #[derive(Serialize, Deserialize)]
568     enum StaticStrEnum<'a> {
569         Struct { a: &'a str, b: &'static str },
570         Tuple(&'a str, &'static str),
571         Newtype(&'static str),
572     }
573 
574     #[derive(Serialize, Deserialize)]
575     struct SkippedStaticStr {
576         #[serde(skip_deserializing)]
577         skipped: &'static str,
578         other: isize,
579     }
580     assert::<SkippedStaticStr>();
581 
582     macro_rules! T {
583         () => {
584             ()
585         };
586     }
587 
588     #[derive(Serialize, Deserialize)]
589     struct TypeMacro<T> {
590         mac: T!(),
591         marker: PhantomData<T>,
592     }
593     assert::<TypeMacro<X>>();
594 
595     #[derive(Serialize)]
596     struct BigArray {
597         #[serde(serialize_with = "<[_]>::serialize")]
598         array: [u8; 256],
599     }
600     assert_ser::<BigArray>();
601 
602     trait AssocSerde {
603         type Assoc;
604     }
605 
606     struct NoSerdeImpl;
607     impl AssocSerde for NoSerdeImpl {
608         type Assoc = u32;
609     }
610 
611     #[derive(Serialize, Deserialize)]
612     struct AssocDerive<T: AssocSerde> {
613         assoc: T::Assoc,
614     }
615 
616     assert::<AssocDerive<NoSerdeImpl>>();
617 
618     #[derive(Serialize, Deserialize)]
619     struct AssocDeriveMulti<S, T: AssocSerde> {
620         s: S,
621         assoc: T::Assoc,
622     }
623 
624     assert::<AssocDeriveMulti<i32, NoSerdeImpl>>();
625 
626     #[derive(Serialize)]
627     #[serde(tag = "t", content = "c")]
628     enum EmptyAdjacentlyTagged {
629         #[allow(dead_code)]
630         Struct {},
631         #[allow(dead_code)]
632         Tuple(),
633     }
634 
635     assert_ser::<EmptyAdjacentlyTagged>();
636 
637     mod restricted {
638         mod inner {
639             use serde_derive::{Deserialize, Serialize};
640 
641             #[derive(Serialize, Deserialize)]
642             struct Restricted {
643                 pub(super) a: usize,
644                 pub(in super::inner) b: usize,
645             }
646         }
647     }
648 
649     #[derive(Deserialize)]
650     #[serde(tag = "t", content = "c")]
651     enum AdjacentlyTaggedVoid {}
652 
653     #[derive(Serialize, Deserialize)]
654     enum SkippedVariant<T> {
655         #[serde(skip)]
656         #[allow(dead_code)]
657         T(T),
658         Unit,
659     }
660 
661     assert::<SkippedVariant<X>>();
662 
663     #[derive(Deserialize)]
664     struct ImplicitlyBorrowedOption<'a> {
665         #[allow(dead_code)]
666         option: std::option::Option<&'a str>,
667     }
668 
669     #[derive(Serialize, Deserialize)]
670     #[serde(untagged)]
671     enum UntaggedNewtypeVariantWith {
672         Newtype(
673             #[serde(serialize_with = "ser_x")]
674             #[serde(deserialize_with = "de_x")]
675             X,
676         ),
677     }
678 
679     #[derive(Serialize, Deserialize)]
680     #[serde(transparent)]
681     struct TransparentWith {
682         #[serde(serialize_with = "ser_x")]
683         #[serde(deserialize_with = "de_x")]
684         x: X,
685     }
686 
687     #[derive(Deserialize)]
688     #[serde(untagged)]
689     pub enum UntaggedWithBorrow<'a> {
690         Single(#[serde(borrow)] RelObject<'a>),
691         Many(#[serde(borrow)] Vec<RelObject<'a>>),
692     }
693 
694     #[derive(Deserialize)]
695     struct RelObject<'a> {
696         #[allow(dead_code)]
697         ty: &'a str,
698         #[allow(dead_code)]
699         id: String,
700     }
701 
702     #[derive(Serialize, Deserialize)]
703     struct FlattenSkipSerializing<T> {
704         #[serde(flatten, skip_serializing)]
705         #[allow(dead_code)]
706         flat: T,
707     }
708 
709     #[derive(Serialize, Deserialize)]
710     struct FlattenSkipSerializingIf<T> {
711         #[serde(flatten, skip_serializing_if = "StdOption::is_none")]
712         flat: StdOption<T>,
713     }
714 
715     #[derive(Serialize, Deserialize)]
716     struct FlattenSkipDeserializing<T> {
717         #[serde(flatten, skip_deserializing)]
718         flat: T,
719     }
720 
721     // https://github.com/serde-rs/serde/issues/1804
722     #[derive(Serialize, Deserialize)]
723     enum Message {
724         #[serde(skip)]
725         #[allow(dead_code)]
726         String(String),
727         #[serde(other)]
728         Unknown,
729     }
730 
731     #[derive(Serialize)]
732     #[repr(packed)]
733     struct Packed {
734         x: u8,
735         y: u16,
736     }
737 
738     macro_rules! deriving {
739         ($field:ty) => {
740             #[derive(Deserialize)]
741             struct MacroRules<'a> {
742                 #[allow(dead_code)]
743                 field: $field,
744             }
745         };
746     }
747 
748     deriving!(&'a str);
749 
750     macro_rules! mac {
751         ($($tt:tt)*) => {
752             $($tt)*
753         };
754     }
755 
756     #[derive(Deserialize)]
757     struct BorrowLifetimeInsideMacro<'a> {
758         #[serde(borrow = "'a")]
759         #[allow(dead_code)]
760         f: mac!(Cow<'a, str>),
761     }
762 
763     #[derive(Serialize)]
764     struct Struct {
765         #[serde(serialize_with = "vec_first_element")]
766         vec: Vec<Self>,
767     }
768 
769     #[derive(Deserialize)]
770     #[serde(bound(deserialize = "[&'de str; N]: Copy"))]
771     struct GenericUnitStruct<const N: usize>;
772 }
773 
774 //////////////////////////////////////////////////////////////////////////
775 
assert<T: Serialize + DeserializeOwned>()776 fn assert<T: Serialize + DeserializeOwned>() {}
assert_ser<T: Serialize>()777 fn assert_ser<T: Serialize>() {}
778 
779 trait SerializeWith {
serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error>780     fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error>;
781 }
782 
783 trait DeserializeWith: Sized {
deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult<Self, D::Error>784     fn deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult<Self, D::Error>;
785 }
786 
787 // Implements neither Serialize nor Deserialize
788 pub struct X;
789 
ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error>790 pub fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
791     unimplemented!()
792 }
793 
de_x<'de, D: Deserializer<'de>>(_: D) -> StdResult<X, D::Error>794 pub fn de_x<'de, D: Deserializer<'de>>(_: D) -> StdResult<X, D::Error> {
795     unimplemented!()
796 }
797 
798 mod both_x {
799     pub use super::{de_x as deserialize, ser_x as serialize};
800 }
801 
802 impl SerializeWith for X {
serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error>803     fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error> {
804         unimplemented!()
805     }
806 }
807 
808 impl DeserializeWith for X {
deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult<Self, D::Error>809     fn deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult<Self, D::Error> {
810         unimplemented!()
811     }
812 }
813 
serialize_some_unit_variant<S>(_: S) -> StdResult<S::Ok, S::Error> where S: Serializer,814 pub fn serialize_some_unit_variant<S>(_: S) -> StdResult<S::Ok, S::Error>
815 where
816     S: Serializer,
817 {
818     unimplemented!()
819 }
820 
deserialize_some_unit_variant<'de, D>(_: D) -> StdResult<(), D::Error> where D: Deserializer<'de>,821 pub fn deserialize_some_unit_variant<'de, D>(_: D) -> StdResult<(), D::Error>
822 where
823     D: Deserializer<'de>,
824 {
825     unimplemented!()
826 }
827 
serialize_some_other_variant<S>(_: &str, _: &u8, _: S) -> StdResult<S::Ok, S::Error> where S: Serializer,828 pub fn serialize_some_other_variant<S>(_: &str, _: &u8, _: S) -> StdResult<S::Ok, S::Error>
829 where
830     S: Serializer,
831 {
832     unimplemented!()
833 }
834 
deserialize_some_other_variant<'de, D>(_: D) -> StdResult<(String, u8), D::Error> where D: Deserializer<'de>,835 pub fn deserialize_some_other_variant<'de, D>(_: D) -> StdResult<(String, u8), D::Error>
836 where
837     D: Deserializer<'de>,
838 {
839     unimplemented!()
840 }
841 
is_zero(n: &u8) -> bool842 pub fn is_zero(n: &u8) -> bool {
843     *n == 0
844 }
845 
vec_first_element<T, S>(vec: &[T], serializer: S) -> StdResult<S::Ok, S::Error> where T: Serialize, S: Serializer,846 fn vec_first_element<T, S>(vec: &[T], serializer: S) -> StdResult<S::Ok, S::Error>
847 where
848     T: Serialize,
849     S: Serializer,
850 {
851     vec.first().serialize(serializer)
852 }
853 
854 //////////////////////////////////////////////////////////////////////////
855 
856 #[derive(Debug, PartialEq, Deserialize)]
857 #[serde(tag = "tag")]
858 enum InternallyTagged {
859     #[serde(deserialize_with = "deserialize_generic")]
860     Unit,
861 
862     #[serde(deserialize_with = "deserialize_generic")]
863     Newtype(i32),
864 
865     #[serde(deserialize_with = "deserialize_generic")]
866     Struct { f1: String, f2: u8 },
867 }
868 
deserialize_generic<'de, T, D>(deserializer: D) -> StdResult<T, D::Error> where T: Deserialize<'de>, D: Deserializer<'de>,869 fn deserialize_generic<'de, T, D>(deserializer: D) -> StdResult<T, D::Error>
870 where
871     T: Deserialize<'de>,
872     D: Deserializer<'de>,
873 {
874     T::deserialize(deserializer)
875 }
876 
877 //////////////////////////////////////////////////////////////////////////
878 
879 #[repr(packed)]
880 pub struct RemotePacked {
881     pub a: u16,
882     pub b: u32,
883 }
884 
885 #[derive(Serialize)]
886 #[repr(packed)]
887 #[serde(remote = "RemotePacked")]
888 pub struct RemotePackedDef {
889     a: u16,
890     b: u32,
891 }
892 
893 impl Drop for RemotePackedDef {
drop(&mut self)894     fn drop(&mut self) {}
895 }
896 
897 #[repr(packed)]
898 pub struct RemotePackedNonCopy {
899     pub a: u16,
900     pub b: String,
901 }
902 
903 #[derive(Deserialize)]
904 #[repr(packed)]
905 #[serde(remote = "RemotePackedNonCopy")]
906 pub struct RemotePackedNonCopyDef {
907     a: u16,
908     b: String,
909 }
910 
911 impl Drop for RemotePackedNonCopyDef {
drop(&mut self)912     fn drop(&mut self) {}
913 }
914