Compare commits

...

293 Commits

Author SHA1 Message Date
David Tolnay 3c7dd6fc1e Release 1.0.185 2023-08-20 21:41:21 -07:00
David Tolnay 8b196ea1c8 Merge pull request #2592 from dtolnay/remotenonexhaustive
Fix "cannot move out of *self which is behind a shared reference"
2023-08-20 21:40:37 -07:00
David Tolnay 1f8c8ad5a3 Fix "cannot move out of *self which is behind a shared reference" 2023-08-20 21:37:14 -07:00
David Tolnay 870925d503 Add repro of issue 2591
error[E0507]: cannot move out of `*__self` which is behind a shared reference
       --> test_suite/tests/test_remote.rs:210:10
        |
    210 | #[derive(Serialize, Deserialize)]
        |          ^^^^^^^^^
        |          |
        |          data moved here
        |          move occurs because `unrecognized` has type `ErrorKind`, which does not implement the `Copy` trait
        |
        = note: this error originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
    help: consider borrowing here
        |
    210 | #[derive(&Serialize, Deserialize)]
        |          +
2023-08-20 21:36:30 -07:00
David Tolnay d593215ef7 No need for slow macOS CI if there is no platform-specific code 2023-08-20 21:27:07 -07:00
David Tolnay 110af31b48 Merge pull request #2590 from pinkforest/phase-out-precompiled
Phase out precompiled
2023-08-20 21:00:39 -07:00
pinkforest 360606b9a6 Following consensus on: #2580 (review)
This PR phases out the precompiled per final consensus made in #2580
i#
Fix a cfg
2023-08-21 13:53:48 +10:00
David Tolnay 151b45ae36 Release 1.0.184 2023-08-20 19:49:55 -07:00
Oli Scherer 4617c957b9 Merge pull request #2587 from wucke13/master
fix shebang in build.sh
2023-08-21 00:22:31 +02:00
wucke13 2547ed83ca fix shebang in build.sh
This fixes #2583.
2023-08-20 23:51:47 +02:00
David Tolnay bfcd44704f Discard the possibility of upx compressing binary
This adds too much decompression overhead per invocation.

It could work if the subprocess were reused across multiple macro
expansions (https://github.com/serde-rs/serde/pull/2523).
2023-08-17 19:07:42 -07:00
David Tolnay 7b548db91e Merge pull request #2572 from serde-rs/cargorm
Adopt `cargo rm` after Cargo bugfix
2023-08-14 04:21:23 -07:00
David Tolnay d39dea85ad Adopt 'cargo rm' after Cargo bugfix 2023-08-14 04:12:55 -07:00
David Tolnay 5e56c9fba8 Merge pull request #2570 from dtolnay/remotenonexhaustive
Treat unmatched non-exhaustive remote variant as serde(skip)
2023-08-13 21:20:42 -07:00
David Tolnay 8d3a03288b Treat unmatched non-exhaustive remote variant as serde(skip) 2023-08-13 21:14:33 -07:00
David Tolnay cb490ec16d Add test of remote enum with non_exhaustive 2023-08-13 21:14:12 -07:00
David Tolnay 45271c3676 Resolve ignored_unit_patterns pedantic clippy lint
warning: matching over `()` is more explicit
       --> serde_derive/src/internals/attr.rs:710:33
        |
    710 |         (Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {
        |                                 ^ help: use `()` instead of `_`: `()`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns
        = note: `-W clippy::ignored-unit-patterns` implied by `-W clippy::pedantic`

    warning: matching over `()` is more explicit
       --> serde_derive/src/internals/attr.rs:721:33
        |
    721 |         (Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {
        |                                 ^ help: use `()` instead of `_`: `()`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns

    warning: matching over `()` is more explicit
       --> serde_derive/src/internals/attr.rs:728:33
        |
    728 |         (Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {
        |                                 ^ help: use `()` instead of `_`: `()`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns

    warning: matching over `()` is more explicit
       --> serde_derive/src/internals/attr.rs:750:44
        |
    750 |         (_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
        |                                            ^ help: use `()` instead of `_`: `()`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns

    warning: matching over `()` is more explicit
       --> serde_derive/src/internals/attr.rs:750:82
        |
    750 |         (_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
        |                                                                                  ^ help: use `()` instead of `_`: `()`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns
2023-08-11 20:27:21 -07:00
David Tolnay 05a5b7e3c6 Release 1.0.183 2023-08-06 21:00:14 -07:00
David Tolnay 3bff326fb3 Merge pull request #2555 from Mingun/field
Refactor code that generates `__Field` enums
2023-08-06 18:23:52 -07:00
David Tolnay aaadd93878 Merge pull request #2556 from DBLouis/master
Add forward impl for OsStr
2023-08-06 18:02:21 -07:00
Louis Dupré Bertoni 9c864f0b02 Add forward impl for OsStr 2023-08-06 19:41:19 +03:00
Mingun 070cce0d9c Get rid of temporary variable 2023-08-06 19:55:48 +05:00
Mingun b58e8bac12 Replace if let Some(...) = ... to Option::map 2023-08-06 19:53:39 +05:00
Mingun ada50b077e ignore_variant variable is always None, let's take this into account 2023-08-06 19:36:48 +05:00
Mingun 5e313a7330 Move generiс code out-of-function, create more specialized and simple code 2023-08-06 19:35:27 +05:00
Mingun 2a36d11238 Introduce a dedicated function for generating Field enum
(the enum that represents all fields of a struct)
2023-08-06 19:32:53 +05:00
David Tolnay b6685cf9dd Release 1.0.182 2023-08-05 22:16:46 -07:00
David Tolnay fc273c6763 Resolve needless_return clippy lint in PR 2553
warning: unneeded `return` statement
        --> serde_derive/src/de.rs:2986:13
         |
    2986 |             return quote!(#assign_to __default.#member);
         |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
         = note: `-W clippy::needless-return` implied by `-W clippy::all`
    help: remove `return`
         |
    2986 -             return quote!(#assign_to __default.#member);
    2986 +             quote!(#assign_to __default.#member)
         |
2023-08-05 22:16:19 -07:00
David Tolnay bd7b0e257e Touch up PR 2553 2023-08-05 22:13:09 -07:00
David Tolnay 73931692b2 Merge pull request #2553 from Mingun/default-on-tuples
Allow `#[serde(default)]` on tuple structs
2023-08-05 22:12:10 -07:00
David Tolnay 4d93e9f44c Keep deserialize aliases as a sorted set 2023-08-05 17:06:11 -07:00
David Tolnay da55ed7e8d Remove some clones of names of things 2023-08-05 17:01:34 -07:00
David Tolnay e3617e1f28 Update explanation of correct_aliases 2023-08-05 16:39:38 -07:00
David Tolnay 431636af0d Merge pull request #2458 from Mingun/identifier
Keep aliases always sorted and include aliases in expecting message for field/variant_identifier
2023-08-05 16:39:14 -07:00
David Tolnay 891ced598a Update test suite to nightly-2023-08-05
error: the feature `lang_items` is internal to the compiler or standard library
     --> src/main.rs:1:12
      |
    1 | #![feature(lang_items, start)]
      |            ^^^^^^^^^^
      |
      = note: using it is strongly discouraged
      = note: `#[deny(internal_features)]` on by default
2023-08-04 19:09:00 -07:00
David Tolnay 57dc0ee769 Release 1.0.181 2023-08-03 16:58:45 -07:00
David Tolnay 5e102c4da1 Relocate private size_hint module
Let's keep crate::__private for only things that *need* to be accessible
to the macro-generated code. Size_hint can be pub(crate).
2023-08-03 16:53:44 -07:00
Mingun 5c33931422 Allow #[serde(default)] on tuple structs 2023-08-03 22:32:34 +05:00
David Tolnay 4aa54222f4 Delete double reference when setting up adjacently tagged variant seed 2023-08-01 22:54:47 -07:00
David Tolnay ef4f860384 Improve "expecting" message of adjacently tagged enum variant 2023-08-01 22:49:44 -07:00
David Tolnay 9bd52ec5c1 Inline AdjacentlyTaggedEnumVariant::new 2023-08-01 22:38:47 -07:00
David Tolnay 5cdd82d41e Remove Serializer from name of private type that is not a Serializer 2023-08-01 22:26:04 -07:00
David Tolnay 110bf10481 Condense AdjacentlyTaggedEnummVariantVisitor implementation 2023-08-01 22:26:03 -07:00
David Tolnay 43035f6f37 Merge pull request #2505 from Baptistemontan/rework_adjacently_tagged_enum
Revisit of the representation of adjacently tagged enums tag
2023-08-01 22:25:31 -07:00
David Tolnay 83b1a3d5dc Merge pull request #2443 from Mingun/deserialize-in-place
Simplify code in deserialize_in_place_struct and implement #2387 for in-place case
2023-08-01 21:51:22 -07:00
Mingun 878110a4bc Simplify code after dead code elimination 2023-08-01 19:03:21 +05:00
Mingun 59ec8b7db2 Remove dead code - variant_ident and deserializer are always None 2023-08-01 19:03:20 +05:00
Mingun cae1b43829 Inline deserialize_newtype_struct_in_place 2023-08-01 19:03:19 +05:00
Mingun 99fde4ee3e Implement #2387 also for deserialize_in_place method 2023-08-01 19:03:19 +05:00
Mingun afe3872810 Simplify check for missing fields 2023-08-01 19:03:18 +05:00
Mingun 3a3e6bf103 Reorder variables to match order in final quote! 2023-08-01 19:03:18 +05:00
Mingun 935f0bd70f Merge some quote! blocks 2023-08-01 19:03:17 +05:00
Mingun 5c18bfeda6 Inline deserialize_struct_as_struct_in_place_visitor 2023-08-01 19:03:17 +05:00
Baptiste de Montangon 957ef206d1 Revisit of the representation of adjacently tagged enums tag 2023-07-31 20:53:02 +02:00
David Tolnay 0c367838cc Merge pull request #2548 from dtolnay/toolattr
Adopt tool attrs for clippy lint level attributes
2023-07-31 11:48:33 -07:00
David Tolnay 2023cf345f Adopt tool attrs for clippy lint level attributes
Requires rustc 1.31+.
2023-07-31 11:39:31 -07:00
David Tolnay 033d05f70b Release 1.0.180 2023-07-31 11:16:12 -07:00
David Tolnay fe4e3fd3b0 Merge pull request #2547 from dtolnay/tombstone
Delete tombstones of the `__private` module
2023-07-30 22:48:34 -07:00
David Tolnay 8a8a8a70ee Delete tombstones of the __private module
These are previous names of the `__private` module -- first
`serde::export` then `serde::private` then `serde::__private` -- in all
cases marked `doc(hidden)` and documented as not public API. Leaving a
tombstone made rustc give a better diagnostic "module is private" rather
than "unresolved import". But the rename to `__private` was 2.5 years
ago in dd1f4b483e so it's unlikely anyone
is still benefiting from the tombstone at this point.
2023-07-30 22:44:59 -07:00
David Tolnay 339dca828d Merge pull request #2546 from dtolnay/edition
Update to 2018 edition
2023-07-30 22:11:44 -07:00
David Tolnay 0d7349fa4e Resolve ambiguous core import on rustc 1.64 through 1.71
In 1.72+, this is fixed by https://github.com/rust-lang/rust/pull/112086.

    error[E0659]: `core` is ambiguous
       --> serde/src/lib.rs:227:13
        |
    227 |     pub use core::ffi::CStr;
        |             ^^^^ ambiguous name
        |
        = note: ambiguous because of multiple potential import sources
        = note: `core` could refer to a built-in crate
        = help: use `::core` to refer to this crate unambiguously
    note: `core` could also refer to the module defined here
       --> serde/src/lib.rs:166:5
        |
    166 | /     mod core {
    167 | |         #[cfg(not(feature = "std"))]
    168 | |         pub use core::*;
    169 | |         #[cfg(feature = "std")]
    170 | |         pub use std::*;
    171 | |     }
        | |_____^
        = help: use `self::core` to refer to this module unambiguously
2023-07-30 22:06:18 -07:00
David Tolnay 830528d5b1 Update to 2018 edition 2023-07-30 21:45:35 -07:00
David Tolnay ab90fbc7c9 Apply 'cargo fix --edition' 2023-07-30 21:42:57 -07:00
David Tolnay 3eec111e8f Delete support for compilers without crate::-based module system 2023-07-30 21:42:57 -07:00
David Tolnay 9388433642 Rename 'try!' macro to 'tri!' in preparation for 2018 edition
Because 'try' is a keyword in 2018+.
2023-07-30 21:29:53 -07:00
David Tolnay ba12070665 Merge pull request #2545 from dtolnay/up
Delete support for rustc versions 1.19 through 1.27
2023-07-30 21:25:58 -07:00
David Tolnay a57a324d72 Delete support for compilers without NonZero integer types 2023-07-30 21:14:32 -07:00
David Tolnay 92e91b3557 Delete support for compilers without Iterator::try_for_each 2023-07-30 21:14:31 -07:00
David Tolnay 4dcf791706 Delete support for compilers without inclusive range accessors 2023-07-30 21:11:43 -07:00
David Tolnay e77900fb94 Update integer128 case in build script 2023-07-30 21:11:09 -07:00
David Tolnay 1b14cadf20 Delete support for compilers without core::ops::Bound 2023-07-30 21:11:02 -07:00
David Tolnay 4f59cd217a Delete support for compilers without core::time 2023-07-30 21:10:49 -07:00
David Tolnay 27c8b2d66a Delete support for compilers without dynamically sized Rc construction 2023-07-30 20:48:27 -07:00
David Tolnay 89976c2712 Delete support for compilers without PathBuf::into_boxed_path 2023-07-30 20:48:27 -07:00
David Tolnay c91737fef1 Delete support for compilers without CString::into_boxed_c_str 2023-07-30 20:48:27 -07:00
David Tolnay a100719bc6 Delete support for compilers without core::cmp::Reverse 2023-07-30 20:48:27 -07:00
David Tolnay 9a0e149225 Sort version checks in build.rs 2023-07-30 20:48:27 -07:00
David Tolnay 9350927903 Delete support for compilers without std::collections::Bound 2023-07-30 20:48:27 -07:00
David Tolnay 677c13a4ec Merge pull request #2544 from dtolnay/testprecompiled
Add CI job to run test suite against precompiled serde_derive
2023-07-30 18:58:13 -07:00
David Tolnay ee8e1ee7ff Add CI job to run test suite against precompiled serde_derive 2023-07-30 18:50:16 -07:00
David Tolnay f969080b9f Pull in syn fix that makes serde test suite independent of "full" feature
See https://github.com/dtolnay/syn/pull/1491.
2023-07-30 17:39:44 -07:00
David Tolnay c2b16bfbb0 Release 1.0.179 2023-07-30 17:20:10 -07:00
David Tolnay e7df53701c Resolve doc_markdown clippy lint from PR 2448
warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2870:25
         |
    2870 |             /// Reaches crate::private::de::content::VariantDeserializer::tuple_variant
         |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
         = note: `-W clippy::doc-markdown` implied by `-W clippy::pedantic`
    help: try
         |
    2870 |             /// Reaches `crate::private::de::content::VariantDeserializer::tuple_variant`
         |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2871:17
         |
    2871 |             /// Content::Seq case
         |                 ^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2871 |             /// `Content::Seq` case
         |                 ~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2872:21
         |
    2872 |             /// via FlatMapDeserializer::deserialize_enum
         |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2872 |             /// via `FlatMapDeserializer::deserialize_enum`
         |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2894:25
         |
    2894 |             /// Reaches crate::private::de::content::VariantDeserializer::struct_variant
         |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2894 |             /// Reaches `crate::private::de::content::VariantDeserializer::struct_variant`
         |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2895:17
         |
    2895 |             /// Content::Seq case
         |                 ^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2895 |             /// `Content::Seq` case
         |                 ~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2896:21
         |
    2896 |             /// via FlatMapDeserializer::deserialize_enum
         |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2896 |             /// via `FlatMapDeserializer::deserialize_enum`
         |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2921:25
         |
    2921 |             /// Reaches crate::private::de::content::VariantDeserializer::struct_variant
         |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2921 |             /// Reaches `crate::private::de::content::VariantDeserializer::struct_variant`
         |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2922:17
         |
    2922 |             /// Content::Map case
         |                 ^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2922 |             /// `Content::Map` case
         |                 ~~~~~~~~~~~~~~

    warning: item in documentation is missing backticks
        --> test_suite/tests/test_annotations.rs:2923:21
         |
    2923 |             /// via FlatMapDeserializer::deserialize_enum
         |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
    help: try
         |
    2923 |             /// via `FlatMapDeserializer::deserialize_enum`
         |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023-07-30 16:15:29 -07:00
David Tolnay 02c34e490b Resolve redundant_field_names clippy lint from PR 2448
warning: redundant field names in struct initialization
        --> serde/src/private/ser.rs:1278:13
         |
    1278 |             map: map,
         |             ^^^^^^^^ help: replace it with: `map`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
         = note: `-W clippy::redundant-field-names` implied by `-W clippy::all`
2023-07-30 16:07:27 -07:00
David Tolnay 427c839b3d Merge pull request #2448 from Mingun/ser-flatten-enums
Implement serialization of tuple variants of flatten enums
2023-07-30 16:02:37 -07:00
David Tolnay 48aa054f53 Release 1.0.178 2023-07-28 16:09:39 -07:00
David Tolnay 3616860203 Delete broken symlink from precompiled derive sources 2023-07-28 16:09:22 -07:00
David Tolnay 861b0dfea2 Consistently list StdError under 'Re-exports' heading of rustdoc 2023-07-28 16:06:49 -07:00
David Tolnay 8b3d71ae2d Merge pull request #2541 from dtolnay/de-core-error
Fix `serde::de::StdError` in no-std unstable build
2023-07-28 16:05:14 -07:00
David Tolnay ff5442cd9e Add no-std unstable build in CI 2023-07-28 16:00:07 -07:00
David Tolnay 92d686f9a5 Fix serde::de::StdError in no-std unstable build 2023-07-28 15:58:57 -07:00
David Tolnay 7b09cccd77 Merge pull request #2539 from dtolnay/questionmark
Replace `try!` with `?` in serde_derive
2023-07-27 19:20:15 -07:00
David Tolnay 6f1f38d046 Replace 'try!' with '?' in serde_derive 2023-07-27 19:16:11 -07:00
David Tolnay db8f06467b Eliminate workaround for pre-1.17 rustc in serde_derive
The oldest compiler supported by serde_derive by this point is 1.56.
2023-07-27 19:05:57 -07:00
David Tolnay 91ec1c290f Enforce question mark not used in serde crate yet
Question mark regresses compile time by 6.5–7.5%.
2023-07-27 19:02:11 -07:00
David Tolnay 0676673ca5 Release 1.0.177 2023-07-27 10:51:22 -07:00
David Tolnay 7a4335d664 Merge pull request #2536 from jplatte/jplatte/error-span
Update error span for attribute / data kind mismatches
2023-07-27 10:50:26 -07:00
Jonas Platte 31a0e73489 Update error span for attribute / data kind mismatches 2023-07-27 10:47:45 +02:00
David Tolnay 74fe70855f Ignore return_self_not_must_use pedantic clippy lint
warning: missing `#[must_use]` attribute on a method returning `Self`
       --> serde_derive_internals/src/attr.rs:204:5
        |
    204 | /     pub fn or(self, other_rules: Self) -> Self {
    205 | |         Self {
    206 | |             serialize: self.serialize.or(other_rules.serialize),
    207 | |             deserialize: self.deserialize.or(other_rules.deserialize),
    208 | |         }
    209 | |     }
        | |_____^
        |
        = help: consider adding the `#[must_use]` attribute to the method or directly to the `Self` type
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#return_self_not_must_use
        = note: `-W clippy::return-self-not-must-use` implied by `-W clippy::pedantic`

    warning: missing `#[must_use]` attribute on a method returning `Self`
       --> serde_derive_internals/src/case.rs:112:5
        |
    112 | /     pub fn or(self, rule_b: Self) -> Self {
    113 | |         match self {
    114 | |             None => rule_b,
    115 | |             _ => self,
    116 | |         }
    117 | |     }
        | |_____^
        |
        = help: consider adding the `#[must_use]` attribute to the method or directly to the `Self` type
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#return_self_not_must_use
2023-07-27 00:51:57 -07:00
David Tolnay e74925bc43 Merge pull request #1695 from jplatte/rename_all_fields
Add #[serde(rename_all_fields = "foo")] attribute
2023-07-27 00:45:45 -07:00
Jonas Platte 56be1c203e Pass RenameRule, RenameAllRules by value 2023-07-27 09:19:42 +02:00
Jonas Platte 2f9bf4d3eb Add #[serde(rename_all_fields = "foo")] attribute 2023-07-27 09:19:42 +02:00
David Tolnay ad94aed753 Merge pull request #2535 from dtolnay/baretrait
Restore bare_trait_objects lint within serde_derive code
2023-07-26 14:26:02 -07:00
David Tolnay 30db83fc44 Restore bare_trait_objects lint within serde_derive code 2023-07-26 14:18:25 -07:00
David Tolnay b0f7b00e1f Resolve manual_string_new pedantic clippy lint
warning: empty String is being created manually
        --> test_suite/tests/test_annotations.rs:2280:29
         |
    2280 |     let data = Data::C { t: "".to_string() };
         |                             ^^^^^^^^^^^^^^ help: consider using: `String::new()`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_string_new
         = note: `-W clippy::manual-string-new` implied by `-W clippy::pedantic`
2023-07-26 13:46:28 -07:00
David Tolnay 7255e192d8 Delete unused statement from PR 2266 test 2023-07-26 13:45:49 -07:00
David Tolnay 2102e1aa42 Format PR 2266 tests with rustfmt 2023-07-26 13:41:56 -07:00
David Tolnay 85d5c1fd38 Release 1.0.176 2023-07-26 13:37:44 -07:00
David Tolnay b789286bc3 Merge pull request #2266 from flisky/master
fix: don't check skipped variant with internal tag
2023-07-26 13:36:33 -07:00
David Tolnay a6a8a334f7 Add CI on macOS for the non-precompiled codepath 2023-07-26 01:03:20 -07:00
David Tolnay 78a11a27b6 Skip an extra trip through filesystem on the critical path 2023-07-26 00:58:34 -07:00
David Tolnay d2d7bad04a Improve error message on missing serde_derive exe 2023-07-26 00:52:09 -07:00
David Tolnay b978854258 Eliminate parse_macro_input conflict in precompiled mode 2023-07-26 00:44:55 -07:00
David Tolnay 0fb672a1ef Eliminate #[macro_use] from serde_derive 2023-07-25 23:23:24 -07:00
David Tolnay dd9913675d Ungroup imports 2023-07-25 23:06:34 -07:00
David Tolnay 11677ad926 Merge pull request #2533 from dtolnay/test
Move serde_test out to serde-rs/test
2023-07-25 22:52:36 -07:00
David Tolnay 25a53f10db Move serde_test out to serde-rs/test 2023-07-25 22:48:39 -07:00
David Tolnay 26e2ef001c Delete deprecated AsciiExt extension trait import
This has been superseded by inherent methods since Rust 1.26.
2023-07-25 21:07:33 -07:00
David Tolnay 30f79b3b2e Eliminate 2015-style module system imports from serde_derive 2023-07-25 20:56:19 -07:00
David Tolnay 89f84c2915 Merge pull request #2532 from dtolnay/macrouse
Delete unused serde_derive #[macro_use]
2023-07-25 18:55:14 -07:00
David Tolnay 6882285be0 Move extern crate proc_macro into each possible lib.rs
This makes it slightly more convenient to use the following as a
Reindeer fixup for those that prefer to build from source:

    extra_mapped_srcs = { "src/lib_from_source.rs" = "src/lib.rs" }

    [platform_fixups.'cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))']
    extra_deps = [":proc-macro2", ":quote", ":syn"]

as opposed to checking in a whole new file containing the `extern crate
proc_macro` + `include!("lib_from_source.rs")`.
2023-07-25 18:54:41 -07:00
David Tolnay 3260bc5896 Delete unused serde_derive #[macro_use]
In old versions of rustc (1.15 through 1.29) it would cause a warning if
this #[macro_use] was not present.

    warning: proc macro crates and `#[no_link]` crates have no effect without `#[macro_use]`
       --> serde/src/lib.rs:340:1
        |
    340 | extern crate serde_derive;
        | ^^^^^^^^^^^^^^^^^^^^^^^^^^

These days serde_derive requires a newer compiler than that, so the
bogus warning would never occur.
2023-07-25 18:47:52 -07:00
David Tolnay 6140b6f527 Release 1.0.175 2023-07-23 20:07:32 -07:00
David Tolnay 4cabc9f293 Merge pull request #2527 from ankane/license-files
Include license files in serde_derive crate
2023-07-23 20:04:18 -07:00
Andrew Kane aa7c6345a4 Include license files in serde_derive crate 2023-07-23 19:54:57 -07:00
Mingun f709fc05b0 Do not run the code when results are not used 2023-07-23 15:23:39 +05:00
Mingun 089aae1292 Eliminate even more allocations 2023-07-23 15:23:39 +05:00
Mingun 855acaf112 Eliminate additional allocations for flattening aliases 2023-07-23 15:23:38 +05:00
Mingun 7ca7720262 Slightly reduced number of allocations 2023-07-23 15:23:37 +05:00
Mingun 78fea3aa4a Show possible aliases in the expected message
Fixes tests
2023-07-23 15:23:37 +05:00
Mingun 1efb8b6a53 Add tests for aliases
failures (2):
    field_identifier::unknown
    variant_identifier::unknown
2023-07-23 15:23:36 +05:00
Mingun bc1960b106 Add tests for unknown field / variant 2023-07-23 15:23:33 +05:00
Mingun 967023b755 Group field_identifier and variant_identifier tests in sub-modules
(review this commit with "ignore whitespace changes" option on)
2023-07-23 15:21:22 +05:00
Mingun bb51e68f16 Keep aliases sorted 2023-07-23 15:21:21 +05:00
David Tolnay 27414c90a8 Merge pull request #2522 from serde-rs/leak
Leak all memory allocated during macro expansion
2023-07-22 08:37:52 -07:00
David Tolnay 50e2f4b213 Leak all memory allocated during macro expansion 2023-07-22 08:32:18 -07:00
David Tolnay 22be673beb Release 1.0.174 2023-07-20 22:20:37 -07:00
David Tolnay 166c89fabf Opt in to generate-link-to-definition when building on docs.rs 2023-07-20 22:19:03 -07:00
David Tolnay 6e0b13eedb Release 1.0.173 2023-07-19 16:34:13 -07:00
David Tolnay 7e8f978ca9 Handle $crate special case 2023-07-19 16:32:59 -07:00
David Tolnay 6c0e838a7c Always consider empty output to be unsuccessful 2023-07-19 16:13:02 -07:00
David Tolnay d3da41927a Enable full expression parsing for precompiled serde_derive 2023-07-19 16:01:18 -07:00
David Tolnay 425a4b7a74 Check precompiled subprocess exit status 2023-07-19 16:00:37 -07:00
David Tolnay 63c65ef742 Release 1.0.172 2023-07-19 14:13:56 -07:00
David Tolnay e838b0bd81 Release 1.0.172-alpha.0 2023-07-19 14:00:50 -07:00
David Tolnay 041e99c78a Implement fallback to compiling serde_derive from source 2023-07-19 13:53:49 -07:00
David Tolnay 07dcc4f7fe Remove unneeded 'include' Cargo.toml entries 2023-07-19 13:44:55 -07:00
David Tolnay b88052d875 Rearrange precompiled directory 2023-07-19 13:24:36 -07:00
David Tolnay a28292764c Publish precompiled deserialize_in_place 2023-07-19 12:29:30 -07:00
David Tolnay 2027088741 Support precompiled deserialize_in_place 2023-07-19 12:27:37 -07:00
David Tolnay e2d8589976 Publish span-related fixes 2023-07-19 09:41:11 -07:00
David Tolnay c8a9f99d14 Preserve Group span better on compilers without Span::join 2023-07-19 09:39:11 -07:00
David Tolnay 645d04012d Fix off by one span counter, 0 is used for call_site() 2023-07-19 09:26:56 -07:00
David Tolnay 100ddada2f Suppress dead_code on fields only accessed by deserialize_in_place
error: field `option` is never read
       --> test_suite/tests/test_gen.rs:666:9
        |
    665 |     struct ImplicitlyBorrowedOption<'a> {
        |            ------------------------ field in this struct
    666 |         option: std::option::Option<&'a str>,
        |         ^^^^^^
        |
    note: the lint level is defined here
       --> test_suite/tests/test_gen.rs:5:9
        |
    5   | #![deny(warnings)]
        |         ^^^^^^^^
        = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]`

    error: fields `ty` and `id` are never read
       --> test_suite/tests/test_gen.rs:696:9
        |
    695 |     struct RelObject<'a> {
        |            --------- fields in this struct
    696 |         ty: &'a str,
        |         ^^
    697 |         id: String,
        |         ^^

    error: field `field` is never read
       --> test_suite/tests/test_gen.rs:740:17
        |
    739 |             struct MacroRules<'a> {
        |                    ---------- field in this struct
    740 |                 field: $field,
        |                 ^^^^^
    ...
    745 |     deriving!(&'a str);
        |     ------------------ in this macro invocation
        |
        = note: this error originates in the macro `deriving` (in Nightly builds, run with -Z macro-backtrace for more info)

    error: field `f` is never read
       --> test_suite/tests/test_gen.rs:756:9
        |
    754 |     struct BorrowLifetimeInsideMacro<'a> {
        |            ------------------------- field in this struct
    755 |         #[serde(borrow = "'a")]
    756 |         f: mac!(Cow<'a, str>),
        |         ^

    warning: fields `question` and `answer` are never read
        --> test_suite/tests/test_annotations.rs:2969:9
         |
    2968 |     struct Struct {
         |            ------ fields in this struct
    2969 |         question: String,
         |         ^^^^^^^^
    2970 |         answer: u32,
         |         ^^^^^^
         |
         = note: `#[warn(dead_code)]` on by default
2023-07-19 09:12:35 -07:00
David Tolnay 2ef1cd4b35 Import macros exclusively through serde_derive in test suite
This makes it easier to execute tests against the precompiled serde_derive.
2023-07-19 09:08:05 -07:00
David Tolnay be9c3fd69d Publish raw string fix 2023-07-18 18:42:54 -07:00
David Tolnay ef522e1d16 Add a reminder about trim-paths 2023-07-18 18:39:40 -07:00
David Tolnay 1ddb6c2fdb Fix handling of raw idents in proc-macro2 shim 2023-07-18 18:29:24 -07:00
David Tolnay eb3f2329af Merge pull request #2514 from dtolnay/precompiled
Add experiment to produce precompiled builds of serde_derive
2023-07-18 13:48:17 -07:00
David Tolnay 9e8f14816b Add experiment to produce precompiled builds of serde_derive 2023-07-18 13:37:36 -07:00
Mingun 4e5e55bf1c Remove custom implementations of SeqDeserializer and MapDeserializer for enums
Those deserializers are used to deserialize tuple or struct variants from Content
which is used by internally tagged enums and by flatten

FlatMapDeserializer is reached in the following tests:
    flatten::enum_::externally_tagged::newtype
    flatten::enum_::externally_tagged::struct_from_map
    flatten::enum_::externally_tagged::struct_from_seq
    flatten::enum_::externally_tagged::tuple

ContentDeserializer is reached in the following tests:
    test_enum_in_internally_tagged_enum
    test_internally_tagged_struct_variant_containing_unit_variant
2023-07-11 22:01:48 +05:00
Mingun 4513a9e6a7 Move test_flatten_enum_newtype into new group of flatten tests - flatten::enum_::externally_tagged::newtype 2023-07-11 22:00:38 +05:00
Mingun 4f922e4e5b Implement serialization of flattened tuple variants of externally tagged enums
The Container struct

  struct Container {
    #[serde(flatten)]
    enum_field: Enum,
  }
  enum Enum {
    Tuple(u32, u32),
  }

now can be serialized to JSON as

  { "enum_field": [1, 2] }

Deserialization already works

Fixes (1):
    flatten::enum_::externally_tagged::tuple
2023-07-11 22:00:10 +05:00
Mingun 993966600e Implement tests for crate::private::de::content::VariantDeserializer
failures (1):
    flatten::enum_::externally_tagged::tuple
2023-07-11 21:59:22 +05:00
Mingun 5b96cf1bde Use traditional order for enum variants (Unit, Newtype, Tuple, Struct) and names for tag and content fields 2023-07-11 21:58:43 +05:00
Mingun f3d50e5209 Use FromIterator to fill HashMap 2023-07-11 21:58:14 +05:00
Mingun ab21d4d017 Merge assert_de_tokens and assert_ser_tokens into assert_tokens 2023-07-11 21:57:49 +05:00
Mingun f7c5d93e6a Pull up types from function into module, unify style 2023-07-11 21:57:22 +05:00
Mingun 52a7d40e6e Rename test types so their names reflects, what's tested 2023-07-11 21:56:53 +05:00
Mingun 348bc6b257 Move flatten enum tests to a dedicated module
(review with "ignore whitespace" option on and editor that shows line moves,
for example, TortoiseGitMerge)
2023-07-11 21:56:22 +05:00
David Tolnay 03da66c805 Release 1.0.171 2023-07-09 18:05:02 -07:00
David Tolnay f75426f47e Inline visitor_expr of unit struct deserialize impl 2023-07-09 18:03:58 -07:00
David Tolnay 662fc3861c Add test of const-generic unit struct where-clause edge case
Closes #2501.
2023-07-09 18:01:43 -07:00
David Tolnay 28c10020b9 Merge pull request #2500 from Baptistemontan/derive_generic_unit_struct
Allow `[derive(serde::Deserialize)]` for generic unit structs
2023-07-09 17:52:12 -07:00
Baptiste de Montangon 89c8d85de9 allow Deserialize derive to handle generic unit structs 2023-07-10 01:31:40 +02:00
David Tolnay 6502838f27 Release 1.0.170 2023-07-09 11:17:30 -07:00
David Tolnay c93a0f335a Merge pull request #2499 from dtolnay/strsuffix
Reject suffixed string literals inside serde attrs
2023-07-09 11:16:31 -07:00
David Tolnay 8264e002a7 Reject suffixed string literals inside serde attrs 2023-07-09 11:13:24 -07:00
David Tolnay 117ef22142 Add ui test with suffixed string literals in attribute 2023-07-09 11:09:38 -07:00
David Tolnay 3fb5e71c33 Release 1.0.169 2023-07-08 21:09:08 -07:00
David Tolnay 296db177e2 Pull in syn fix for issue 2414 2023-07-08 21:08:22 -07:00
David Tolnay e4a4389177 Delete excessive tests of adjacently tagged non-string keys 2023-07-08 18:23:31 -07:00
David Tolnay 7aa0453c3b Merge pull request 2475 from Baptistemontan/master 2023-07-08 18:07:16 -07:00
David Tolnay 09b78b24e9 Release 1.0.168 2023-07-08 17:52:06 -07:00
David Tolnay a622b8a74a Merge pull request #2495 from dtolnay/cautious
Allow larger preallocated capacity for smaller elements
2023-07-08 17:51:16 -07:00
David Tolnay 399ef081ec Allow larger preallocated capacity for smaller elements 2023-07-08 17:43:01 -07:00
David Tolnay 3686277e14 Merge pull request #2436 from Mingun/flatten-ignored-any
Allow to flatten `IgnoredAny`
2023-07-06 16:35:10 -07:00
David Tolnay 807bd20a64 Release 1.0.167 2023-07-06 16:25:48 -07:00
David Tolnay ed9a140348 Merge pull request #2444 from Mingun/dedup
Simplify code for generation of struct deserializers
2023-07-06 16:25:15 -07:00
David Tolnay 2de7c2bea2 Resolve redundant_static_lifetimes clippy lint from PR 2471
error: constants have by default a `'static` lifetime
        --> serde/src/de/impls.rs:2467:24
         |
    2467 |     pub const FIELDS: &'static [&'static str] = &["end"];
         |                       -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
         = note: `-D clippy::redundant-static-lifetimes` implied by `-D clippy::all`

    error: constants have by default a `'static` lifetime
        --> serde/src/de/impls.rs:2467:34
         |
    2467 |     pub const FIELDS: &'static [&'static str] = &["end"];
         |                                 -^^^^^^^---- help: consider removing `'static`: `&str`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes

    error: constants have by default a `'static` lifetime
        --> serde/src/de/impls.rs:2605:24
         |
    2605 |     pub const FIELDS: &'static [&'static str] = &["start"];
         |                       -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes

    error: constants have by default a `'static` lifetime
        --> serde/src/de/impls.rs:2605:34
         |
    2605 |     pub const FIELDS: &'static [&'static str] = &["start"];
         |                                 -^^^^^^^---- help: consider removing `'static`: `&str`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
2023-07-06 16:18:39 -07:00
David Tolnay e6a4a3772e Delete unuseful RangeFull impls 2023-07-06 16:10:21 -07:00
David Tolnay 0fca04e1a6 Merge pull request 2471 from tbu-/pr_more_ranges 2023-07-06 16:09:13 -07:00
David Tolnay 92bfc8d3af Merge pull request #2290 from Mingun/enum-tests-and-cleanup
Remove unused `impl` and unnecessary struct-wrapper around tuple
2023-07-06 16:02:27 -07:00
David Tolnay fa0312ac45 More formatting of doc tests and example code 2023-07-06 15:56:47 -07:00
David Tolnay 1920b694aa Declare required automod dev-dependency
1.0.0 does not work with workspaces.

    error: No such file or directory (os error 2)
     --> test_suite/tests/regression.rs:2:5
      |
    2 |     automod::dir!("tests/regression");
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: this error originates in the macro `automod::dir` (in Nightly builds, run with -Z macro-backtrace for more info)
2023-07-06 15:50:32 -07:00
David Tolnay 3bfd41d624 Format doctests using rustfmt's format_code_in_doc_comments
cargo fmt -- --config format_code_in_doc_comments=true
2023-07-06 15:44:32 -07:00
David Tolnay 290449f19b Fix doc tests to work whether or not serde derive feature is used 2023-07-06 15:40:12 -07:00
David Tolnay 3a1f387e69 Merge pull request #2493 from dtolnay/docedition
Update documentation example code to 2021 edition
2023-07-06 15:40:03 -07:00
David Tolnay 541603ac94 Fix doc tests for 2021 edition 2023-07-06 15:36:17 -07:00
David Tolnay 0666fbfa20 Update documentation example code to 2021 edition 2023-07-06 15:17:05 -07:00
David Tolnay ea071ae1d4 Add CI job using minimal-versions 2023-07-05 10:01:33 -07:00
David Tolnay 992a01bad2 Sort dependency features in Cargo.toml 2023-07-04 12:12:31 -07:00
David Tolnay d640b5624f Add no-alloc category to the macro crate also 2023-07-03 14:05:32 -07:00
David Tolnay 48479e4bae Release 1.0.166 2023-07-03 11:33:19 -07:00
David Tolnay dfaf48bc09 Add no-std::no-alloc category 2023-07-03 11:32:55 -07:00
David Tolnay dcbc3e0162 Release 1.0.165 2023-07-03 04:21:59 -07:00
David Tolnay 0289d31724 Fix -Zminimal-versions build 2023-07-03 04:21:14 -07:00
David Tolnay 015e39776f No need for single-element vec for chaining one element 2023-07-02 21:11:09 -07:00
David Tolnay 6a9a21f178 Resolve useless_conversion clippy lint in test
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
       --> test_suite/tests/test_de.rs:202:12
        |
    202 |     .chain(vec![Token::MapEnd].into_iter())
        |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![Token::MapEnd]`
        |
    note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
       --> /home/david/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:522:12
        |
    522 |         U: IntoIterator<Item = Self::Item>,
        |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
        = note: `-D clippy::useless-conversion` implied by `-D clippy::all`
2023-07-02 21:10:06 -07:00
David Tolnay 81ac54b20d Resolve redundant_closure_call clippy lint
error: try not to call a closure in the expression where it is declared
        --> serde/src/de/impls.rs:1590:76
         |
    1590 |                       <(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
         |                                                                              ^^^^^^^^^^^^^^
    ...
    1620 | / parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new(
    1621 | |     ip, port, 0, 0
    1622 | | ));
         | |__- in this macro invocation
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
         = note: `-D clippy::redundant-closure-call` implied by `-D clippy::all`
         = note: this error originates in the macro `parse_socket_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
2023-07-02 21:08:50 -07:00
David Tolnay 6b4e75520a Resolve explicit_iter_loop pedantic clippy lint
error: it is more concise to loop over references to containers instead of using explicit iteration methods
        --> serde/src/private/de.rs:2761:22
         |
    2761 |         for entry in self.0.iter_mut() {
         |                      ^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut *self.0`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
         = note: `-D clippy::explicit-iter-loop` implied by `-D clippy::pedantic`

    error: it is more concise to loop over references to containers instead of using explicit iteration methods
       --> serde_derive/src/internals/check.rs:202:20
        |
    202 |     for variant in variants.iter() {
        |                    ^^^^^^^^^^^^^^^ help: to write this more concisely, try: `variants`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
        = note: `-D clippy::explicit-iter-loop` implied by `-D clippy::pedantic`

    error: it is more concise to loop over references to containers instead of using explicit iteration methods
       --> serde_derive/src/bound.rs:262:28
        |
    262 |             for variant in variants.iter() {
        |                            ^^^^^^^^^^^^^^^ help: to write this more concisely, try: `variants`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
2023-07-02 21:08:44 -07:00
David Tolnay b053b4f492 Touch up early return in Enum checks 2023-07-02 21:08:44 -07:00
Mingun 4cf1fec575 Replace several linked variables with enumeration for tuples 2023-06-26 20:55:52 +05:00
Mingun ee7d77defa Replace several linked variables with enumeration for structs 2023-06-26 20:55:52 +05:00
Mingun d0dfc4577e Replace enum with boolean parameter 2023-06-26 20:55:52 +05:00
Mingun bbbd1d24c9 Move deserialize_generated_identifier out from if because the call is same in both arms 2023-06-26 20:55:51 +05:00
Mingun fb3a9e0d7c Simplify check for missing fields 2023-06-26 20:55:51 +05:00
Mingun 5ffebeb6ef Actually, field_names_idents can be calculated using the same code in both cases
When !cattrs.has_flatten() all fields is !field.attrs.flatten()

Co-authored-by: Oliver Schneider <oli-obk@users.noreply.github.com>
2023-06-26 20:55:51 +05:00
Mingun 75db73066b Inline deserialize_struct_as_map_visitor and deserialize_struct_as_struct_visitor 2023-06-26 20:55:51 +05:00
Mingun 2796833c82 Pull up call to deserialize_map because it's identical 2023-06-26 20:55:50 +05:00
Mingun 95730dc7f7 Reorder variables to match order in final quote! 2023-06-26 20:55:50 +05:00
Mingun 795261919f Generate visit_seq only when needed 2023-06-26 20:55:50 +05:00
Mingun 3783a30ae7 Remove TaggedContent, replace it by a tuple
That type does not give any benefits so we can avoid that hidden public but no-API struct
2023-06-24 20:49:49 +05:00
Mingun b61ec84886 Remove implementation of DeserializeSeed for TaggedContentVisitor
It is not used anywhere
2023-06-24 20:48:09 +05:00
Mingun 780a461d92 Generate one deserializer rather than in each arm 2023-06-24 20:48:08 +05:00
David Tolnay c0ba323166 Support a manual trigger on CI workflow 2023-06-23 22:50:52 -07:00
David Tolnay 20a48c9580 Remove .clippy.toml in favor of respecting rust-version from Cargo.toml 2023-06-15 18:31:38 -07:00
David Tolnay 09938803af Resolve redundant_static_lifetimes clippy lint 2023-06-15 18:31:38 -07:00
David Tolnay 6d0b43a220 Resolve redundant_field_names clippy lint 2023-06-15 18:31:38 -07:00
Baptiste de Montangon c604bdbfe4 Clarified tokens that represents tag and content for integer identifiers 2023-06-12 17:17:15 +02:00
Baptiste de Montangon 9fef892f6d fixed difference in error message for adjacently tagged enums 2023-06-12 16:02:19 +02:00
Baptiste de Montangon b1c7db47b8 Adjancently tagged enum field is determined with visit_identifier instead of only visit_str 2023-06-12 15:27:28 +02:00
Tobias Bucher e76e87a430 Add Serialize/Deserialize impls for Range{From,Full,To}
CC #796
CC #1466
CC #1713
2023-06-09 15:11:44 +02:00
David Tolnay 8a4dfa7231 Merge pull request #2466 from Mingun/fix-de-count-of-field
Fix incorrect count of fields passed to tuple deserialization methods
2023-06-07 22:13:08 -07:00
David Tolnay 107018c628 Release 1.0.164 2023-06-07 22:05:07 -07:00
David Tolnay a398237930 Point out serde(untagged) variants which are out of order
Previously if someone wrote an enum containing:

- `A` (untagged)
- `B` (tagged)
- `C` (tagged)
- `D` (untagged)
- `E` (tagged)
- `F` (untagged)

serde_derive would produce errors referring to B and E only, saying
you're supposed to put untagged variants at the end. The choice of B and
E for this error doesn't make a lot of sense because in order to resolve
the issue, the user must either:

- move A and D down

or:

- move B, C, and E up.

This commit changes the error to appear on A and D instead.
2023-06-07 21:49:30 -07:00
David Tolnay b63c65d7f5 Merge pull request #2470 from dtolnay/contentref
Reuse a single ContentRefDeserializer throughout untagged enum deserialization
2023-06-07 21:38:49 -07:00
David Tolnay f60324e883 Reuse a single ContentRefDeserializer throughout untagged enum deserialization 2023-06-07 21:33:14 -07:00
David Tolnay 361c23a09a Simplify enumerate().find(...) -> Iterator::position 2023-06-07 21:23:31 -07:00
David Tolnay 43b23c7ea0 Format PR 2403 with rustfmt 2023-06-07 21:18:30 -07:00
David Tolnay 6081497506 Resolve semicolon_if_nothing_returned pedantic clippy lint
error: consider adding a `;` to the last statement for consistent formatting
       --> serde_derive/src/internals/ast.rs:161:13
        |
    161 |             seen_untagged = variant.attrs.untagged()
        |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `seen_untagged = variant.attrs.untagged();`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
        = note: `-D clippy::semicolon-if-nothing-returned` implied by `-D clippy::pedantic`

    error: consider adding a `;` to the last statement for consistent formatting
       --> serde_derive/src/internals/ast.rs:159:17
        |
    159 | ...   cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum")
        |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
2023-06-07 21:17:24 -07:00
David Ewert 48e5753e76 Allowed Enum variants to be individually marked as untagged (#2403) 2023-06-07 20:58:59 -07:00
David Tolnay bbba632ab3 Revert "Ui tests with compile_error resolved at call site"
This reverts commit e77db40b8d.
2023-06-07 20:50:51 -07:00
David Tolnay e77db40b8d Ui tests with compile_error resolved at call site 2023-06-07 20:02:04 -07:00
Mingun 2c1f62d4b4 Fix incorrect count of fields passed to tuple deserialization methods
This count should mean the number of fields expected in the serialized form,
so if some fields are skipped, they shouldn't be counted

Methods affected:
- Deserializer::deserialize_tuple
- Deserializer::deserialize_tuple_struct
- VariantAccess::tuple_variant
2023-05-28 23:17:05 +05:00
David Tolnay 1aebdc2760 Release serde_derive_internals 0.28.0 2023-05-25 08:20:10 -07:00
David Tolnay 705e58be8c Merge pull request #2464 from serde-rs/combine
Use syn::Error's combine() API instead of Vec<syn::Error>
2023-05-25 08:19:16 -07:00
David Tolnay 7c2c12aa43 Use syn::Error's combine() API instead of Vec<syn::Error> 2023-05-25 08:10:14 -07:00
David Tolnay a0f850f15b Show error details during miri setup in CI
Without this, if it fails, the only information printed is useless:

    Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)...
    fatal error: failed to build sysroot; run `cargo miri setup` to see the error details
2023-05-23 08:29:47 -07:00
David Tolnay fccb9499bc Release 1.0.163 2023-05-10 00:47:53 -07:00
David Tolnay a139ab2572 Adjust PR 2446 with less overgeneralized name 2023-05-10 00:45:52 -07:00
David Tolnay 1d910a484c Format with rustfmt 1.5.2-nightly 2023-05-10 00:40:39 -07:00
David Tolnay ee9166ec97 Revise comments on the FlatMapDeserializer entry taker 2023-05-10 00:39:10 -07:00
David Tolnay b5a9eff32e Resolve while_let_on_iterator clippy lint
warning: this loop could be written as a `for` loop
        --> serde/src/private/de.rs:2905:9
         |
    2905 |         while let Some(item) = self.iter.next() {
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for item in self.iter.by_ref()`
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
         = note: `#[warn(clippy::while_let_on_iterator)]` on by default
2023-05-10 00:23:38 -07:00
David Tolnay 9441a29663 Merge pull request #2446 from Mingun/dedup2
Eliminate some duplicated code
2023-05-10 00:13:44 -07:00
Mingun ab6588ef74 Extract duplicated code into a function 2023-05-08 10:39:30 +05:00
Mingun 1d11f03449 Extract logic of taking flattened fields into a function 2023-05-08 10:39:27 +05:00
Mingun e11d01fe1d Remove constructors for FlatMapAccess and FlatStructAccess
They are used only in one place each, so for simplifying understanding it is better to inline them
2023-05-08 09:40:06 +05:00
Mingun a901f50850 FlatMapAccess and FlatStructAccess does not need to be public 2023-05-08 09:37:15 +05:00
Mingun c399e9c368 Remove FlatInternallyTaggedAccess because it is the same as FlatMapAccess 2023-05-08 09:25:18 +05:00
David Tolnay 25381be0c9 Merge pull request #2442 from taiki-e/derive-build-script
Remove build script from serde_derive
2023-05-05 14:35:09 -07:00
Taiki Endo ef2a7c753f Remove build script from serde_derive
The current serde_derive's MSRV is 1.56, and both underscore consts and
ptr::addr_of! are always available.
2023-05-06 05:34:38 +09:00
David Tolnay 99f165b45a Release 1.0.162 2023-05-04 18:46:55 -07:00
David Tolnay 2fb5560746 Attempt to generate just one copy of TagContentOtherFieldVisitor's field matching 2023-05-04 18:42:21 -07:00
David Tolnay bd653ab30c Format PR 2377 with rustfmt 2023-05-04 18:42:21 -07:00
David Tolnay b5d68aedaa Merge pull request #2377 from mfro/master
Allow bytes for adjacently tagged enums
2023-05-04 18:39:57 -07:00
David Tolnay 624879c4c6 Merge pull request #2441 from dtolnay/test
Reimplement tests that touched serde_test internal API
2023-05-04 17:42:07 -07:00
David Tolnay bd9e9abf35 Reimplement tests that touched serde_test internal API 2023-05-04 17:38:58 -07:00
David Tolnay 3e4a23cbd0 Release 1.0.161 2023-05-04 16:45:18 -07:00
David Tolnay 6326ceec3f Don't panic in serde_test on running out of tokens 2023-05-04 16:38:20 -07:00
David Tolnay 8f4d37c7ec Convert serde_test's assert_next_token from macro to function 2023-05-04 16:34:14 -07:00
David Tolnay 1b8290b318 Convert serde_test's unexpected from macro to function 2023-05-04 16:30:34 -07:00
David Tolnay 48193fbccd Merge pull request #2435 from Mingun/hitchhiker-guide
Don't panic in serde_test
2023-05-04 16:27:35 -07:00
Mingun 51799dd654 Allow to flatten IgnoredAny to ignore any additional data
Although any additional fields in struct by default are ignored, sometimes
this can be useful, if you use generic structures, for example
2023-04-30 01:59:55 +05:00
Mingun 732ac49321 Implement PartialEq for IgnoredAny so it can be used in tests 2023-04-30 01:58:07 +05:00
Mingun ac8ea72d88 Don't panic in serde_test
Panics lead to reporting errors in tests inside of serde_test internals,
returning errors moves the report location to the corresponding assert_tokens
expression
2023-04-30 00:06:51 +05:00
David Tolnay f583401284 Merge pull request #2433 from Mingun/rm-gitattributes
Remove unused after .gitattributes
2023-04-26 12:04:04 -07:00
Mingun 2d88228b7d Remove unused after a649190a4d .gitattributes 2023-04-26 23:54:09 +05:00
David Tolnay 0c6a2bbf79 Release 1.0.160 2023-04-10 22:15:49 -07:00
David Tolnay a80d830f27 Merge pull request #2426 from compiler-errors/dont-doc-private
Make derived serializer/deserializer internals `doc(hidden)`
2023-04-10 22:12:35 -07:00
Michael Goulet 5f3fd9994e Make serializer/deserializer internals doc(hidden) 2023-04-10 21:41:50 -07:00
David Tolnay d6de911855 Release 1.0.159 2023-03-27 22:05:58 -07:00
David Tolnay 04af32230e Merge pull request #2422 from dtolnay/emptyattr
Accept empty #[serde()] attribute
2023-03-27 22:05:18 -07:00
David Tolnay 4cb8d079f8 Accept empty #[serde()] attribute 2023-03-27 22:00:46 -07:00
David Tolnay 6ab55a1e52 Add regression test for issue 2415
Currently fails:

    error: unexpected end of input, unexpected token in nested attribute, expected ident
     --> test_suite/tests/regression/issue2415.rs:4:9
      |
    4 | #[serde()]
      |         ^
2023-03-27 22:00:01 -07:00
David Tolnay acfd19cb46 Release serde_derive_internals 0.27.0 2023-03-20 04:32:49 -07:00
David Tolnay e3058105f0 Release 1.0.158 2023-03-20 04:24:27 -07:00
David Tolnay dc200a6450 Reformat comments of non-public serde_derive internals
Fixes these being treated as "tests" by `cargo test` in serde_derive:

    running 3 tests
    test src/internals/check.rs - internals::check::check_remote_generic (line 23) ... FAILED
    test src/internals/check.rs - internals::check::check_remote_generic (line 29) ... FAILED
    test src/lib.rs - (line 3) ... ok

    failures:

    ---- src/internals/check.rs - internals::check::check_remote_generic (line 23) stdout ----
    error: unknown start of token: \u{2026}
     --> src/internals/check.rs:25:20
      |
    4 | struct Generic<T> {…}
      |                    ^

    error: cannot find attribute `serde` in this scope
     --> src/internals/check.rs:24:3
      |
    3 | #[serde(remote = "Generic")]
      |   ^^^^^
      |
      = note: `serde` is in scope, but it is a crate, not an attribute

    error[E0392]: parameter `T` is never used
     --> src/internals/check.rs:25:16
      |
    4 | struct Generic<T> {…}
      |                ^ unused parameter
      |
      = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
      = help: if you intended `T` to be a const parameter, use `const T: usize` instead

    ---- src/internals/check.rs - internals::check::check_remote_generic (line 29) stdout ----
    error: unknown start of token: \u{2026}
     --> src/internals/check.rs:31:21
      |
    4 | struct ConcreteDef {…}
      |                     ^

    error: cannot find attribute `serde` in this scope
     --> src/internals/check.rs:30:3
      |
    3 | #[serde(remote = "Generic<T>")]
      |   ^^^^^
      |
      = note: `serde` is in scope, but it is a crate, not an attribute
2023-03-20 04:23:46 -07:00
David Tolnay 2c0999a0b9 Merge pull request #2410 from serde-rs/attrvalue
Check for None-delimited group in attribute value
2023-03-20 04:23:33 -07:00
David Tolnay dd460f82a1 Check for None-delimited group in attribute value 2023-03-20 04:16:52 -07:00
David Tolnay c3d637f397 Add regression test for issue 2409 2023-03-20 03:59:43 -07:00
Max Froehlich a803ec1c1f Allow bytes for adjantly tagged enums 2023-02-18 12:49:23 -08:00
Yin Jifeng 983347484e fix: don't check skipped variant with internal tag 2022-08-23 20:19:34 +08:00
109 changed files with 3376 additions and 5465 deletions
-1
View File
@@ -1 +0,0 @@
test_suite/tests/expand/*.expanded.rs linguist-generated
+14 -23
View File
@@ -3,6 +3,7 @@ name: CI
on:
push:
pull_request:
workflow_dispatch:
schedule: [cron: "40 1 * * *"]
permissions:
@@ -45,8 +46,6 @@ jobs:
toolchain: ${{matrix.rust}}
- run: cd serde && cargo build --features rc
- run: cd serde && cargo build --no-default-features
- run: cd serde_test && cargo build
- run: cd serde_test && cargo test --features serde/derive,serde/rc
nightly:
name: Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}}
@@ -63,6 +62,7 @@ jobs:
- run: cd serde && cargo build --no-default-features
- run: cd serde && cargo build --no-default-features --features alloc
- run: cd serde && cargo build --no-default-features --features rc,alloc
- run: cd serde && cargo build --no-default-features --features unstable
- run: cd serde && cargo test --features derive,rc,unstable
- run: cd test_suite/no_std && cargo build
if: matrix.os != 'windows'
@@ -77,7 +77,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [1.19.0, 1.20.0, 1.21.0, 1.25.0, 1.26.0, 1.34.0]
rust: [1.31.0, 1.34.0]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
@@ -87,25 +87,6 @@ jobs:
- run: cd serde && cargo build --features rc
- run: cd serde && cargo build --no-default-features
- run: cd serde && cargo build
- run: cd serde_test && cargo build
more:
name: Rust ${{matrix.rust}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
rust: [1.27.0, 1.28.0]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
# Work around failing to parse manifest because editions are unstable.
- run: sed -i /test_suite/d Cargo.toml
- run: cd serde && cargo build --no-default-features
- run: cd serde && cargo build
derive:
name: Rust 1.56.0
@@ -127,6 +108,16 @@ jobs:
- uses: dtolnay/rust-toolchain@1.36.0
- run: cd serde && cargo build --no-default-features --features alloc
minimal:
name: Minimal versions
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
- run: cargo generate-lockfile -Z minimal-versions
- run: cargo check --locked --workspace
clippy:
name: Clippy
runs-on: ubuntu-latest
@@ -138,7 +129,6 @@ jobs:
- run: cd serde && cargo clippy --features rc,unstable -- -Dclippy::all -Dclippy::pedantic
- run: cd serde_derive && cargo clippy -- -Dclippy::all -Dclippy::pedantic
- run: cd serde_derive_internals && cargo clippy -- -Dclippy::all -Dclippy::pedantic
- run: cd serde_test && cargo clippy -- -Dclippy::all -Dclippy::pedantic
- run: cd test_suite && cargo clippy --tests --features unstable -- -Dclippy::all -Dclippy::pedantic
- run: cd test_suite/no_std && cargo clippy -- -Dclippy::all -Dclippy::pedantic
@@ -149,6 +139,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@miri
- run: cargo miri setup
- run: cd serde && cargo miri test --features derive,rc,unstable
env:
MIRIFLAGS: -Zmiri-strict-provenance
+3 -1
View File
@@ -3,6 +3,8 @@ members = [
"serde",
"serde_derive",
"serde_derive_internals",
"serde_test",
"test_suite",
]
[patch.crates-io]
serde = { path = "serde" }
+4 -4
View File
@@ -1,12 +1,12 @@
# Serde &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.19+]][Rust 1.19] [![serde_derive: rustc 1.56+]][Rust 1.56]
# Serde &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.31+]][Rust 1.31] [![serde_derive: rustc 1.56+]][Rust 1.56]
[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/serde/ci.yml?branch=master
[actions]: https://github.com/serde-rs/serde/actions?query=branch%3Amaster
[Latest Version]: https://img.shields.io/crates/v/serde.svg
[crates.io]: https://crates.io/crates/serde
[serde: rustc 1.19+]: https://img.shields.io/badge/serde-rustc_1.19+-lightgray.svg
[serde: rustc 1.31+]: https://img.shields.io/badge/serde-rustc_1.31+-lightgray.svg
[serde_derive: rustc 1.56+]: https://img.shields.io/badge/serde_derive-rustc_1.56+-lightgray.svg
[Rust 1.19]: https://blog.rust-lang.org/2017/07/20/Rust-1.19.html
[Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
[Rust 1.56]: https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
@@ -48,7 +48,7 @@ serde_json = "1.0"
<p></p>
```rust
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
+1 -1
View File
@@ -16,7 +16,7 @@ You may be looking for:
## Serde in action
```rust
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
-1
View File
@@ -1 +0,0 @@
msrv = "1.13.0"
+7 -6
View File
@@ -1,24 +1,24 @@
[package]
name = "serde"
version = "1.0.157" # remember to update html_root_url and serde_derive dependency
version = "1.0.185" # remember to update html_root_url and serde_derive dependency
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
build = "build.rs"
categories = ["encoding", "no-std"]
categories = ["encoding", "no-std", "no-std::no-alloc"]
description = "A generic serialization/deserialization framework"
documentation = "https://docs.rs/serde"
edition = "2018"
homepage = "https://serde.rs"
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
keywords = ["serde", "serialization", "no_std"]
license = "MIT OR Apache-2.0"
readme = "crates-io.md"
repository = "https://github.com/serde-rs/serde"
rust-version = "1.19"
rust-version = "1.31"
[dependencies]
serde_derive = { version = "=1.0.157", optional = true, path = "../serde_derive" }
serde_derive = { version = "=1.0.185", optional = true, path = "../serde_derive" }
[dev-dependencies]
serde_derive = { version = "1.0", path = "../serde_derive" }
serde_derive = { version = "1", path = "../serde_derive" }
[lib]
doc-scrape-examples = false
@@ -29,6 +29,7 @@ features = ["derive", "rc"]
[package.metadata.docs.rs]
features = ["derive"]
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]
### FEATURES #################################################################
+7 -62
View File
@@ -16,68 +16,6 @@ fn main() {
let target = env::var("TARGET").unwrap();
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
// std::collections::Bound was stabilized in Rust 1.17
// but it was moved to core::ops later in Rust 1.26:
// https://doc.rust-lang.org/core/ops/enum.Bound.html
if minor < 26 {
println!("cargo:rustc-cfg=no_ops_bound");
if minor < 17 {
println!("cargo:rustc-cfg=no_collections_bound");
}
}
// core::cmp::Reverse stabilized in Rust 1.19:
// https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html
if minor < 19 {
println!("cargo:rustc-cfg=no_core_reverse");
}
// CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
// https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
// https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
if minor < 20 {
println!("cargo:rustc-cfg=no_de_boxed_c_str");
println!("cargo:rustc-cfg=no_de_boxed_path");
}
// From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
// https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
// https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
if minor < 21 {
println!("cargo:rustc-cfg=no_de_rc_dst");
}
// Duration available in core since Rust 1.25:
// https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
if minor < 25 {
println!("cargo:rustc-cfg=no_core_duration");
}
// 128-bit integers stabilized in Rust 1.26:
// https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
//
// Disabled on Emscripten targets before Rust 1.40 since
// Emscripten did not support 128-bit integers until Rust 1.40
// (https://github.com/rust-lang/rust/pull/65251)
if minor < 26 || emscripten && minor < 40 {
println!("cargo:rustc-cfg=no_integer128");
}
// Inclusive ranges methods stabilized in Rust 1.27:
// https://github.com/rust-lang/rust/pull/50758
// Also Iterator::try_for_each:
// https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
if minor < 27 {
println!("cargo:rustc-cfg=no_range_inclusive");
println!("cargo:rustc-cfg=no_iterator_try_fold");
}
// Non-zero integers stabilized in Rust 1.28:
// https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
if minor < 28 {
println!("cargo:rustc-cfg=no_num_nonzero");
}
// TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
// stabilized in Rust 1.34:
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
@@ -89,6 +27,13 @@ fn main() {
println!("cargo:rustc-cfg=no_relaxed_trait_bounds");
}
// Disabled on Emscripten targets before Rust 1.40 since
// Emscripten did not support 128-bit integers until Rust 1.40
// (https://github.com/rust-lang/rust/pull/65251)
if emscripten && minor < 40 {
println!("cargo:rustc-cfg=no_integer128");
}
// Current minimum supported version of serde_derive crate is Rust 1.56.
if minor < 56 {
println!("cargo:rustc-cfg=no_serde_derive");
+2 -2
View File
@@ -1,5 +1,5 @@
use lib::fmt::{self, Write};
use lib::str;
use crate::lib::fmt::{self, Write};
use crate::lib::str;
pub(super) struct Buf<'a> {
bytes: &'a mut [u8],
+9 -10
View File
@@ -1,6 +1,6 @@
use lib::*;
use crate::lib::*;
use de::{
use crate::de::{
Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor,
};
@@ -10,13 +10,12 @@ use de::{
/// any type, except that it does not store any information about the data that
/// gets deserialized.
///
/// ```edition2018
/// use std::fmt;
/// use std::marker::PhantomData;
///
/// ```edition2021
/// use serde::de::{
/// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor,
/// };
/// use std::fmt;
/// use std::marker::PhantomData;
///
/// /// A seed that can be used to deserialize only the `n`th element of a sequence
/// /// while efficiently discarding elements of any type before or after index `n`.
@@ -108,7 +107,7 @@ use de::{
/// # Ok(())
/// # }
/// ```
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct IgnoredAny;
impl<'de> Visitor<'de> for IgnoredAny {
@@ -198,7 +197,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
where
A: SeqAccess<'de>,
{
while let Some(IgnoredAny) = try!(seq.next_element()) {
while let Some(IgnoredAny) = tri!(seq.next_element()) {
// Gobble
}
Ok(IgnoredAny)
@@ -209,7 +208,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
where
A: MapAccess<'de>,
{
while let Some((IgnoredAny, IgnoredAny)) = try!(map.next_entry()) {
while let Some((IgnoredAny, IgnoredAny)) = tri!(map.next_entry()) {
// Gobble
}
Ok(IgnoredAny)
@@ -228,7 +227,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
where
A: EnumAccess<'de>,
{
try!(data.variant::<IgnoredAny>()).1.newtype_variant()
tri!(data.variant::<IgnoredAny>()).1.newtype_variant()
}
}
+363 -131
View File
@@ -1,16 +1,14 @@
use lib::*;
use crate::lib::*;
use de::{
Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess, Visitor,
use crate::de::{
Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, Unexpected, VariantAccess,
Visitor,
};
#[cfg(any(feature = "std", feature = "alloc", not(no_core_duration)))]
use de::MapAccess;
use seed::InPlaceSeed;
use crate::seed::InPlaceSeed;
#[cfg(any(feature = "std", feature = "alloc"))]
use __private::size_hint;
use crate::de::size_hint;
////////////////////////////////////////////////////////////////////////////////
@@ -84,7 +82,7 @@ macro_rules! impl_deserialize_num {
($primitive:ident, $nonzero:ident $(cfg($($cfg:tt)*))*, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => {
impl_deserialize_num!($primitive, $deserialize $($method!($($val : $visit)*);)*);
#[cfg(all(not(no_num_nonzero), $($($cfg)*)*))]
$(#[cfg($($cfg)*)])*
impl<'de> Deserialize<'de> for num::$nonzero {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@@ -681,10 +679,10 @@ impl<'de> Visitor<'de> for CStringVisitor {
where
A: SeqAccess<'de>,
{
let len = size_hint::cautious(seq.size_hint());
let mut values = Vec::with_capacity(len);
let capacity = size_hint::cautious::<u8>(seq.size_hint());
let mut values = Vec::<u8>::with_capacity(capacity);
while let Some(value) = try!(seq.next_element()) {
while let Some(value) = tri!(seq.next_element()) {
values.push(value);
}
@@ -747,13 +745,9 @@ macro_rules! forwarded_impl {
}
}
#[cfg(all(
any(feature = "std", all(not(no_core_cstr), feature = "alloc")),
not(no_de_boxed_c_str)
))]
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
#[cfg(not(no_core_reverse))]
forwarded_impl!((T), Reverse<T>, Reverse);
////////////////////////////////////////////////////////////////////////////////
@@ -901,7 +895,7 @@ macro_rules! seq_impl {
{
let mut values = $with_capacity;
while let Some(value) = try!($access.next_element()) {
while let Some(value) = tri!($access.next_element()) {
$insert(&mut values, value);
}
@@ -936,10 +930,10 @@ macro_rules! seq_impl {
A: SeqAccess<'de>,
{
$clear(&mut self.0);
$reserve(&mut self.0, size_hint::cautious($access.size_hint()));
$reserve(&mut self.0, size_hint::cautious::<T>($access.size_hint()));
// FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
while let Some(value) = try!($access.next_element()) {
while let Some(value) = tri!($access.next_element()) {
$insert(&mut self.0, value);
}
@@ -962,7 +956,7 @@ seq_impl!(
BinaryHeap<T: Ord>,
seq,
BinaryHeap::clear,
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
BinaryHeap::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
BinaryHeap::reserve,
BinaryHeap::push
);
@@ -992,16 +986,17 @@ seq_impl!(
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
seq,
HashSet::clear,
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
HashSet::with_capacity_and_hasher(size_hint::cautious::<T>(seq.size_hint()), S::default()),
HashSet::reserve,
HashSet::insert);
HashSet::insert
);
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
VecDeque<T>,
seq,
VecDeque::clear,
VecDeque::with_capacity(size_hint::cautious(seq.size_hint())),
VecDeque::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
VecDeque::reserve,
VecDeque::push_back
);
@@ -1035,9 +1030,10 @@ where
where
A: SeqAccess<'de>,
{
let mut values = Vec::with_capacity(size_hint::cautious(seq.size_hint()));
let capacity = size_hint::cautious::<T>(seq.size_hint());
let mut values = Vec::<T>::with_capacity(capacity);
while let Some(value) = try!(seq.next_element()) {
while let Some(value) = tri!(seq.next_element()) {
values.push(value);
}
@@ -1071,7 +1067,7 @@ where
where
A: SeqAccess<'de>,
{
let hint = size_hint::cautious(seq.size_hint());
let hint = size_hint::cautious::<T>(seq.size_hint());
if let Some(additional) = hint.checked_sub(self.0.len()) {
self.0.reserve(additional);
}
@@ -1079,7 +1075,7 @@ where
for i in 0..self.0.len() {
let next = {
let next_place = InPlaceSeed(&mut self.0[i]);
try!(seq.next_element_seed(next_place))
tri!(seq.next_element_seed(next_place))
};
if next.is_none() {
self.0.truncate(i);
@@ -1087,7 +1083,7 @@ where
}
}
while let Some(value) = try!(seq.next_element()) {
while let Some(value) = tri!(seq.next_element()) {
self.0.push(value);
}
@@ -1159,7 +1155,7 @@ macro_rules! array_impls {
A: SeqAccess<'de>,
{
Ok([$(
match try!(seq.next_element()) {
match tri!(seq.next_element()) {
Some(val) => val,
None => return Err(Error::invalid_length($n, &self)),
}
@@ -1184,7 +1180,7 @@ macro_rules! array_impls {
{
let mut fail_idx = None;
for (idx, dest) in self.0[..].iter_mut().enumerate() {
if try!(seq.next_element_seed(InPlaceSeed(dest))).is_none() {
if tri!(seq.next_element_seed(InPlaceSeed(dest))).is_none() {
fail_idx = Some(idx);
break;
}
@@ -1282,7 +1278,7 @@ macro_rules! tuple_impls {
A: SeqAccess<'de>,
{
$(
let $name = match try!(seq.next_element()) {
let $name = match tri!(seq.next_element()) {
Some(value) => value,
None => return Err(Error::invalid_length($n, &self)),
};
@@ -1316,7 +1312,7 @@ macro_rules! tuple_impls {
A: SeqAccess<'de>,
{
$(
if try!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() {
if tri!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() {
return Err(Error::invalid_length($n, &self));
}
)+
@@ -1393,7 +1389,7 @@ macro_rules! map_impl {
{
let mut values = $with_capacity;
while let Some((key, value)) = try!($access.next_entry()) {
while let Some((key, value)) = tri!($access.next_entry()) {
values.insert(key, value);
}
@@ -1409,16 +1405,14 @@ macro_rules! map_impl {
}
#[cfg(any(feature = "std", feature = "alloc"))]
map_impl!(
BTreeMap<K: Ord, V>,
map,
BTreeMap::new());
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
#[cfg(feature = "std")]
map_impl!(
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
map,
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default()));
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default())
);
////////////////////////////////////////////////////////////////////////////////
@@ -1451,7 +1445,7 @@ macro_rules! variant_identifier {
$($variant),*
}
static $variants_name: &'static [&'static str] = &[$(stringify!($variant)),*];
static $variants_name: &[&str] = &[$(stringify!($variant)),*];
impl<'de> Deserialize<'de> for $name_kind {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -1541,7 +1535,7 @@ macro_rules! deserialize_enum {
where
A: EnumAccess<'de>,
{
match try!(data.variant()) {
match tri!(data.variant()) {
$(
($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant),
)*
@@ -1561,7 +1555,7 @@ impl<'de> Deserialize<'de> for net::IpAddr {
if deserializer.is_human_readable() {
deserializer.deserialize_str(FromStrVisitor::new("IP address"))
} else {
use lib::net::IpAddr;
use crate::lib::net::IpAddr;
deserialize_enum! {
IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
"`V4` or `V6`",
@@ -1588,7 +1582,7 @@ macro_rules! parse_socket_impl {
if deserializer.is_human_readable() {
deserializer.deserialize_str(FromStrVisitor::new($expecting))
} else {
<(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
<(_, u16)>::deserialize(deserializer).map($new)
}
}
}
@@ -1604,7 +1598,7 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
if deserializer.is_human_readable() {
deserializer.deserialize_str(FromStrVisitor::new("socket address"))
} else {
use lib::net::SocketAddr;
use crate::lib::net::SocketAddr;
deserialize_enum! {
SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1)
"`V4` or `V6`",
@@ -1615,12 +1609,10 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
}
#[cfg(feature = "std")]
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, net::SocketAddrV4::new);
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, |(ip, port)| net::SocketAddrV4::new(ip, port));
#[cfg(feature = "std")]
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new(
ip, port, 0, 0
));
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0));
////////////////////////////////////////////////////////////////////////////////
@@ -1716,7 +1708,7 @@ impl<'de> Deserialize<'de> for PathBuf {
}
}
#[cfg(all(feature = "std", not(no_de_boxed_path)))]
#[cfg(feature = "std")]
forwarded_impl!((), Box<Path>, PathBuf::into_boxed_path);
////////////////////////////////////////////////////////////////////////////////
@@ -1750,7 +1742,7 @@ impl<'de> Visitor<'de> for OsStringVisitor {
{
use std::os::unix::ffi::OsStringExt;
match try!(data.variant()) {
match tri!(data.variant()) {
(OsStringKind::Unix, v) => v.newtype_variant().map(OsString::from_vec),
(OsStringKind::Windows, _) => Err(Error::custom(
"cannot deserialize Windows OS string on Unix",
@@ -1765,7 +1757,7 @@ impl<'de> Visitor<'de> for OsStringVisitor {
{
use std::os::windows::ffi::OsStringExt;
match try!(data.variant()) {
match tri!(data.variant()) {
(OsStringKind::Windows, v) => v
.newtype_variant::<Vec<u16>>()
.map(|vec| OsString::from_wide(&vec)),
@@ -1797,29 +1789,8 @@ forwarded_impl!((T), Box<[T]>, Vec::into_boxed_slice);
#[cfg(any(feature = "std", feature = "alloc"))]
forwarded_impl!((), Box<str>, String::into_boxed_str);
#[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
forwarded_impl! {
/// This impl requires the [`"rc"`] Cargo feature of Serde.
///
/// Deserializing a data structure containing `Arc` will not attempt to
/// deduplicate `Arc` references to the same data. Every deserialized `Arc`
/// will end up with a strong count of 1.
///
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
(T), Arc<T>, Arc::new
}
#[cfg(all(no_de_rc_dst, feature = "rc", any(feature = "std", feature = "alloc")))]
forwarded_impl! {
/// This impl requires the [`"rc"`] Cargo feature of Serde.
///
/// Deserializing a data structure containing `Rc` will not attempt to
/// deduplicate `Rc` references to the same data. Every deserialized `Rc`
/// will end up with a strong count of 1.
///
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
(T), Rc<T>, Rc::new
}
#[cfg(all(feature = "std", any(unix, windows)))]
forwarded_impl!((), Box<OsStr>, OsString::into_boxed_os_str);
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
@@ -1851,7 +1822,7 @@ where
where
D: Deserializer<'de>,
{
try!(Option::<T>::deserialize(deserializer));
tri!(Option::<T>::deserialize(deserializer));
Ok(RcWeak::new())
}
}
@@ -1869,18 +1840,14 @@ where
where
D: Deserializer<'de>,
{
try!(Option::<T>::deserialize(deserializer));
tri!(Option::<T>::deserialize(deserializer));
Ok(ArcWeak::new())
}
}
////////////////////////////////////////////////////////////////////////////////
#[cfg(all(
not(no_de_rc_dst),
feature = "rc",
any(feature = "std", feature = "alloc")
))]
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
macro_rules! box_forwarded_impl {
(
$(#[doc = $doc:tt])*
@@ -1901,11 +1868,7 @@ macro_rules! box_forwarded_impl {
};
}
#[cfg(all(
not(no_de_rc_dst),
feature = "rc",
any(feature = "std", feature = "alloc")
))]
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
box_forwarded_impl! {
/// This impl requires the [`"rc"`] Cargo feature of Serde.
///
@@ -1917,11 +1880,7 @@ box_forwarded_impl! {
Rc
}
#[cfg(all(
not(no_de_rc_dst),
feature = "rc",
any(feature = "std", feature = "alloc")
))]
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
box_forwarded_impl! {
/// This impl requires the [`"rc"`] Cargo feature of Serde.
///
@@ -1965,7 +1924,6 @@ forwarded_impl!((T), RwLock<T>, RwLock::new);
// secs: u64,
// nanos: u32,
// }
#[cfg(any(feature = "std", not(no_core_duration)))]
impl<'de> Deserialize<'de> for Duration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@@ -2013,7 +1971,7 @@ impl<'de> Deserialize<'de> for Duration {
b"secs" => Ok(Field::Secs),
b"nanos" => Ok(Field::Nanos),
_ => {
let value = ::__private::from_utf8_lossy(value);
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
}
}
@@ -2048,19 +2006,19 @@ impl<'de> Deserialize<'de> for Duration {
where
A: SeqAccess<'de>,
{
let secs: u64 = match try!(seq.next_element()) {
let secs: u64 = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
let nanos: u32 = match try!(seq.next_element()) {
let nanos: u32 = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(1, &self));
}
};
try!(check_overflow(secs, nanos));
tri!(check_overflow(secs, nanos));
Ok(Duration::new(secs, nanos))
}
@@ -2070,19 +2028,19 @@ impl<'de> Deserialize<'de> for Duration {
{
let mut secs: Option<u64> = None;
let mut nanos: Option<u32> = None;
while let Some(key) = try!(map.next_key()) {
while let Some(key) = tri!(map.next_key()) {
match key {
Field::Secs => {
if secs.is_some() {
return Err(<A::Error as Error>::duplicate_field("secs"));
}
secs = Some(try!(map.next_value()));
secs = Some(tri!(map.next_value()));
}
Field::Nanos => {
if nanos.is_some() {
return Err(<A::Error as Error>::duplicate_field("nanos"));
}
nanos = Some(try!(map.next_value()));
nanos = Some(tri!(map.next_value()));
}
}
}
@@ -2094,12 +2052,12 @@ impl<'de> Deserialize<'de> for Duration {
Some(nanos) => nanos,
None => return Err(<A::Error as Error>::missing_field("nanos")),
};
try!(check_overflow(secs, nanos));
tri!(check_overflow(secs, nanos));
Ok(Duration::new(secs, nanos))
}
}
const FIELDS: &'static [&'static str] = &["secs", "nanos"];
const FIELDS: &[&str] = &["secs", "nanos"];
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
}
}
@@ -2186,19 +2144,19 @@ impl<'de> Deserialize<'de> for SystemTime {
where
A: SeqAccess<'de>,
{
let secs: u64 = match try!(seq.next_element()) {
let secs: u64 = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
let nanos: u32 = match try!(seq.next_element()) {
let nanos: u32 = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(1, &self));
}
};
try!(check_overflow(secs, nanos));
tri!(check_overflow(secs, nanos));
Ok(Duration::new(secs, nanos))
}
@@ -2208,7 +2166,7 @@ impl<'de> Deserialize<'de> for SystemTime {
{
let mut secs: Option<u64> = None;
let mut nanos: Option<u32> = None;
while let Some(key) = try!(map.next_key()) {
while let Some(key) = tri!(map.next_key()) {
match key {
Field::Secs => {
if secs.is_some() {
@@ -2216,7 +2174,7 @@ impl<'de> Deserialize<'de> for SystemTime {
"secs_since_epoch",
));
}
secs = Some(try!(map.next_value()));
secs = Some(tri!(map.next_value()));
}
Field::Nanos => {
if nanos.is_some() {
@@ -2224,7 +2182,7 @@ impl<'de> Deserialize<'de> for SystemTime {
"nanos_since_epoch",
));
}
nanos = Some(try!(map.next_value()));
nanos = Some(tri!(map.next_value()));
}
}
}
@@ -2236,13 +2194,13 @@ impl<'de> Deserialize<'de> for SystemTime {
Some(nanos) => nanos,
None => return Err(<A::Error as Error>::missing_field("nanos_since_epoch")),
};
try!(check_overflow(secs, nanos));
tri!(check_overflow(secs, nanos));
Ok(Duration::new(secs, nanos))
}
}
const FIELDS: &'static [&'static str] = &["secs_since_epoch", "nanos_since_epoch"];
let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"];
let duration = tri!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
#[cfg(not(no_systemtime_checked_add))]
let ret = UNIX_EPOCH
.checked_add(duration)
@@ -2259,9 +2217,9 @@ impl<'de> Deserialize<'de> for SystemTime {
//
// #[derive(Deserialize)]
// #[serde(deny_unknown_fields)]
// struct Range {
// start: u64,
// end: u32,
// struct Range<Idx> {
// start: Idx,
// end: Idx,
// }
impl<'de, Idx> Deserialize<'de> for Range<Idx>
where
@@ -2271,7 +2229,7 @@ where
where
D: Deserializer<'de>,
{
let (start, end) = try!(deserializer.deserialize_struct(
let (start, end) = tri!(deserializer.deserialize_struct(
"Range",
range::FIELDS,
range::RangeVisitor {
@@ -2283,7 +2241,6 @@ where
}
}
#[cfg(not(no_range_inclusive))]
impl<'de, Idx> Deserialize<'de> for RangeInclusive<Idx>
where
Idx: Deserialize<'de>,
@@ -2292,7 +2249,7 @@ where
where
D: Deserializer<'de>,
{
let (start, end) = try!(deserializer.deserialize_struct(
let (start, end) = tri!(deserializer.deserialize_struct(
"RangeInclusive",
range::FIELDS,
range::RangeVisitor {
@@ -2305,11 +2262,11 @@ where
}
mod range {
use lib::*;
use crate::lib::*;
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
pub const FIELDS: &'static [&'static str] = &["start", "end"];
pub const FIELDS: &[&str] = &["start", "end"];
// If this were outside of the serde crate, it would just use:
//
@@ -2353,7 +2310,7 @@ mod range {
b"start" => Ok(Field::Start),
b"end" => Ok(Field::End),
_ => {
let value = ::__private::from_utf8_lossy(value);
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
}
}
@@ -2383,13 +2340,13 @@ mod range {
where
A: SeqAccess<'de>,
{
let start: Idx = match try!(seq.next_element()) {
let start: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
let end: Idx = match try!(seq.next_element()) {
let end: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(1, &self));
@@ -2404,19 +2361,19 @@ mod range {
{
let mut start: Option<Idx> = None;
let mut end: Option<Idx> = None;
while let Some(key) = try!(map.next_key()) {
while let Some(key) = tri!(map.next_key()) {
match key {
Field::Start => {
if start.is_some() {
return Err(<A::Error as Error>::duplicate_field("start"));
}
start = Some(try!(map.next_value()));
start = Some(tri!(map.next_value()));
}
Field::End => {
if end.is_some() {
return Err(<A::Error as Error>::duplicate_field("end"));
}
end = Some(try!(map.next_value()));
end = Some(tri!(map.next_value()));
}
}
}
@@ -2435,7 +2392,282 @@ mod range {
////////////////////////////////////////////////////////////////////////////////
#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
// Similar to:
//
// #[derive(Deserialize)]
// #[serde(deny_unknown_fields)]
// struct RangeFrom<Idx> {
// start: Idx,
// }
impl<'de, Idx> Deserialize<'de> for RangeFrom<Idx>
where
Idx: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let start = tri!(deserializer.deserialize_struct(
"RangeFrom",
range_from::FIELDS,
range_from::RangeFromVisitor {
expecting: "struct RangeFrom",
phantom: PhantomData,
},
));
Ok(start..)
}
}
mod range_from {
use crate::lib::*;
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
pub const FIELDS: &[&str] = &["end"];
// If this were outside of the serde crate, it would just use:
//
// #[derive(Deserialize)]
// #[serde(field_identifier, rename_all = "lowercase")]
enum Field {
End,
}
impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct FieldVisitor;
impl<'de> Visitor<'de> for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`end`")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
match value {
"end" => Ok(Field::End),
_ => Err(Error::unknown_field(value, FIELDS)),
}
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
match value {
b"end" => Ok(Field::End),
_ => {
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
}
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
pub struct RangeFromVisitor<Idx> {
pub expecting: &'static str,
pub phantom: PhantomData<Idx>,
}
impl<'de, Idx> Visitor<'de> for RangeFromVisitor<Idx>
where
Idx: Deserialize<'de>,
{
type Value = Idx;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(self.expecting)
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let end: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
Ok(end)
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut end: Option<Idx> = None;
while let Some(key) = tri!(map.next_key()) {
match key {
Field::End => {
if end.is_some() {
return Err(<A::Error as Error>::duplicate_field("end"));
}
end = Some(tri!(map.next_value()));
}
}
}
let end = match end {
Some(end) => end,
None => return Err(<A::Error as Error>::missing_field("end")),
};
Ok(end)
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Similar to:
//
// #[derive(Deserialize)]
// #[serde(deny_unknown_fields)]
// struct RangeTo<Idx> {
// start: Idx,
// }
impl<'de, Idx> Deserialize<'de> for RangeTo<Idx>
where
Idx: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let end = tri!(deserializer.deserialize_struct(
"RangeTo",
range_to::FIELDS,
range_to::RangeToVisitor {
expecting: "struct RangeTo",
phantom: PhantomData,
},
));
Ok(..end)
}
}
mod range_to {
use crate::lib::*;
use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
pub const FIELDS: &[&str] = &["start"];
// If this were outside of the serde crate, it would just use:
//
// #[derive(Deserialize)]
// #[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Start,
}
impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct FieldVisitor;
impl<'de> Visitor<'de> for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`start`")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
match value {
"start" => Ok(Field::Start),
_ => Err(Error::unknown_field(value, FIELDS)),
}
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
match value {
b"start" => Ok(Field::Start),
_ => {
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
}
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
pub struct RangeToVisitor<Idx> {
pub expecting: &'static str,
pub phantom: PhantomData<Idx>,
}
impl<'de, Idx> Visitor<'de> for RangeToVisitor<Idx>
where
Idx: Deserialize<'de>,
{
type Value = Idx;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(self.expecting)
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let start: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
Ok(start)
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut start: Option<Idx> = None;
while let Some(key) = tri!(map.next_key()) {
match key {
Field::Start => {
if start.is_some() {
return Err(<A::Error as Error>::duplicate_field("start"));
}
start = Some(tri!(map.next_value()));
}
}
}
let start = match start {
Some(start) => start,
None => return Err(<A::Error as Error>::missing_field("start")),
};
Ok(start)
}
}
}
////////////////////////////////////////////////////////////////////////////////
impl<'de, T> Deserialize<'de> for Bound<T>
where
T: Deserialize<'de>,
@@ -2527,7 +2759,7 @@ where
where
A: EnumAccess<'de>,
{
match try!(data.variant()) {
match tri!(data.variant()) {
(Field::Unbounded, v) => v.unit_variant().map(|()| Bound::Unbounded),
(Field::Included, v) => v.newtype_variant().map(Bound::Included),
(Field::Excluded, v) => v.newtype_variant().map(Bound::Excluded),
@@ -2535,7 +2767,7 @@ where
}
}
const VARIANTS: &'static [&'static str] = &["Unbounded", "Included", "Excluded"];
const VARIANTS: &[&str] = &["Unbounded", "Included", "Excluded"];
deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData))
}
@@ -2636,14 +2868,14 @@ where
where
A: EnumAccess<'de>,
{
match try!(data.variant()) {
match tri!(data.variant()) {
(Field::Ok, v) => v.newtype_variant().map(Ok),
(Field::Err, v) => v.newtype_variant().map(Err),
}
}
}
const VARIANTS: &'static [&'static str] = &["Ok", "Err"];
const VARIANTS: &[&str] = &["Ok", "Err"];
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
}
@@ -2709,7 +2941,7 @@ struct FromStrVisitor<T> {
impl<T> FromStrVisitor<T> {
fn new(expecting: &'static str) -> Self {
FromStrVisitor {
expecting: expecting,
expecting,
ty: PhantomData,
}
}
+44 -44
View File
@@ -112,7 +112,7 @@
//! [derive section of the manual]: https://serde.rs/derive.html
//! [data formats]: https://serde.rs/#data-formats
use lib::*;
use crate::lib::*;
////////////////////////////////////////////////////////////////////////////////
@@ -122,16 +122,20 @@ pub mod value;
mod format;
mod ignored_any;
mod impls;
pub(crate) mod size_hint;
mod utf8;
pub use self::ignored_any::IgnoredAny;
#[cfg(not(any(feature = "std", feature = "unstable")))]
#[doc(no_inline)]
pub use crate::std_error::Error as StdError;
#[cfg(all(feature = "unstable", not(feature = "std")))]
#[doc(no_inline)]
pub use core::error::Error as StdError;
#[cfg(feature = "std")]
#[doc(no_inline)]
pub use std::error::Error as StdError;
#[cfg(not(feature = "std"))]
#[doc(no_inline)]
pub use std_error::Error as StdError;
////////////////////////////////////////////////////////////////////////////////
@@ -162,7 +166,7 @@ macro_rules! declare_error_trait {
///
/// The message should not be capitalized and should not end with a period.
///
/// ```edition2018
/// ```edition2021
/// # use std::str::FromStr;
/// #
/// # struct IpAddr;
@@ -307,7 +311,7 @@ declare_error_trait!(Error: Sized + Debug + Display);
/// This is used as an argument to the `invalid_type`, `invalid_value`, and
/// `invalid_length` methods of the `Error` trait to build error messages.
///
/// ```edition2018
/// ```edition2021
/// # use std::fmt;
/// #
/// # use serde::de::{self, Unexpected, Visitor};
@@ -432,10 +436,9 @@ impl<'a> fmt::Display for Unexpected<'a> {
/// Within the context of a `Visitor` implementation, the `Visitor` itself
/// (`&self`) is an implementation of this trait.
///
/// ```edition2018
/// # use std::fmt;
/// #
/// ```edition2021
/// # use serde::de::{self, Unexpected, Visitor};
/// # use std::fmt;
/// #
/// # struct Example;
/// #
@@ -457,7 +460,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
///
/// Outside of a `Visitor`, `&"..."` can be used.
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{self, Unexpected};
/// #
/// # fn example<E>() -> Result<(), E>
@@ -465,7 +468,10 @@ impl<'a> fmt::Display for Unexpected<'a> {
/// # E: de::Error,
/// # {
/// # let v = true;
/// return Err(de::Error::invalid_type(Unexpected::Bool(v), &"a negative integer"));
/// return Err(de::Error::invalid_type(
/// Unexpected::Bool(v),
/// &"a negative integer",
/// ));
/// # }
/// ```
pub trait Expected {
@@ -564,7 +570,7 @@ pub trait Deserialize<'de>: Sized {
D: Deserializer<'de>,
{
// Default implementation just delegates to `deserialize` impl.
*place = try!(Deserialize::deserialize(deserializer));
*place = tri!(Deserialize::deserialize(deserializer));
Ok(())
}
}
@@ -577,7 +583,7 @@ pub trait Deserialize<'de>: Sized {
/// from the input string, but a `from_reader` function may only deserialize
/// owned data.
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{Deserialize, DeserializeOwned};
/// # use std::io::{Read, Result};
/// #
@@ -616,7 +622,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
///
/// The canonical API for stateless deserialization looks like this:
///
/// ```edition2018
/// ```edition2021
/// # use serde::Deserialize;
/// #
/// # enum Error {}
@@ -630,7 +636,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
/// Adjusting an API like this to support stateful deserialization is a matter
/// of accepting a seed as input:
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::DeserializeSeed;
/// #
/// # enum Error {}
@@ -663,12 +669,11 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
/// into it. This requires stateful deserialization using the `DeserializeSeed`
/// trait.
///
/// ```edition2018
/// ```edition2021
/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
/// use std::fmt;
/// use std::marker::PhantomData;
///
/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
///
/// // A DeserializeSeed implementation that uses stateful deserialization to
/// // append array elements onto the end of an existing vector. The preexisting
/// // state ("seed") in this case is the Vec<T>. The `deserialize` method of
@@ -709,7 +714,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
/// {
/// // Decrease the number of reallocations if there are many elements
/// if let Some(size_hint) = seq.size_hint() {
/// self.0.reserve(size_hint);
/// self.0.reserve(size_hint);
/// }
///
/// // Visit each element in the inner array and push it onto
@@ -1158,7 +1163,7 @@ pub trait Deserializer<'de>: Sized {
/// human-readable one and binary formats like Postcard will prefer the
/// compact one.
///
/// ```edition2018
/// ```edition2021
/// # use std::ops::Add;
/// # use std::str::FromStr;
/// #
@@ -1225,11 +1230,11 @@ pub trait Deserializer<'de>: Sized {
#[doc(hidden)]
fn __deserialize_content<V>(
self,
_: ::actually_private::T,
_: crate::actually_private::T,
visitor: V,
) -> Result<::private::de::Content<'de>, Self::Error>
) -> Result<crate::__private::de::Content<'de>, Self::Error>
where
V: Visitor<'de, Value = ::private::de::Content<'de>>,
V: Visitor<'de, Value = crate::__private::de::Content<'de>>,
{
self.deserialize_any(visitor)
}
@@ -1249,10 +1254,9 @@ pub trait Deserializer<'de>: Sized {
///
/// # Example
///
/// ```edition2018
/// # use std::fmt;
/// #
/// ```edition2021
/// # use serde::de::{self, Unexpected, Visitor};
/// # use std::fmt;
/// #
/// /// A visitor that deserializes a long string - a string containing at least
/// /// some minimum number of bytes.
@@ -1290,7 +1294,7 @@ pub trait Visitor<'de>: Sized {
/// "an integer between 0 and 64". The message should not be capitalized and
/// should not end with a period.
///
/// ```edition2018
/// ```edition2021
/// # use std::fmt;
/// #
/// # struct S {
@@ -1831,9 +1835,9 @@ pub trait MapAccess<'de> {
K: DeserializeSeed<'de>,
V: DeserializeSeed<'de>,
{
match try!(self.next_key_seed(kseed)) {
match tri!(self.next_key_seed(kseed)) {
Some(key) => {
let value = try!(self.next_value_seed(vseed));
let value = tri!(self.next_value_seed(vseed));
Ok(Some((key, value)))
}
None => Ok(None),
@@ -2035,7 +2039,7 @@ pub trait VariantAccess<'de>: Sized {
/// If the data contains a different type of variant, the following
/// `invalid_type` error should be constructed:
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
/// #
/// # struct X;
@@ -2075,7 +2079,7 @@ pub trait VariantAccess<'de>: Sized {
/// If the data contains a different type of variant, the following
/// `invalid_type` error should be constructed:
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
/// #
/// # struct X;
@@ -2131,7 +2135,7 @@ pub trait VariantAccess<'de>: Sized {
/// If the data contains a different type of variant, the following
/// `invalid_type` error should be constructed:
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
/// #
/// # struct X;
@@ -2148,11 +2152,7 @@ pub trait VariantAccess<'de>: Sized {
/// # T: DeserializeSeed<'de>,
/// # { unimplemented!() }
/// #
/// fn tuple_variant<V>(
/// self,
/// _len: usize,
/// _visitor: V,
/// ) -> Result<V::Value, Self::Error>
/// fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
/// where
/// V: Visitor<'de>,
/// {
@@ -2178,7 +2178,7 @@ pub trait VariantAccess<'de>: Sized {
/// If the data contains a different type of variant, the following
/// `invalid_type` error should be constructed:
///
/// ```edition2018
/// ```edition2021
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
/// #
/// # struct X;
@@ -2238,10 +2238,10 @@ pub trait VariantAccess<'de>: Sized {
///
/// # Example
///
/// ```edition2018
/// ```edition2021
/// use serde::de::{value, Deserialize, IntoDeserializer};
/// use serde_derive::Deserialize;
/// use std::str::FromStr;
/// use serde::Deserialize;
/// use serde::de::{value, IntoDeserializer};
///
/// #[derive(Deserialize)]
/// enum Setting {
@@ -2285,12 +2285,12 @@ impl Display for OneOf {
1 => write!(formatter, "`{}`", self.names[0]),
2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
_ => {
try!(write!(formatter, "one of "));
tri!(write!(formatter, "one of "));
for (i, alt) in self.names.iter().enumerate() {
if i > 0 {
try!(write!(formatter, ", "));
tri!(write!(formatter, ", "));
}
try!(write!(formatter, "`{}`", alt));
tri!(write!(formatter, "`{}`", alt));
}
Ok(())
}
+1 -1
View File
@@ -1,4 +1,4 @@
use de::{Deserialize, DeserializeSeed, Deserializer};
use crate::de::{Deserialize, DeserializeSeed, Deserializer};
/// A DeserializeSeed helper for implementing deserialize_in_place Visitors.
///
@@ -1,4 +1,4 @@
use lib::*;
use crate::lib::*;
pub fn from_bounds<I>(iter: &I) -> Option<usize>
where
@@ -8,9 +8,17 @@ where
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[inline]
pub fn cautious(hint: Option<usize>) -> usize {
cmp::min(hint.unwrap_or(0), 4096)
pub fn cautious<Element>(hint: Option<usize>) -> usize {
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
if mem::size_of::<Element>() == 0 {
0
} else {
cmp::min(
hint.unwrap_or(0),
MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
)
}
}
fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
+2 -2
View File
@@ -1,4 +1,4 @@
use lib::*;
use crate::lib::*;
const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO_B: u8 = 0b1100_0000;
@@ -31,7 +31,7 @@ pub fn encode(c: char) -> Encode {
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
0
};
Encode { buf: buf, pos: pos }
Encode { buf, pos }
}
pub struct Encode {
+34 -37
View File
@@ -1,10 +1,10 @@
//! Building blocks for deserializing basic values using the `IntoDeserializer`
//! trait.
//!
//! ```edition2018
//! ```edition2021
//! use serde::de::{value, Deserialize, IntoDeserializer};
//! use serde_derive::Deserialize;
//! use std::str::FromStr;
//! use serde::Deserialize;
//! use serde::de::{value, IntoDeserializer};
//!
//! #[derive(Deserialize)]
//! enum Setting {
@@ -21,12 +21,11 @@
//! }
//! ```
use lib::*;
use crate::lib::*;
use self::private::{First, Second};
use __private::size_hint;
use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
use ser;
use crate::de::{self, size_hint, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
use crate::ser;
////////////////////////////////////////////////////////////////////////////////
@@ -251,7 +250,7 @@ macro_rules! primitive_deserializer {
#[allow(missing_docs)]
pub fn new(value: $ty) -> Self {
$name {
value: value,
value,
marker: PhantomData,
}
}
@@ -330,7 +329,7 @@ impl<E> U32Deserializer<E> {
#[allow(missing_docs)]
pub fn new(value: u32) -> Self {
U32Deserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -419,7 +418,7 @@ impl<'a, E> StrDeserializer<'a, E> {
#[allow(missing_docs)]
pub fn new(value: &'a str) -> Self {
StrDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -498,7 +497,7 @@ impl<'de, E> BorrowedStrDeserializer<'de, E> {
/// Create a new borrowed deserializer from the given string.
pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
BorrowedStrDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -598,7 +597,7 @@ impl<E> StringDeserializer<E> {
#[allow(missing_docs)]
pub fn new(value: String) -> Self {
StringDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -701,7 +700,7 @@ impl<'a, E> CowStrDeserializer<'a, E> {
#[allow(missing_docs)]
pub fn new(value: Cow<'a, str>) -> Self {
CowStrDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -783,7 +782,7 @@ impl<'a, E> BytesDeserializer<'a, E> {
/// Create a new deserializer from the given bytes.
pub fn new(value: &'a [u8]) -> Self {
BytesDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -842,7 +841,7 @@ impl<'de, E> BorrowedBytesDeserializer<'de, E> {
/// Create a new borrowed deserializer from the given borrowed bytes.
pub fn new(value: &'de [u8]) -> Self {
BorrowedBytesDeserializer {
value: value,
value,
marker: PhantomData,
}
}
@@ -937,8 +936,8 @@ where
where
V: de::Visitor<'de>,
{
let v = try!(visitor.visit_seq(&mut self));
try!(self.end());
let v = tri!(visitor.visit_seq(&mut self));
tri!(self.end());
Ok(v)
}
@@ -1053,7 +1052,7 @@ pub struct SeqAccessDeserializer<A> {
impl<A> SeqAccessDeserializer<A> {
/// Construct a new `SeqAccessDeserializer<A>`.
pub fn new(seq: A) -> Self {
SeqAccessDeserializer { seq: seq }
SeqAccessDeserializer { seq }
}
}
@@ -1162,8 +1161,8 @@ where
where
V: de::Visitor<'de>,
{
let value = try!(visitor.visit_map(&mut self));
try!(self.end());
let value = tri!(visitor.visit_map(&mut self));
tri!(self.end());
Ok(value)
}
@@ -1171,8 +1170,8 @@ where
where
V: de::Visitor<'de>,
{
let value = try!(visitor.visit_seq(&mut self));
try!(self.end());
let value = tri!(visitor.visit_seq(&mut self));
tri!(self.end());
Ok(value)
}
@@ -1236,8 +1235,8 @@ where
{
match self.next_pair() {
Some((key, value)) => {
let key = try!(kseed.deserialize(key.into_deserializer()));
let value = try!(vseed.deserialize(value.into_deserializer()));
let key = tri!(kseed.deserialize(key.into_deserializer()));
let value = tri!(vseed.deserialize(value.into_deserializer()));
Ok(Some((key, value)))
}
None => Ok(None),
@@ -1341,7 +1340,7 @@ where
V: de::Visitor<'de>,
{
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
let pair = try!(visitor.visit_seq(&mut pair_visitor));
let pair = tri!(visitor.visit_seq(&mut pair_visitor));
if pair_visitor.1.is_none() {
Ok(pair)
} else {
@@ -1454,7 +1453,7 @@ pub struct MapAccessDeserializer<A> {
impl<A> MapAccessDeserializer<A> {
/// Construct a new `MapAccessDeserializer<A>`.
pub fn new(map: A) -> Self {
MapAccessDeserializer { map: map }
MapAccessDeserializer { map }
}
}
@@ -1501,7 +1500,7 @@ where
where
T: de::DeserializeSeed<'de>,
{
match try!(self.map.next_key_seed(seed)) {
match tri!(self.map.next_key_seed(seed)) {
Some(key) => Ok((key, private::map_as_enum(self.map))),
None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
}
@@ -1519,7 +1518,7 @@ pub struct EnumAccessDeserializer<A> {
impl<A> EnumAccessDeserializer<A> {
/// Construct a new `EnumAccessDeserializer<A>`.
pub fn new(access: A) -> Self {
EnumAccessDeserializer { access: access }
EnumAccessDeserializer { access }
}
}
@@ -1546,9 +1545,11 @@ where
////////////////////////////////////////////////////////////////////////////////
mod private {
use lib::*;
use crate::lib::*;
use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor};
use crate::de::{
self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor,
};
pub struct UnitOnly<E> {
marker: PhantomData<E>,
@@ -1613,7 +1614,7 @@ mod private {
}
pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> {
MapAsEnum { map: map }
MapAsEnum { map }
}
impl<'de, A> VariantAccess<'de> for MapAsEnum<A>
@@ -1637,10 +1638,7 @@ mod private {
where
V: Visitor<'de>,
{
self.map.next_value_seed(SeedTupleVariant {
len: len,
visitor: visitor,
})
self.map.next_value_seed(SeedTupleVariant { len, visitor })
}
fn struct_variant<V>(
@@ -1651,8 +1649,7 @@ mod private {
where
V: Visitor<'de>,
{
self.map
.next_value_seed(SeedStructVariant { visitor: visitor })
self.map.next_value_seed(SeedStructVariant { visitor })
}
}
+3 -3
View File
@@ -9,7 +9,7 @@
/// or do not target platforms that lack 128-bit integers, do not need to
/// bother with this macro and may assume support for 128-bit integers.
///
/// ```edition2018
/// ```edition2021
/// # use serde::__private::doc::Error;
/// #
/// # struct MySerializer;
@@ -50,7 +50,7 @@
/// When Serde is built with support for 128-bit integers, this macro expands
/// transparently into just the input tokens.
///
/// ```edition2018
/// ```edition2021
/// macro_rules! serde_if_integer128 {
/// ($($tt:tt)*) => {
/// $($tt)*
@@ -61,7 +61,7 @@
/// When built without support for 128-bit integers, this macro expands to
/// nothing.
///
/// ```edition2018
/// ```edition2021
/// macro_rules! serde_if_integer128 {
/// ($($tt:tt)*) => {};
/// }
+51 -73
View File
@@ -93,7 +93,7 @@
////////////////////////////////////////////////////////////////////////////////
// Serde types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/serde/1.0.157")]
#![doc(html_root_url = "https://docs.rs/serde/1.0.185")]
// Support using Serde without the standard library!
#![cfg_attr(not(feature = "std"), no_std)]
// Unstable functionality only if the user asks for it. For tracking and
@@ -102,53 +102,51 @@
// https://github.com/serde-rs/serde/issues/812
#![cfg_attr(feature = "unstable", feature(error_in_core, never_type))]
#![allow(unknown_lints, bare_trait_objects, deprecated)]
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
// Ignored clippy and clippy_pedantic lints
#![cfg_attr(
feature = "cargo-clippy",
allow(
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
unnested_or_patterns,
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
semicolon_if_nothing_returned,
// not available in our oldest supported compiler
empty_enum,
type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
// integer and float ser/de requires these sorts of casts
cast_possible_truncation,
cast_possible_wrap,
cast_sign_loss,
// things are often more readable this way
cast_lossless,
module_name_repetitions,
option_if_let_else,
single_match_else,
type_complexity,
use_self,
zero_prefixed_literal,
// correctly used
derive_partial_eq_without_eq,
enum_glob_use,
explicit_auto_deref,
let_underscore_untyped,
map_err_ignore,
new_without_default,
result_unit_err,
wildcard_imports,
// not practical
needless_pass_by_value,
similar_names,
too_many_lines,
// preference
doc_markdown,
unseparated_literal_suffix,
// false positive
needless_doctest_main,
// noisy
missing_errors_doc,
must_use_candidate,
)
#![allow(
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
clippy::unnested_or_patterns,
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
clippy::semicolon_if_nothing_returned,
// not available in our oldest supported compiler
clippy::empty_enum,
clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
// integer and float ser/de requires these sorts of casts
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
// things are often more readable this way
clippy::cast_lossless,
clippy::module_name_repetitions,
clippy::option_if_let_else,
clippy::single_match_else,
clippy::type_complexity,
clippy::use_self,
clippy::zero_prefixed_literal,
// correctly used
clippy::derive_partial_eq_without_eq,
clippy::enum_glob_use,
clippy::explicit_auto_deref,
clippy::let_underscore_untyped,
clippy::map_err_ignore,
clippy::new_without_default,
clippy::result_unit_err,
clippy::wildcard_imports,
// not practical
clippy::needless_pass_by_value,
clippy::similar_names,
clippy::too_many_lines,
// preference
clippy::doc_markdown,
clippy::unseparated_literal_suffix,
// false positive
clippy::needless_doctest_main,
// noisy
clippy::missing_errors_doc,
clippy::must_use_candidate,
)]
// Restrictions
#![deny(clippy::question_mark_used)]
// Rustc lints.
#![deny(missing_docs, unused_imports)]
@@ -175,14 +173,16 @@ mod lib {
pub use self::core::cell::{Cell, RefCell};
pub use self::core::clone::{self, Clone};
pub use self::core::cmp::Reverse;
pub use self::core::convert::{self, From, Into};
pub use self::core::default::{self, Default};
pub use self::core::fmt::{self, Debug, Display};
pub use self::core::marker::{self, PhantomData};
pub use self::core::num::Wrapping;
pub use self::core::ops::Range;
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
pub use self::core::option::{self, Option};
pub use self::core::result::{self, Result};
pub use self::core::time::Duration;
#[cfg(all(feature = "alloc", not(feature = "std")))]
pub use alloc::borrow::{Cow, ToOwned};
@@ -220,7 +220,7 @@ mod lib {
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
pub use core::ffi::CStr;
pub use self::core::ffi::CStr;
#[cfg(feature = "std")]
pub use std::ffi::CStr;
@@ -247,18 +247,6 @@ mod lib {
#[cfg(feature = "std")]
pub use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(feature = "std", not(no_collections_bound), no_ops_bound))]
pub use std::collections::Bound;
#[cfg(not(no_core_reverse))]
pub use self::core::cmp::Reverse;
#[cfg(not(no_ops_bound))]
pub use self::core::ops::Bound;
#[cfg(not(no_range_inclusive))]
pub use self::core::ops::RangeInclusive;
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
pub use std::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
@@ -279,16 +267,13 @@ mod lib {
pub use std::sync::atomic::{AtomicI64, AtomicU64};
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
#[cfg(any(feature = "std", not(no_core_duration)))]
pub use self::core::time::Duration;
}
// None of this crate's error handling needs the `From::from` error conversion
// performed implicitly by the `?` operator or the standard library's `try!`
// macro. This simplified macro gives a 5.5% improvement in compile time
// compared to standard `try!`, and 9% improvement compared to `?`.
macro_rules! try {
macro_rules! tri {
($expr:expr) => {
match $expr {
Ok(val) => val,
@@ -309,20 +294,15 @@ pub mod de;
pub mod ser;
#[doc(inline)]
pub use de::{Deserialize, Deserializer};
pub use crate::de::{Deserialize, Deserializer};
#[doc(inline)]
pub use ser::{Serialize, Serializer};
pub use crate::ser::{Serialize, Serializer};
// Used by generated code and doc tests. Not public API.
#[doc(hidden)]
#[path = "private/mod.rs"]
pub mod __private;
#[allow(unused_imports)]
use self::__private as export;
#[allow(unused_imports)]
use self::__private as private;
#[path = "de/seed.rs"]
mod seed;
@@ -335,8 +315,6 @@ mod std_error;
// be annoying for crates that provide handwritten impls or data formats. They
// would need to disable default features and then explicitly re-enable std.
#[cfg(feature = "serde_derive")]
#[allow(unused_imports)]
#[macro_use]
extern crate serde_derive;
/// Derive macro available if serde is built with `features = ["derive"]`.
+4 -5
View File
@@ -11,7 +11,7 @@
/// input. This requires repetitive implementations of all the [`Deserializer`]
/// trait methods.
///
/// ```edition2018
/// ```edition2021
/// # use serde::forward_to_deserialize_any;
/// # use serde::de::{value, Deserializer, Visitor};
/// #
@@ -47,7 +47,7 @@
/// methods so that they forward directly to [`Deserializer::deserialize_any`].
/// You can choose which methods to forward.
///
/// ```edition2018
/// ```edition2021
/// # use serde::forward_to_deserialize_any;
/// # use serde::de::{value, Deserializer, Visitor};
/// #
@@ -78,11 +78,10 @@
/// called `V`. A different type parameter and a different lifetime can be
/// specified explicitly if necessary.
///
/// ```edition2018
/// # use std::marker::PhantomData;
/// #
/// ```edition2021
/// # use serde::forward_to_deserialize_any;
/// # use serde::de::{value, Deserializer, Visitor};
/// # use std::marker::PhantomData;
/// #
/// # struct MyDeserializer<V>(PhantomData<V>);
/// #
+224 -327
View File
@@ -1,10 +1,13 @@
use lib::*;
use crate::lib::*;
use de::value::{BorrowedBytesDeserializer, BytesDeserializer};
use de::{Deserialize, Deserializer, Error, IntoDeserializer, Visitor};
use crate::de::value::{BorrowedBytesDeserializer, BytesDeserializer};
use crate::de::{
Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, IntoDeserializer, VariantAccess,
Visitor,
};
#[cfg(any(feature = "std", feature = "alloc"))]
use de::{DeserializeSeed, MapAccess, Unexpected};
use crate::de::{MapAccess, Unexpected};
#[cfg(any(feature = "std", feature = "alloc"))]
pub use self::content::{
@@ -13,7 +16,7 @@ pub use self::content::{
TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
};
pub use seed::InPlaceSeed;
pub use crate::seed::InPlaceSeed;
/// If the missing field is of type `Option<T>` then treat is as `None`,
/// otherwise it is an error.
@@ -203,13 +206,13 @@ mod content {
// This issue is tracking making some of this stuff public:
// https://github.com/serde-rs/serde/issues/741
use lib::*;
use crate::lib::*;
use __private::size_hint;
use actually_private;
use de::{
self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, IgnoredAny,
MapAccess, SeqAccess, Unexpected, Visitor,
use crate::actually_private;
use crate::de::value::{MapDeserializer, SeqDeserializer};
use crate::de::{
self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected,
IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor,
};
/// Used from generated code to buffer the contents of the Deserializer when
@@ -299,6 +302,17 @@ mod content {
}
}
impl<'de, E> de::IntoDeserializer<'de, E> for Content<'de>
where
E: de::Error,
{
type Deserializer = ContentDeserializer<'de, E>;
fn into_deserializer(self) -> Self::Deserializer {
ContentDeserializer::new(self)
}
}
struct ContentVisitor<'de> {
value: PhantomData<Content<'de>>,
}
@@ -474,8 +488,9 @@ mod content {
where
V: SeqAccess<'de>,
{
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
while let Some(e) = try!(visitor.next_element()) {
let mut vec =
Vec::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
while let Some(e) = tri!(visitor.next_element()) {
vec.push(e);
}
Ok(Content::Seq(vec))
@@ -485,8 +500,11 @@ mod content {
where
V: MapAccess<'de>,
{
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
while let Some(kv) = try!(visitor.next_entry()) {
let mut vec =
Vec::<(Content, Content)>::with_capacity(
size_hint::cautious::<(Content, Content)>(visitor.size_hint()),
);
while let Some(kv) = tri!(visitor.next_entry()) {
vec.push(kv);
}
Ok(Content::Map(vec))
@@ -518,7 +536,7 @@ mod content {
impl<'de> TagOrContentVisitor<'de> {
fn new(name: &'static str) -> Self {
TagOrContentVisitor {
name: name,
name,
value: PhantomData,
}
}
@@ -797,51 +815,29 @@ mod content {
/// Used by generated code to deserialize an internally tagged enum.
///
/// Not public API.
pub struct TaggedContent<'de, T> {
pub tag: T,
pub content: Content<'de>,
}
/// Not public API.
pub struct TaggedContentVisitor<'de, T> {
pub struct TaggedContentVisitor<T> {
tag_name: &'static str,
expecting: &'static str,
value: PhantomData<TaggedContent<'de, T>>,
value: PhantomData<T>,
}
impl<'de, T> TaggedContentVisitor<'de, T> {
impl<T> TaggedContentVisitor<T> {
/// Visitor for the content of an internally tagged enum with the given
/// tag name.
pub fn new(name: &'static str, expecting: &'static str) -> Self {
TaggedContentVisitor {
tag_name: name,
expecting: expecting,
expecting,
value: PhantomData,
}
}
}
impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
where
T: Deserialize<'de>,
{
type Value = TaggedContent<'de, T>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
// Internally tagged enums are only supported in self-describing
// formats.
deserializer.deserialize_any(self)
}
}
impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T>
where
T: Deserialize<'de>,
{
type Value = TaggedContent<'de, T>;
type Value = (T, Content<'de>);
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(self.expecting)
@@ -851,17 +847,14 @@ mod content {
where
S: SeqAccess<'de>,
{
let tag = match try!(seq.next_element()) {
let tag = match tri!(seq.next_element()) {
Some(tag) => tag,
None => {
return Err(de::Error::missing_field(self.tag_name));
}
};
let rest = de::value::SeqAccessDeserializer::new(seq);
Ok(TaggedContent {
tag: tag,
content: try!(Content::deserialize(rest)),
})
Ok((tag, tri!(Content::deserialize(rest))))
}
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
@@ -869,27 +862,27 @@ mod content {
M: MapAccess<'de>,
{
let mut tag = None;
let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint()));
while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
let mut vec = Vec::<(Content, Content)>::with_capacity(size_hint::cautious::<(
Content,
Content,
)>(map.size_hint()));
while let Some(k) = tri!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
match k {
TagOrContent::Tag => {
if tag.is_some() {
return Err(de::Error::duplicate_field(self.tag_name));
}
tag = Some(try!(map.next_value()));
tag = Some(tri!(map.next_value()));
}
TagOrContent::Content(k) => {
let v = try!(map.next_value());
let v = tri!(map.next_value());
vec.push((k, v));
}
}
}
match tag {
None => Err(de::Error::missing_field(self.tag_name)),
Some(tag) => Ok(TaggedContent {
tag: tag,
content: Content::Map(vec),
}),
Some(tag) => Ok((tag, Content::Map(vec))),
}
}
}
@@ -915,7 +908,7 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}
@@ -926,6 +919,20 @@ mod content {
write!(formatter, "{:?} or {:?}", self.tag, self.content)
}
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
match field_index {
0 => Ok(TagOrContentField::Tag),
1 => Ok(TagOrContentField::Content),
_ => Err(de::Error::invalid_value(
Unexpected::Unsigned(field_index),
&self,
)),
}
}
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
where
E: de::Error,
@@ -938,6 +945,19 @@ mod content {
Err(de::Error::invalid_value(Unexpected::Str(field), &self))
}
}
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
if field == self.tag.as_bytes() {
Ok(TagOrContentField::Tag)
} else if field == self.content.as_bytes() {
Ok(TagOrContentField::Content)
} else {
Err(de::Error::invalid_value(Unexpected::Bytes(field), &self))
}
}
}
/// Used by generated code to deserialize an adjacently tagged enum when
@@ -963,7 +983,7 @@ mod content {
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(self)
deserializer.deserialize_identifier(self)
}
}
@@ -978,13 +998,31 @@ mod content {
)
}
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
match field_index {
0 => Ok(TagContentOtherField::Tag),
1 => Ok(TagContentOtherField::Content),
_ => Ok(TagContentOtherField::Other),
}
}
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
if field == self.tag {
self.visit_bytes(field.as_bytes())
}
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
if field == self.tag.as_bytes() {
Ok(TagContentOtherField::Tag)
} else if field == self.content {
} else if field == self.content.as_bytes() {
Ok(TagContentOtherField::Content)
} else {
Ok(TagContentOtherField::Other)
@@ -1050,9 +1088,9 @@ mod content {
E: de::Error,
{
let seq = content.into_iter().map(ContentDeserializer::new);
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
let value = try!(visitor.visit_seq(&mut seq_visitor));
try!(seq_visitor.end());
let mut seq_visitor = SeqDeserializer::new(seq);
let value = tri!(visitor.visit_seq(&mut seq_visitor));
tri!(seq_visitor.end());
Ok(value)
}
@@ -1067,9 +1105,9 @@ mod content {
let map = content
.into_iter()
.map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
let mut map_visitor = de::value::MapDeserializer::new(map);
let value = try!(visitor.visit_map(&mut map_visitor));
try!(map_visitor.end());
let mut map_visitor = MapDeserializer::new(map);
let value = tri!(visitor.visit_map(&mut map_visitor));
tri!(map_visitor.end());
Ok(value)
}
@@ -1457,7 +1495,7 @@ mod content {
/// private API, don't use
pub fn new(content: Content<'de>) -> Self {
ContentDeserializer {
content: content,
content,
err: PhantomData,
}
}
@@ -1478,8 +1516,8 @@ mod content {
{
pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
EnumDeserializer {
variant: variant,
value: value,
variant,
value,
err: PhantomData,
}
}
@@ -1545,7 +1583,7 @@ mod content {
{
match self.value {
Some(Content::Seq(v)) => {
de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor)
}
Some(other) => Err(de::Error::invalid_type(
other.unexpected(),
@@ -1568,10 +1606,10 @@ mod content {
{
match self.value {
Some(Content::Map(v)) => {
de::Deserializer::deserialize_any(MapDeserializer::new(v), visitor)
de::Deserializer::deserialize_any(MapDeserializer::new(v.into_iter()), visitor)
}
Some(Content::Seq(v)) => {
de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor)
de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor)
}
Some(other) => Err(de::Error::invalid_type(
other.unexpected(),
@@ -1585,156 +1623,6 @@ mod content {
}
}
struct SeqDeserializer<'de, E>
where
E: de::Error,
{
iter: <Vec<Content<'de>> as IntoIterator>::IntoIter,
err: PhantomData<E>,
}
impl<'de, E> SeqDeserializer<'de, E>
where
E: de::Error,
{
fn new(vec: Vec<Content<'de>>) -> Self {
SeqDeserializer {
iter: vec.into_iter(),
err: PhantomData,
}
}
}
impl<'de, E> de::Deserializer<'de> for SeqDeserializer<'de, E>
where
E: de::Error,
{
type Error = E;
#[inline]
fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
let len = self.iter.len();
if len == 0 {
visitor.visit_unit()
} else {
let ret = try!(visitor.visit_seq(&mut self));
let remaining = self.iter.len();
if remaining == 0 {
Ok(ret)
} else {
Err(de::Error::invalid_length(len, &"fewer elements in array"))
}
}
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<'de, E>
where
E: de::Error,
{
type Error = E;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
match self.iter.next() {
Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some),
None => Ok(None),
}
}
fn size_hint(&self) -> Option<usize> {
size_hint::from_bounds(&self.iter)
}
}
struct MapDeserializer<'de, E>
where
E: de::Error,
{
iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter,
value: Option<Content<'de>>,
err: PhantomData<E>,
}
impl<'de, E> MapDeserializer<'de, E>
where
E: de::Error,
{
fn new(map: Vec<(Content<'de>, Content<'de>)>) -> Self {
MapDeserializer {
iter: map.into_iter(),
value: None,
err: PhantomData,
}
}
}
impl<'de, E> de::MapAccess<'de> for MapDeserializer<'de, E>
where
E: de::Error,
{
type Error = E;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
match self.iter.next() {
Some((key, value)) => {
self.value = Some(value);
seed.deserialize(ContentDeserializer::new(key)).map(Some)
}
None => Ok(None),
}
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
match self.value.take() {
Some(value) => seed.deserialize(ContentDeserializer::new(value)),
None => Err(de::Error::custom("value is missing")),
}
}
fn size_hint(&self) -> Option<usize> {
size_hint::from_bounds(&self.iter)
}
}
impl<'de, E> de::Deserializer<'de> for MapDeserializer<'de, E>
where
E: de::Error,
{
type Error = E;
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_map(self)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
/// Not public API.
pub struct ContentRefDeserializer<'a, 'de: 'a, E> {
content: &'a Content<'de>,
@@ -1796,9 +1684,9 @@ mod content {
E: de::Error,
{
let seq = content.iter().map(ContentRefDeserializer::new);
let mut seq_visitor = de::value::SeqDeserializer::new(seq);
let value = try!(visitor.visit_seq(&mut seq_visitor));
try!(seq_visitor.end());
let mut seq_visitor = SeqDeserializer::new(seq);
let value = tri!(visitor.visit_seq(&mut seq_visitor));
tri!(seq_visitor.end());
Ok(value)
}
@@ -1816,9 +1704,9 @@ mod content {
ContentRefDeserializer::new(v),
)
});
let mut map_visitor = de::value::MapDeserializer::new(map);
let value = try!(visitor.visit_map(&mut map_visitor));
try!(map_visitor.end());
let mut map_visitor = MapDeserializer::new(map);
let value = tri!(visitor.visit_map(&mut map_visitor));
tri!(map_visitor.end());
Ok(value)
}
@@ -2135,8 +2023,8 @@ mod content {
};
visitor.visit_enum(EnumRefDeserializer {
variant: variant,
value: value,
variant,
value,
err: PhantomData,
})
}
@@ -2180,12 +2068,20 @@ mod content {
/// private API, don't use
pub fn new(content: &'a Content<'de>) -> Self {
ContentRefDeserializer {
content: content,
content,
err: PhantomData,
}
}
}
impl<'a, 'de: 'a, E> Copy for ContentRefDeserializer<'a, 'de, E> {}
impl<'a, 'de: 'a, E> Clone for ContentRefDeserializer<'a, 'de, E> {
fn clone(&self) -> Self {
*self
}
}
struct EnumRefDeserializer<'a, 'de: 'a, E>
where
E: de::Error,
@@ -2330,7 +2226,7 @@ mod content {
if len == 0 {
visitor.visit_unit()
} else {
let ret = try!(visitor.visit_seq(&mut self));
let ret = tri!(visitor.visit_seq(&mut self));
let remaining = self.iter.len();
if remaining == 0 {
Ok(ret)
@@ -2481,8 +2377,8 @@ mod content {
/// Not public API.
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
InternallyTaggedUnitVisitor {
type_name: type_name,
variant_name: variant_name,
type_name,
variant_name,
}
}
}
@@ -2509,7 +2405,7 @@ mod content {
where
M: MapAccess<'de>,
{
while try!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {}
while tri!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {}
Ok(())
}
}
@@ -2526,8 +2422,8 @@ mod content {
/// Not public API.
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
UntaggedUnitVisitor {
type_name: type_name,
variant_name: variant_name,
type_name,
variant_name,
}
}
}
@@ -2731,11 +2627,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_map(FlatInternallyTaggedAccess {
iter: self.0.iter_mut(),
pending: None,
_marker: PhantomData,
})
self.deserialize_map(visitor)
}
fn deserialize_enum<V>(
@@ -2747,17 +2639,8 @@ where
where
V: Visitor<'de>,
{
for item in self.0.iter_mut() {
// items in the vector are nulled out when used. So we can only use
// an item if it's still filled in and if the field is one we care
// about.
let use_item = match *item {
None => false,
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
};
if use_item {
let (key, value) = item.take().unwrap();
for entry in self.0 {
if let Some((key, value)) = flat_map_take_entry(entry, variants) {
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
}
}
@@ -2772,7 +2655,11 @@ where
where
V: Visitor<'de>,
{
visitor.visit_map(FlatMapAccess::new(self.0.iter()))
visitor.visit_map(FlatMapAccess {
iter: self.0.iter(),
pending_content: None,
_marker: PhantomData,
})
}
fn deserialize_struct<V>(
@@ -2784,7 +2671,12 @@ where
where
V: Visitor<'de>,
{
visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
visitor.visit_map(FlatStructAccess {
iter: self.0.iter_mut(),
pending_content: None,
fields,
_marker: PhantomData,
})
}
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
@@ -2811,6 +2703,13 @@ where
visitor.visit_unit()
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
forward_to_deserialize_other! {
deserialize_bool()
deserialize_i8()
@@ -2833,30 +2732,16 @@ where
deserialize_tuple(usize)
deserialize_tuple_struct(&'static str, usize)
deserialize_identifier()
deserialize_ignored_any()
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapAccess<'a, 'de: 'a, E> {
struct FlatMapAccess<'a, 'de: 'a, E> {
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
pending_content: Option<&'a Content<'de>>,
_marker: PhantomData<E>,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
fn new(
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
) -> FlatMapAccess<'a, 'de, E> {
FlatMapAccess {
iter: iter,
pending_content: None,
_marker: PhantomData,
}
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
where
@@ -2871,6 +2756,10 @@ where
for item in &mut self.iter {
// Items in the vector are nulled out when used by a struct.
if let Some((ref key, ref content)) = *item {
// Do not take(), instead borrow this entry. The internally tagged
// enum does its own buffering so we can't tell whether this entry
// is going to be consumed. Borrowing here leaves the entry
// available for later flattened fields.
self.pending_content = Some(content);
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
}
@@ -2890,28 +2779,13 @@ where
}
#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatStructAccess<'a, 'de: 'a, E> {
struct FlatStructAccess<'a, 'de: 'a, E> {
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
pending_content: Option<Content<'de>>,
fields: &'static [&'static str],
_marker: PhantomData<E>,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
fn new(
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
fields: &'static [&'static str],
) -> FlatStructAccess<'a, 'de, E> {
FlatStructAccess {
iter: iter,
pending_content: None,
fields: fields,
_marker: PhantomData,
}
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
where
@@ -2923,17 +2797,8 @@ where
where
T: DeserializeSeed<'de>,
{
while let Some(item) = self.iter.next() {
// items in the vector are nulled out when used. So we can only use
// an item if it's still filled in and if the field is one we care
// about. In case we do not know which fields we want, we take them all.
let use_item = match *item {
None => false,
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
};
if use_item {
let (key, content) = item.take().unwrap();
for entry in self.iter.by_ref() {
if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
self.pending_content = Some(content);
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
}
@@ -2952,44 +2817,76 @@ where
}
}
/// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
/// field name matches any of the recognized ones.
#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
pending: Option<&'a Content<'de>>,
_marker: PhantomData<E>,
fn flat_map_take_entry<'de>(
entry: &mut Option<(Content<'de>, Content<'de>)>,
recognized: &[&str],
) -> Option<(Content<'de>, Content<'de>)> {
// Entries in the FlatMapDeserializer buffer are nulled out as they get
// claimed for deserialization. We only use an entry if it is still present
// and if the field is one recognized by the current data structure.
let is_recognized = match entry {
None => false,
Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
};
if is_recognized {
entry.take()
} else {
None
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
pub struct AdjacentlyTaggedEnumVariantSeed<F> {
pub enum_name: &'static str,
pub variants: &'static [&'static str],
pub fields_enum: PhantomData<F>,
}
pub struct AdjacentlyTaggedEnumVariantVisitor<F> {
enum_name: &'static str,
fields_enum: PhantomData<F>,
}
impl<'de, F> Visitor<'de> for AdjacentlyTaggedEnumVariantVisitor<F>
where
E: Error,
F: Deserialize<'de>,
{
type Error = E;
type Value = F;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
where
T: DeserializeSeed<'de>,
{
for item in &mut self.iter {
if let Some((ref key, ref content)) = *item {
// Do not take(), instead borrow this entry. The internally tagged
// enum does its own buffering so we can't tell whether this entry
// is going to be consumed. Borrowing here leaves the entry
// available for later flattened fields.
self.pending = Some(content);
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
}
}
Ok(None)
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "variant of enum {}", self.enum_name)
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
T: DeserializeSeed<'de>,
A: EnumAccess<'de>,
{
match self.pending.take() {
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
None => panic!("value is missing"),
}
let (variant, variant_access) = tri!(data.variant());
tri!(variant_access.unit_variant());
Ok(variant)
}
}
impl<'de, F> DeserializeSeed<'de> for AdjacentlyTaggedEnumVariantSeed<F>
where
F: Deserialize<'de>,
{
type Value = F;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_enum(
self.enum_name,
self.variants,
AdjacentlyTaggedEnumVariantVisitor {
enum_name: self.enum_name,
fields_enum: PhantomData,
},
)
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
// Used only by Serde doc tests. Not public API.
use lib::*;
use crate::lib::*;
use ser;
use crate::ser;
#[doc(hidden)]
#[derive(Debug)]
+11 -13
View File
@@ -3,30 +3,28 @@ pub mod de;
#[cfg(not(no_serde_derive))]
pub mod ser;
pub mod size_hint;
// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed.
pub mod doc;
pub use lib::clone::Clone;
pub use lib::convert::{From, Into};
pub use lib::default::Default;
pub use lib::fmt::{self, Formatter};
pub use lib::marker::PhantomData;
pub use lib::option::Option::{self, None, Some};
pub use lib::ptr;
pub use lib::result::Result::{self, Err, Ok};
pub use crate::lib::clone::Clone;
pub use crate::lib::convert::{From, Into};
pub use crate::lib::default::Default;
pub use crate::lib::fmt::{self, Formatter};
pub use crate::lib::marker::PhantomData;
pub use crate::lib::option::Option::{self, None, Some};
pub use crate::lib::ptr;
pub use crate::lib::result::Result::{self, Err, Ok};
pub use self::string::from_utf8_lossy;
#[cfg(any(feature = "alloc", feature = "std"))]
pub use lib::{ToString, Vec};
pub use crate::lib::{ToString, Vec};
#[cfg(not(no_core_try_from))]
pub use lib::convert::TryFrom;
pub use crate::lib::convert::TryFrom;
mod string {
use lib::*;
use crate::lib::*;
#[cfg(any(feature = "std", feature = "alloc"))]
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
+159 -84
View File
@@ -1,6 +1,6 @@
use lib::*;
use crate::lib::*;
use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
use crate::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
#[cfg(any(feature = "std", feature = "alloc"))]
use self::content::{
@@ -27,10 +27,10 @@ where
T: Serialize,
{
value.serialize(TaggedSerializer {
type_ident: type_ident,
variant_ident: variant_ident,
tag: tag,
variant_name: variant_name,
type_ident,
variant_ident,
tag,
variant_name,
delegate: serializer,
})
}
@@ -182,14 +182,14 @@ where
}
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(1)));
try!(map.serialize_entry(self.tag, self.variant_name));
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
map.end()
}
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(1)));
try!(map.serialize_entry(self.tag, self.variant_name));
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
map.end()
}
@@ -199,9 +199,9 @@ where
_: u32,
inner_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(inner_variant, &()));
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(inner_variant, &()));
map.end()
}
@@ -226,9 +226,9 @@ where
where
T: Serialize,
{
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(inner_variant, inner_value));
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
@@ -269,9 +269,9 @@ where
inner_variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_key(inner_variant));
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(
map,
inner_variant,
@@ -280,8 +280,8 @@ where
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1)));
try!(map.serialize_entry(self.tag, self.variant_name));
let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
Ok(map)
}
@@ -290,8 +290,8 @@ where
name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
try!(state.serialize_field(self.tag, self.variant_name));
let mut state = tri!(self.delegate.serialize_struct(name, len + 1));
tri!(state.serialize_field(self.tag, self.variant_name));
Ok(state)
}
@@ -316,9 +316,9 @@ where
inner_variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_key(inner_variant));
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeStructVariantAsMapValue::new(
map,
inner_variant,
@@ -337,9 +337,9 @@ where
#[cfg(any(feature = "std", feature = "alloc"))]
mod content {
use lib::*;
use crate::lib::*;
use ser::{self, Serialize, Serializer};
use crate::ser::{self, Serialize, Serializer};
pub struct SerializeTupleVariantAsMapValue<M> {
map: M,
@@ -350,8 +350,8 @@ mod content {
impl<M> SerializeTupleVariantAsMapValue<M> {
pub fn new(map: M, name: &'static str, len: usize) -> Self {
SerializeTupleVariantAsMapValue {
map: map,
name: name,
map,
name,
fields: Vec::with_capacity(len),
}
}
@@ -368,13 +368,13 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push(value);
Ok(())
}
fn end(mut self) -> Result<M::Ok, M::Error> {
try!(self
tri!(self
.map
.serialize_value(&Content::TupleStruct(self.name, self.fields)));
self.map.end()
@@ -390,8 +390,8 @@ mod content {
impl<M> SerializeStructVariantAsMapValue<M> {
pub fn new(map: M, name: &'static str, len: usize) -> Self {
SerializeStructVariantAsMapValue {
map: map,
name: name,
map,
name,
fields: Vec::with_capacity(len),
}
}
@@ -412,13 +412,13 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push((key, value));
Ok(())
}
fn end(mut self) -> Result<M::Ok, M::Error> {
try!(self
tri!(self
.map
.serialize_value(&Content::Struct(self.name, self.fields)));
self.map.end()
@@ -499,50 +499,50 @@ mod content {
}
Content::Seq(ref elements) => elements.serialize(serializer),
Content::Tuple(ref elements) => {
use ser::SerializeTuple;
let mut tuple = try!(serializer.serialize_tuple(elements.len()));
use crate::ser::SerializeTuple;
let mut tuple = tri!(serializer.serialize_tuple(elements.len()));
for e in elements {
try!(tuple.serialize_element(e));
tri!(tuple.serialize_element(e));
}
tuple.end()
}
Content::TupleStruct(n, ref fields) => {
use ser::SerializeTupleStruct;
let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
use crate::ser::SerializeTupleStruct;
let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len()));
for f in fields {
try!(ts.serialize_field(f));
tri!(ts.serialize_field(f));
}
ts.end()
}
Content::TupleVariant(n, i, v, ref fields) => {
use ser::SerializeTupleVariant;
let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
use crate::ser::SerializeTupleVariant;
let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
for f in fields {
try!(tv.serialize_field(f));
tri!(tv.serialize_field(f));
}
tv.end()
}
Content::Map(ref entries) => {
use ser::SerializeMap;
let mut map = try!(serializer.serialize_map(Some(entries.len())));
use crate::ser::SerializeMap;
let mut map = tri!(serializer.serialize_map(Some(entries.len())));
for (k, v) in entries {
try!(map.serialize_entry(k, v));
tri!(map.serialize_entry(k, v));
}
map.end()
}
Content::Struct(n, ref fields) => {
use ser::SerializeStruct;
let mut s = try!(serializer.serialize_struct(n, fields.len()));
use crate::ser::SerializeStruct;
let mut s = tri!(serializer.serialize_struct(n, fields.len()));
for &(k, ref v) in fields {
try!(s.serialize_field(k, v));
tri!(s.serialize_field(k, v));
}
s.end()
}
Content::StructVariant(n, i, v, ref fields) => {
use ser::SerializeStructVariant;
let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
use crate::ser::SerializeStructVariant;
let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len()));
for &(k, ref v) in fields {
try!(sv.serialize_field(k, v));
tri!(sv.serialize_field(k, v));
}
sv.end()
}
@@ -639,7 +639,7 @@ mod content {
where
T: Serialize,
{
Ok(Content::Some(Box::new(try!(value.serialize(self)))))
Ok(Content::Some(Box::new(tri!(value.serialize(self)))))
}
fn serialize_unit(self) -> Result<Content, E> {
@@ -669,7 +669,7 @@ mod content {
{
Ok(Content::NewtypeStruct(
name,
Box::new(try!(value.serialize(self))),
Box::new(tri!(value.serialize(self))),
))
}
@@ -687,7 +687,7 @@ mod content {
name,
variant_index,
variant,
Box::new(try!(value.serialize(self))),
Box::new(tri!(value.serialize(self))),
))
}
@@ -711,7 +711,7 @@ mod content {
len: usize,
) -> Result<Self::SerializeTupleStruct, E> {
Ok(SerializeTupleStruct {
name: name,
name,
fields: Vec::with_capacity(len),
error: PhantomData,
})
@@ -725,9 +725,9 @@ mod content {
len: usize,
) -> Result<Self::SerializeTupleVariant, E> {
Ok(SerializeTupleVariant {
name: name,
variant_index: variant_index,
variant: variant,
name,
variant_index,
variant,
fields: Vec::with_capacity(len),
error: PhantomData,
})
@@ -747,7 +747,7 @@ mod content {
len: usize,
) -> Result<Self::SerializeStruct, E> {
Ok(SerializeStruct {
name: name,
name,
fields: Vec::with_capacity(len),
error: PhantomData,
})
@@ -761,9 +761,9 @@ mod content {
len: usize,
) -> Result<Self::SerializeStructVariant, E> {
Ok(SerializeStructVariant {
name: name,
variant_index: variant_index,
variant: variant,
name,
variant_index,
variant,
fields: Vec::with_capacity(len),
error: PhantomData,
})
@@ -786,7 +786,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.elements.push(value);
Ok(())
}
@@ -812,7 +812,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.elements.push(value);
Ok(())
}
@@ -839,7 +839,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push(value);
Ok(())
}
@@ -868,7 +868,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push(value);
Ok(())
}
@@ -900,7 +900,7 @@ mod content {
where
T: Serialize,
{
let key = try!(key.serialize(ContentSerializer::<E>::new()));
let key = tri!(key.serialize(ContentSerializer::<E>::new()));
self.key = Some(key);
Ok(())
}
@@ -913,7 +913,7 @@ mod content {
.key
.take()
.expect("serialize_value called before serialize_key");
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.entries.push((key, value));
Ok(())
}
@@ -927,8 +927,8 @@ mod content {
K: Serialize,
V: Serialize,
{
let key = try!(key.serialize(ContentSerializer::<E>::new()));
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let key = tri!(key.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.entries.push((key, value));
Ok(())
}
@@ -951,7 +951,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push((key, value));
Ok(())
}
@@ -980,7 +980,7 @@ mod content {
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<E>::new()));
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
self.fields.push((key, value));
Ok(())
}
@@ -1025,7 +1025,7 @@ where
type SerializeTupleStruct = Impossible<Self::Ok, M::Error>;
type SerializeMap = FlatMapSerializeMap<'a, M>;
type SerializeStruct = FlatMapSerializeStruct<'a, M>;
type SerializeTupleVariant = Impossible<Self::Ok, M::Error>;
type SerializeTupleVariant = FlatMapSerializeTupleVariantAsMapValue<'a, M>;
type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>;
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
@@ -1133,7 +1133,7 @@ where
where
T: Serialize,
{
try!(self.0.serialize_key(variant));
tri!(self.0.serialize_key(variant));
self.0.serialize_value(value)
}
@@ -1157,10 +1157,11 @@ where
self,
_: &'static str,
_: u32,
_: &'static str,
variant: &'static str,
_: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(Self::bad_type(Unsupported::Enum))
tri!(self.0.serialize_key(variant));
Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0))
}
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
@@ -1182,7 +1183,7 @@ where
inner_variant: &'static str,
_: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
try!(self.0.serialize_key(inner_variant));
tri!(self.0.serialize_key(inner_variant));
Ok(FlatMapSerializeStructVariantAsMapValue::new(
self.0,
inner_variant,
@@ -1259,6 +1260,52 @@ where
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> {
map: &'a mut M,
fields: Vec<Content>,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> FlatMapSerializeTupleVariantAsMapValue<'a, M>
where
M: SerializeMap + 'a,
{
fn new(map: &'a mut M) -> Self {
FlatMapSerializeTupleVariantAsMapValue {
map,
fields: Vec::new(),
}
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, M> ser::SerializeTupleVariant for FlatMapSerializeTupleVariantAsMapValue<'a, M>
where
M: SerializeMap + 'a,
{
type Ok = ();
type Error = M::Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push(value);
Ok(())
}
fn end(self) -> Result<(), Self::Error> {
tri!(self.map.serialize_value(&Content::Seq(self.fields)));
Ok(())
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
map: &'a mut M,
@@ -1273,8 +1320,8 @@ where
{
fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
FlatMapSerializeStructVariantAsMapValue {
map: map,
name: name,
map,
name,
fields: Vec::new(),
}
}
@@ -1296,15 +1343,43 @@ where
where
T: Serialize,
{
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
self.fields.push((key, value));
Ok(())
}
fn end(self) -> Result<(), Self::Error> {
try!(self
tri!(self
.map
.serialize_value(&Content::Struct(self.name, self.fields)));
Ok(())
}
}
pub struct AdjacentlyTaggedEnumVariant {
pub enum_name: &'static str,
pub variant_index: u32,
pub variant_name: &'static str,
}
impl Serialize for AdjacentlyTaggedEnumVariant {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name)
}
}
// Error when Serialize for a non_exhaustive remote enum encounters a variant
// that is not recognized.
pub struct CannotSerializeVariant<T>(pub T);
impl<T> Display for CannotSerializeVariant<T>
where
T: Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "enum variant cannot be serialized: {:?}", self.0)
}
}
+5 -4
View File
@@ -1,5 +1,5 @@
use lib::*;
use ser::{Error, Impossible, Serialize, Serializer};
use crate::lib::*;
use crate::ser::{Error, Impossible, Serialize, Serializer};
impl Error for fmt::Error {
fn custom<T: Display>(_msg: T) -> Self {
@@ -17,8 +17,9 @@ macro_rules! fmt_primitives {
};
}
/// ```edition2018
/// use serde::Serialize;
/// ```edition2021
/// use serde::ser::Serialize;
/// use serde_derive::Serialize;
/// use std::fmt::{self, Display};
///
/// #[derive(Serialize)]
+54 -25
View File
@@ -1,6 +1,6 @@
use lib::*;
use crate::lib::*;
use ser::{Error, Serialize, SerializeTuple, Serializer};
use crate::ser::{Error, Serialize, SerializeTuple, Serializer};
////////////////////////////////////////////////////////////////////////////////
@@ -133,7 +133,7 @@ impl<T> Serialize for [T; 0] {
where
S: Serializer,
{
try!(serializer.serialize_tuple(0)).end()
tri!(serializer.serialize_tuple(0)).end()
}
}
@@ -149,9 +149,9 @@ macro_rules! array_impls {
where
S: Serializer,
{
let mut seq = try!(serializer.serialize_tuple($len));
let mut seq = tri!(serializer.serialize_tuple($len));
for e in self {
try!(seq.serialize_element(e));
tri!(seq.serialize_element(e));
}
seq.end()
}
@@ -248,16 +248,32 @@ where
S: Serializer,
{
use super::SerializeStruct;
let mut state = try!(serializer.serialize_struct("Range", 2));
try!(state.serialize_field("start", &self.start));
try!(state.serialize_field("end", &self.end));
let mut state = tri!(serializer.serialize_struct("Range", 2));
tri!(state.serialize_field("start", &self.start));
tri!(state.serialize_field("end", &self.end));
state.end()
}
}
////////////////////////////////////////////////////////////////////////////////
impl<Idx> Serialize for RangeFrom<Idx>
where
Idx: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use super::SerializeStruct;
let mut state = tri!(serializer.serialize_struct("RangeFrom", 1));
tri!(state.serialize_field("start", &self.start));
state.end()
}
}
////////////////////////////////////////////////////////////////////////////////
#[cfg(not(no_range_inclusive))]
impl<Idx> Serialize for RangeInclusive<Idx>
where
Idx: Serialize,
@@ -267,16 +283,32 @@ where
S: Serializer,
{
use super::SerializeStruct;
let mut state = try!(serializer.serialize_struct("RangeInclusive", 2));
try!(state.serialize_field("start", &self.start()));
try!(state.serialize_field("end", &self.end()));
let mut state = tri!(serializer.serialize_struct("RangeInclusive", 2));
tri!(state.serialize_field("start", &self.start()));
tri!(state.serialize_field("end", &self.end()));
state.end()
}
}
////////////////////////////////////////////////////////////////////////////////
impl<Idx> Serialize for RangeTo<Idx>
where
Idx: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use super::SerializeStruct;
let mut state = tri!(serializer.serialize_struct("RangeTo", 1));
tri!(state.serialize_field("end", &self.end));
state.end()
}
}
////////////////////////////////////////////////////////////////////////////////
#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
impl<T> Serialize for Bound<T>
where
T: Serialize,
@@ -333,9 +365,9 @@ macro_rules! tuple_impls {
where
S: Serializer,
{
let mut tuple = try!(serializer.serialize_tuple($len));
let mut tuple = tri!(serializer.serialize_tuple($len));
$(
try!(tuple.serialize_element(&self.$n));
tri!(tuple.serialize_element(&self.$n));
)+
tuple.end()
}
@@ -504,7 +536,6 @@ where
macro_rules! nonzero_integers {
($($T:ident,)+) => {
$(
#[cfg(not(no_num_nonzero))]
impl Serialize for num::$T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -628,16 +659,15 @@ where
////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", not(no_core_duration)))]
impl Serialize for Duration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use super::SerializeStruct;
let mut state = try!(serializer.serialize_struct("Duration", 2));
try!(state.serialize_field("secs", &self.as_secs()));
try!(state.serialize_field("nanos", &self.subsec_nanos()));
let mut state = tri!(serializer.serialize_struct("Duration", 2));
tri!(state.serialize_field("secs", &self.as_secs()));
tri!(state.serialize_field("nanos", &self.subsec_nanos()));
state.end()
}
}
@@ -655,9 +685,9 @@ impl Serialize for SystemTime {
Ok(duration_since_epoch) => duration_since_epoch,
Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
};
let mut state = try!(serializer.serialize_struct("SystemTime", 2));
try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
let mut state = tri!(serializer.serialize_struct("SystemTime", 2));
tri!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
tri!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
state.end()
}
}
@@ -713,7 +743,7 @@ impl Serialize for net::IpAddr {
}
#[cfg(feature = "std")]
const DEC_DIGITS_LUT: &'static [u8] = b"\
const DEC_DIGITS_LUT: &[u8] = b"\
0001020304050607080910111213141516171819\
2021222324252627282930313233343536373839\
4041424344454647484950515253545556575859\
@@ -929,7 +959,6 @@ where
}
}
#[cfg(not(no_core_reverse))]
impl<T> Serialize for Reverse<T>
where
T: Serialize,
+3 -3
View File
@@ -1,8 +1,8 @@
//! This module contains `Impossible` serializer and its implementations.
use lib::*;
use crate::lib::*;
use ser::{
use crate::ser::{
self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
};
@@ -15,7 +15,7 @@ use ser::{
/// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],
/// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].
///
/// ```edition2018
/// ```edition2021
/// # use serde::ser::{Serializer, Impossible};
/// # use serde::__private::doc::Error;
/// #
+72 -102
View File
@@ -107,7 +107,7 @@
//! [derive section of the manual]: https://serde.rs/derive.html
//! [data formats]: https://serde.rs/#data-formats
use lib::*;
use crate::lib::*;
mod fmt;
mod impls;
@@ -115,15 +115,15 @@ mod impossible;
pub use self::impossible::Impossible;
#[cfg(not(any(feature = "std", feature = "unstable")))]
#[doc(no_inline)]
pub use crate::std_error::Error as StdError;
#[cfg(all(feature = "unstable", not(feature = "std")))]
#[doc(inline)]
#[doc(no_inline)]
pub use core::error::Error as StdError;
#[cfg(feature = "std")]
#[doc(no_inline)]
pub use std::error::Error as StdError;
#[cfg(not(any(feature = "std", feature = "unstable")))]
#[doc(no_inline)]
pub use std_error::Error as StdError;
////////////////////////////////////////////////////////////////////////////////
@@ -149,7 +149,7 @@ macro_rules! declare_error_trait {
/// For example, a filesystem [`Path`] may refuse to serialize
/// itself if it contains invalid UTF-8 data.
///
/// ```edition2018
/// ```edition2021
/// # struct Path;
/// #
/// # impl Path {
@@ -221,7 +221,7 @@ pub trait Serialize {
/// See the [Implementing `Serialize`] section of the manual for more
/// information about how to implement this method.
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
///
/// struct Person {
@@ -388,7 +388,7 @@ pub trait Serializer: Sized {
/// Serialize a `bool` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -410,7 +410,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `i64` and
/// forward to `serialize_i64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -432,7 +432,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `i64` and
/// forward to `serialize_i64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -454,7 +454,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `i64` and
/// forward to `serialize_i64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -472,7 +472,7 @@ pub trait Serializer: Sized {
/// Serialize an `i64` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -491,7 +491,7 @@ pub trait Serializer: Sized {
serde_if_integer128! {
/// Serialize an `i128` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -520,7 +520,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `u64` and
/// forward to `serialize_u64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -542,7 +542,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `u64` and
/// forward to `serialize_u64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -564,7 +564,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `u64` and
/// forward to `serialize_u64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -582,7 +582,7 @@ pub trait Serializer: Sized {
/// Serialize a `u64` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -601,7 +601,7 @@ pub trait Serializer: Sized {
serde_if_integer128! {
/// Serialize a `u128` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -630,7 +630,7 @@ pub trait Serializer: Sized {
/// reasonable implementation would be to cast the value to `f64` and
/// forward to `serialize_f64`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -648,7 +648,7 @@ pub trait Serializer: Sized {
/// Serialize an `f64` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -669,7 +669,7 @@ pub trait Serializer: Sized {
/// If the format does not support characters, it is reasonable to serialize
/// it as a single element `str` or a `u32`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -687,7 +687,7 @@ pub trait Serializer: Sized {
/// Serialize a `&str`.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -711,7 +711,7 @@ pub trait Serializer: Sized {
/// `serialize_seq`. If forwarded, the implementation looks usually just
/// like this:
///
/// ```edition2018
/// ```edition2021
/// # use serde::ser::{Serializer, SerializeSeq};
/// # use serde::__private::doc::Error;
/// #
@@ -740,7 +740,7 @@ pub trait Serializer: Sized {
/// Serialize a [`None`] value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::{Serialize, Serializer};
/// #
/// # enum Option<T> {
@@ -773,7 +773,7 @@ pub trait Serializer: Sized {
/// Serialize a [`Some(T)`] value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::{Serialize, Serializer};
/// #
/// # enum Option<T> {
@@ -808,7 +808,7 @@ pub trait Serializer: Sized {
/// Serialize a `()` value.
///
/// ```edition2018
/// ```edition2021
/// # use serde::Serializer;
/// #
/// # serde::__private_serialize!();
@@ -828,7 +828,7 @@ pub trait Serializer: Sized {
///
/// A reasonable implementation would be to forward to `serialize_unit`.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
///
/// struct Nothing;
@@ -850,7 +850,7 @@ pub trait Serializer: Sized {
/// this variant within the enum, and the `variant` is the name of the
/// variant.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
///
/// enum E {
@@ -883,7 +883,7 @@ pub trait Serializer: Sized {
/// wrappers around the data they contain. A reasonable implementation would
/// be to forward to `value.serialize(self)`.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
///
/// struct Millimeters(u8);
@@ -911,7 +911,7 @@ pub trait Serializer: Sized {
/// this variant within the enum, and the `variant` is the name of the
/// variant. The `value` is the data contained within this newtype variant.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
///
/// enum E {
@@ -949,7 +949,7 @@ pub trait Serializer: Sized {
/// not be computable before the sequence is iterated. Some serializers only
/// support sequences whose length is known up front.
///
/// ```edition2018
/// ```edition2021
/// # use std::marker::PhantomData;
/// #
/// # struct Vec<T>(PhantomData<T>);
@@ -962,14 +962,14 @@ pub trait Serializer: Sized {
/// #
/// # impl<'a, T> IntoIterator for &'a Vec<T> {
/// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
/// #
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
///
/// impl<T> Serialize for Vec<T>
/// where
@@ -994,8 +994,8 @@ pub trait Serializer: Sized {
/// This call must be followed by zero or more calls to `serialize_element`,
/// then a call to `end`.
///
/// ```edition2018
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
@@ -1024,7 +1024,7 @@ pub trait Serializer: Sized {
/// }
/// ```
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// const VRAM_SIZE: usize = 386;
@@ -1052,7 +1052,7 @@ pub trait Serializer: Sized {
/// The `name` is the name of the tuple struct and the `len` is the number
/// of data fields that will be serialized.
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
///
/// struct Rgb(u8, u8, u8);
@@ -1084,7 +1084,7 @@ pub trait Serializer: Sized {
/// this variant within the enum, the `variant` is the name of the variant,
/// and the `len` is the number of data fields that will be serialized.
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
///
/// enum E {
@@ -1130,7 +1130,7 @@ pub trait Serializer: Sized {
/// be computable before the map is iterated. Some serializers only support
/// maps whose length is known up front.
///
/// ```edition2018
/// ```edition2021
/// # use std::marker::PhantomData;
/// #
/// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
@@ -1143,14 +1143,14 @@ pub trait Serializer: Sized {
/// #
/// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
/// # type Item = (&'a K, &'a V);
/// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
/// # type IntoIter = Box<dyn Iterator<Item = (&'a K, &'a V)>>;
/// #
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeMap};
/// use serde::ser::{Serialize, SerializeMap, Serializer};
///
/// impl<K, V> Serialize for HashMap<K, V>
/// where
@@ -1178,7 +1178,7 @@ pub trait Serializer: Sized {
/// The `name` is the name of the struct and the `len` is the number of
/// data fields that will be serialized.
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
///
/// struct Rgb {
@@ -1214,7 +1214,7 @@ pub trait Serializer: Sized {
/// this variant within the enum, the `variant` is the name of the variant,
/// and the `len` is the number of data fields that will be serialized.
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
///
/// enum E {
@@ -1256,7 +1256,7 @@ pub trait Serializer: Sized {
/// using [`serialize_seq`]. Implementors should not need to override this
/// method.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
///
/// struct SecretlyOneHigher {
@@ -1279,22 +1279,9 @@ pub trait Serializer: Sized {
I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_seq(iterator_len_hint(&iter)));
#[cfg(not(no_iterator_try_fold))]
{
let mut iter = iter;
try!(iter.try_for_each(|item| serializer.serialize_element(&item)));
}
#[cfg(no_iterator_try_fold)]
{
for item in iter {
try!(serializer.serialize_element(&item));
}
}
let mut iter = iter.into_iter();
let mut serializer = tri!(self.serialize_seq(iterator_len_hint(&iter)));
tri!(iter.try_for_each(|item| serializer.serialize_element(&item)));
serializer.end()
}
@@ -1304,7 +1291,7 @@ pub trait Serializer: Sized {
/// using [`serialize_map`]. Implementors should not need to override this
/// method.
///
/// ```edition2018
/// ```edition2021
/// use serde::{Serialize, Serializer};
/// use std::collections::BTreeSet;
///
@@ -1330,22 +1317,9 @@ pub trait Serializer: Sized {
V: Serialize,
I: IntoIterator<Item = (K, V)>,
{
let iter = iter.into_iter();
let mut serializer = try!(self.serialize_map(iterator_len_hint(&iter)));
#[cfg(not(no_iterator_try_fold))]
{
let mut iter = iter;
try!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
}
#[cfg(no_iterator_try_fold)]
{
for (key, value) in iter {
try!(serializer.serialize_entry(&key, &value));
}
}
let mut iter = iter.into_iter();
let mut serializer = tri!(self.serialize_map(iterator_len_hint(&iter)));
tri!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
serializer.end()
}
@@ -1355,7 +1329,7 @@ pub trait Serializer: Sized {
/// delegates to [`serialize_str`]. Serializers are encouraged to provide a
/// more efficient implementation if possible.
///
/// ```edition2018
/// ```edition2021
/// # struct DateTime;
/// #
/// # impl DateTime {
@@ -1370,9 +1344,7 @@ pub trait Serializer: Sized {
/// where
/// S: Serializer,
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
/// }
/// }
/// ```
@@ -1393,7 +1365,7 @@ pub trait Serializer: Sized {
/// of this method. If no more sensible behavior is possible, the
/// implementation is expected to return an error.
///
/// ```edition2018
/// ```edition2021
/// # struct DateTime;
/// #
/// # impl DateTime {
@@ -1408,9 +1380,7 @@ pub trait Serializer: Sized {
/// where
/// S: Serializer,
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
/// }
/// }
/// ```
@@ -1428,7 +1398,7 @@ pub trait Serializer: Sized {
/// human-readable one and binary formats like Postcard will prefer the
/// compact one.
///
/// ```edition2018
/// ```edition2021
/// # use std::fmt::{self, Display};
/// #
/// # struct Timestamp;
@@ -1477,7 +1447,7 @@ pub trait Serializer: Sized {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// # use std::marker::PhantomData;
/// #
/// # struct Vec<T>(PhantomData<T>);
@@ -1490,13 +1460,13 @@ pub trait Serializer: Sized {
/// #
/// # impl<'a, T> IntoIterator for &'a Vec<T> {
/// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
///
/// impl<T> Serialize for Vec<T>
/// where
@@ -1541,8 +1511,8 @@ pub trait SerializeSeq {
///
/// # Example use
///
/// ```edition2018
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
@@ -1571,7 +1541,7 @@ pub trait SerializeSeq {
/// }
/// ```
///
/// ```edition2018
/// ```edition2021
/// # use std::marker::PhantomData;
/// #
/// # struct Array<T>(PhantomData<T>);
@@ -1584,13 +1554,13 @@ pub trait SerializeSeq {
/// #
/// # impl<'a, T> IntoIterator for &'a Array<T> {
/// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
@@ -1641,7 +1611,7 @@ pub trait SerializeTuple {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
///
/// struct Rgb(u8, u8, u8);
@@ -1686,7 +1656,7 @@ pub trait SerializeTupleStruct {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
///
/// enum E {
@@ -1744,7 +1714,7 @@ pub trait SerializeTupleVariant {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// # use std::marker::PhantomData;
/// #
/// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
@@ -1757,14 +1727,14 @@ pub trait SerializeTupleVariant {
/// #
/// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
/// # type Item = (&'a K, &'a V);
/// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
/// # type IntoIter = Box<dyn Iterator<Item = (&'a K, &'a V)>>;
/// #
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeMap};
/// use serde::ser::{Serialize, SerializeMap, Serializer};
///
/// impl<K, V> Serialize for HashMap<K, V>
/// where
@@ -1843,7 +1813,7 @@ pub trait SerializeMap {
K: Serialize,
V: Serialize,
{
try!(self.serialize_key(key));
tri!(self.serialize_key(key));
self.serialize_value(value)
}
@@ -1855,7 +1825,7 @@ pub trait SerializeMap {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
///
/// struct Rgb {
@@ -1915,7 +1885,7 @@ pub trait SerializeStruct {
///
/// # Example use
///
/// ```edition2018
/// ```edition2021
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
///
/// enum E {
+4 -4
View File
@@ -1,4 +1,4 @@
use lib::{Debug, Display};
use crate::lib::{Debug, Display};
/// Either a re-export of std::error::Error or a new identical trait, depending
/// on whether Serde's "std" feature is enabled.
@@ -9,7 +9,7 @@ use lib::{Debug, Display};
/// generally provide their error types with a `std::error::Error` impl
/// directly:
///
/// ```edition2018
/// ```edition2021
/// #[derive(Debug)]
/// struct MySerError {...}
///
@@ -29,7 +29,7 @@ use lib::{Debug, Display};
/// std = ["serde/std"]
/// ```
///
/// ```edition2018
/// ```edition2021
/// #[cfg(feature = "std")]
/// impl std::error::Error for MySerError {}
/// ```
@@ -37,7 +37,7 @@ use lib::{Debug, Display};
/// ... or else provide the std Error impl unconditionally via Serde's
/// re-export:
///
/// ```edition2018
/// ```edition2021
/// impl serde::ser::StdError for MySerError {}
/// ```
pub trait Error: Debug + Display {
-1
View File
@@ -1 +0,0 @@
msrv = "1.31.0"
+5 -5
View File
@@ -1,12 +1,11 @@
[package]
name = "serde_derive"
version = "1.0.157" # remember to update html_root_url
version = "1.0.185" # remember to update html_root_url
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
categories = ["no-std"]
categories = ["no-std", "no-std::no-alloc"]
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
documentation = "https://serde.rs/derive.html"
homepage = "https://serde.rs"
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
keywords = ["serde", "serialization", "no_std", "derive"]
license = "MIT OR Apache-2.0"
readme = "crates-io.md"
@@ -24,10 +23,11 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
syn = "2.0.28"
[dev-dependencies]
serde = { version = "1.0", path = "../serde" }
serde = { version = "1", path = "../serde" }
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]
-38
View File
@@ -1,38 +0,0 @@
use std::env;
use std::process::Command;
use std::str;
// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};
// Underscore const names stabilized in Rust 1.37:
// https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#using-unnamed-const-items-for-macros
if minor < 37 {
println!("cargo:rustc-cfg=no_underscore_consts");
}
// The ptr::addr_of! macro stabilized in Rust 1.51:
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
if minor < 51 {
println!("cargo:rustc-cfg=no_ptr_addr_of");
}
}
fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}
+6 -9
View File
@@ -1,12 +1,9 @@
use std::collections::HashSet;
use syn;
use syn::punctuated::{Pair, Punctuated};
use internals::ast::{Container, Data};
use internals::{attr, ungroup};
use crate::internals::ast::{Container, Data};
use crate::internals::{attr, ungroup};
use proc_macro2::Span;
use std::collections::HashSet;
use syn::punctuated::{Pair, Punctuated};
use syn::Token;
// Remove the default from every type parameter because in the generated impls
// they look like associated types: "error: associated type bindings are not
@@ -259,7 +256,7 @@ pub fn with_bound(
};
match &cont.data {
Data::Enum(variants) => {
for variant in variants.iter() {
for variant in variants {
let relevant_fields = variant
.fields
.iter()
+502 -512
View File
File diff suppressed because it is too large Load Diff
+4 -25
View File
@@ -1,23 +1,7 @@
use proc_macro2::{Ident, TokenStream};
use quote::format_ident;
use syn;
use try;
pub fn wrap_in_const(
serde_path: Option<&syn::Path>,
trait_: &str,
ty: &Ident,
code: TokenStream,
) -> TokenStream {
let try_replacement = try::replacement();
let dummy_const = if cfg!(no_underscore_consts) {
format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty))
} else {
format_ident!("_")
};
use proc_macro2::TokenStream;
use quote::quote;
pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream {
let use_serde = match serde_path {
Some(path) => quote! {
use #path as _serde;
@@ -31,14 +15,9 @@ pub fn wrap_in_const(
quote! {
#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = {
const _: () = {
#use_serde
#try_replacement
#code
};
}
}
fn unraw(ident: &Ident) -> String {
ident.to_string().trim_start_matches("r#").to_owned()
}
+1 -1
View File
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::token;
use syn::{token, Token};
pub enum Fragment {
/// Tokens that can be used as an expression.
+24 -10
View File
@@ -1,10 +1,8 @@
//! A Serde ast, parsed from the Syn ast and ready to generate Rust code.
use internals::attr;
use internals::check;
use internals::{Ctxt, Derive};
use syn;
use crate::internals::{attr, check, Ctxt, Derive};
use syn::punctuated::Punctuated;
use syn::Token;
/// A source data structure annotated with `#[derive(Serialize)]` and/or `#[derive(Deserialize)]`,
/// parsed into an internal representation.
@@ -88,9 +86,12 @@ impl<'a> Container<'a> {
if field.attrs.flatten() {
has_flatten = true;
}
field
.attrs
.rename_by_rules(variant.attrs.rename_all_rules());
field.attrs.rename_by_rules(
variant
.attrs
.rename_all_rules()
.or(attrs.rename_all_fields_rules()),
);
}
}
}
@@ -121,7 +122,7 @@ impl<'a> Container<'a> {
}
impl<'a> Data<'a> {
pub fn all_fields(&'a self) -> Box<Iterator<Item = &'a Field<'a>> + 'a> {
pub fn all_fields(&'a self) -> Box<dyn Iterator<Item = &'a Field<'a>> + 'a> {
match self {
Data::Enum(variants) => {
Box::new(variants.iter().flat_map(|variant| variant.fields.iter()))
@@ -140,7 +141,7 @@ fn enum_from_ast<'a>(
variants: &'a Punctuated<syn::Variant, Token![,]>,
container_default: &attr::Default,
) -> Vec<Variant<'a>> {
variants
let variants: Vec<Variant> = variants
.iter()
.map(|variant| {
let attrs = attr::Variant::from_ast(cx, variant);
@@ -154,7 +155,20 @@ fn enum_from_ast<'a>(
original: variant,
}
})
.collect()
.collect();
let index_of_last_tagged_variant = variants
.iter()
.rposition(|variant| !variant.attrs.untagged());
if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant {
for variant in &variants[..index_of_last_tagged_variant] {
if variant.attrs.untagged() {
cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");
}
}
}
variants
}
fn struct_from_ast<'a>(
+186 -81
View File
@@ -1,15 +1,14 @@
use internals::symbol::*;
use internals::{ungroup, Ctxt};
use crate::internals::symbol::*;
use crate::internals::{ungroup, Ctxt};
use proc_macro2::{Spacing, Span, TokenStream, TokenTree};
use quote::ToTokens;
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::iter::FromIterator;
use syn;
use syn::meta::ParseNestedMeta;
use syn::parse::ParseStream;
use syn::punctuated::Punctuated;
use syn::{token, Ident, Lifetime};
use syn::{parse_quote, token, Ident, Lifetime, Token};
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
// are `attr::Container::from_ast`, `attr::Variant::from_ast`, and
@@ -19,7 +18,7 @@ use syn::{token, Ident, Lifetime};
// user will see errors simultaneously for all bad attributes in the crate
// rather than just the first.
pub use internals::case::RenameRule;
pub use crate::internals::case::RenameRule;
struct Attr<'c, T> {
cx: &'c Ctxt,
@@ -135,7 +134,7 @@ pub struct Name {
serialize_renamed: bool,
deserialize: String,
deserialize_renamed: bool,
deserialize_aliases: Vec<String>,
deserialize_aliases: BTreeSet<String>,
}
fn unraw(ident: &Ident) -> String {
@@ -149,16 +148,12 @@ impl Name {
de_name: Attr<String>,
de_aliases: Option<VecAttr<String>>,
) -> Name {
let deserialize_aliases = match de_aliases {
Some(de_aliases) => {
let mut alias_list = BTreeSet::new();
for alias_name in de_aliases.get() {
alias_list.insert(alias_name);
}
alias_list.into_iter().collect()
let mut alias_set = BTreeSet::new();
if let Some(de_aliases) = de_aliases {
for alias_name in de_aliases.get() {
alias_set.insert(alias_name);
}
None => Vec::new(),
};
}
let ser_name = ser_name.get();
let ser_renamed = ser_name.is_some();
@@ -169,35 +164,42 @@ impl Name {
serialize_renamed: ser_renamed,
deserialize: de_name.unwrap_or(source_name),
deserialize_renamed: de_renamed,
deserialize_aliases,
deserialize_aliases: alias_set,
}
}
/// Return the container name for the container when serializing.
pub fn serialize_name(&self) -> String {
self.serialize.clone()
pub fn serialize_name(&self) -> &str {
&self.serialize
}
/// Return the container name for the container when deserializing.
pub fn deserialize_name(&self) -> String {
self.deserialize.clone()
pub fn deserialize_name(&self) -> &str {
&self.deserialize
}
fn deserialize_aliases(&self) -> Vec<String> {
let mut aliases = self.deserialize_aliases.clone();
let main_name = self.deserialize_name();
if !aliases.contains(&main_name) {
aliases.push(main_name);
}
aliases
fn deserialize_aliases(&self) -> &BTreeSet<String> {
&self.deserialize_aliases
}
}
#[derive(Copy, Clone)]
pub struct RenameAllRules {
serialize: RenameRule,
deserialize: RenameRule,
}
impl RenameAllRules {
/// Returns a new `RenameAllRules` with the individual rules of `self` and
/// `other_rules` joined by `RenameRules::or`.
pub fn or(self, other_rules: Self) -> Self {
Self {
serialize: self.serialize.or(other_rules.serialize),
deserialize: self.deserialize.or(other_rules.deserialize),
}
}
}
/// Represents struct or enum attribute information.
pub struct Container {
name: Name,
@@ -205,6 +207,7 @@ pub struct Container {
deny_unknown_fields: bool,
default: Default,
rename_all_rules: RenameAllRules,
rename_all_fields_rules: RenameAllRules,
ser_bound: Option<Vec<syn::WherePredicate>>,
de_bound: Option<Vec<syn::WherePredicate>>,
tag: TagType,
@@ -218,6 +221,7 @@ pub struct Container {
is_packed: bool,
/// Error message generated when type can't be deserialized
expecting: Option<String>,
non_exhaustive: bool,
}
/// Styles of representing an enum.
@@ -288,6 +292,8 @@ impl Container {
let mut default = Attr::none(cx, DEFAULT);
let mut rename_all_ser_rule = Attr::none(cx, RENAME_ALL);
let mut rename_all_de_rule = Attr::none(cx, RENAME_ALL);
let mut rename_all_fields_ser_rule = Attr::none(cx, RENAME_ALL_FIELDS);
let mut rename_all_fields_de_rule = Attr::none(cx, RENAME_ALL_FIELDS);
let mut ser_bound = Attr::none(cx, BOUND);
let mut de_bound = Attr::none(cx, BOUND);
let mut untagged = BoolAttr::none(cx, UNTAGGED);
@@ -301,12 +307,21 @@ impl Container {
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
let mut serde_path = Attr::none(cx, CRATE);
let mut expecting = Attr::none(cx, EXPECTING);
let mut non_exhaustive = false;
for attr in &item.attrs {
if attr.path() != SERDE {
non_exhaustive |=
matches!(&attr.meta, syn::Meta::Path(path) if path == NON_EXHAUSTIVE);
continue;
}
if let syn::Meta::List(meta) = &attr.meta {
if meta.tokens.is_empty() {
continue;
}
}
if let Err(err) = attr.parse_nested_meta(|meta| {
if meta.path == RENAME {
// #[serde(rename = "foo")]
@@ -335,6 +350,44 @@ impl Container {
}
}
}
} else if meta.path == RENAME_ALL_FIELDS {
// #[serde(rename_all_fields = "foo")]
// #[serde(rename_all_fields(serialize = "foo", deserialize = "bar"))]
let one_name = meta.input.peek(Token![=]);
let (ser, de) = get_renames(cx, RENAME_ALL_FIELDS, &meta)?;
match item.data {
syn::Data::Enum(_) => {
if let Some(ser) = ser {
match RenameRule::from_str(&ser.value()) {
Ok(rename_rule) => {
rename_all_fields_ser_rule.set(&meta.path, rename_rule);
}
Err(err) => cx.error_spanned_by(ser, err),
}
}
if let Some(de) = de {
match RenameRule::from_str(&de.value()) {
Ok(rename_rule) => {
rename_all_fields_de_rule.set(&meta.path, rename_rule);
}
Err(err) => {
if !one_name {
cx.error_spanned_by(de, err);
}
}
}
}
}
syn::Data::Struct(_) => {
let msg = "#[serde(rename_all_fields)] can only be used on enums";
cx.syn_error(meta.error(msg));
}
syn::Data::Union(_) => {
let msg = "#[serde(rename_all_fields)] can only be used on enums";
cx.syn_error(meta.error(msg));
}
}
} else if meta.path == TRANSPARENT {
// #[serde(transparent)]
transparent.set_true(meta.path);
@@ -347,21 +400,21 @@ impl Container {
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
match &item.data {
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
syn::Fields::Named(_) => {
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
default.set(&meta.path, Default::Path(path));
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
cx.error_spanned_by(fields, msg);
syn::Fields::Unit => {
let msg = "#[serde(default = \"...\")] can only be used on structs that have fields";
cx.syn_error(meta.error(msg));
}
},
syn::Data::Enum(syn::DataEnum { enum_token, .. }) => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
cx.error_spanned_by(enum_token, msg);
syn::Data::Enum(_) => {
let msg = "#[serde(default = \"...\")] can only be used on structs";
cx.syn_error(meta.error(msg));
}
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
cx.error_spanned_by(union_token, msg);
syn::Data::Union(_) => {
let msg = "#[serde(default = \"...\")] can only be used on structs";
cx.syn_error(meta.error(msg));
}
}
}
@@ -369,21 +422,21 @@ impl Container {
// #[serde(default)]
match &item.data {
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
syn::Fields::Named(_) => {
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
default.set(meta.path, Default::Default);
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let msg = "#[serde(default)] can only be used on structs with named fields";
syn::Fields::Unit => {
let msg = "#[serde(default)] can only be used on structs that have fields";
cx.error_spanned_by(fields, msg);
}
},
syn::Data::Enum(syn::DataEnum { enum_token, .. }) => {
let msg = "#[serde(default)] can only be used on structs with named fields";
cx.error_spanned_by(enum_token, msg);
syn::Data::Enum(_) => {
let msg = "#[serde(default)] can only be used on structs";
cx.syn_error(meta.error(msg));
}
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
let msg = "#[serde(default)] can only be used on structs with named fields";
cx.error_spanned_by(union_token, msg);
syn::Data::Union(_) => {
let msg = "#[serde(default)] can only be used on structs";
cx.syn_error(meta.error(msg));
}
}
}
@@ -399,13 +452,13 @@ impl Container {
syn::Data::Enum(_) => {
untagged.set_true(&meta.path);
}
syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
syn::Data::Struct(_) => {
let msg = "#[serde(untagged)] can only be used on enums";
cx.error_spanned_by(struct_token, msg);
cx.syn_error(meta.error(msg));
}
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
syn::Data::Union(_) => {
let msg = "#[serde(untagged)] can only be used on enums";
cx.error_spanned_by(union_token, msg);
cx.syn_error(meta.error(msg));
}
}
} else if meta.path == TAG {
@@ -421,12 +474,12 @@ impl Container {
}
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
let msg = "#[serde(tag = \"...\")] can only be used on enums and structs with named fields";
cx.error_spanned_by(fields, msg);
cx.syn_error(meta.error(msg));
}
},
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
syn::Data::Union(_) => {
let msg = "#[serde(tag = \"...\")] can only be used on enums and structs with named fields";
cx.error_spanned_by(union_token, msg);
cx.syn_error(meta.error(msg));
}
}
}
@@ -437,13 +490,13 @@ impl Container {
syn::Data::Enum(_) => {
content.set(&meta.path, s.value());
}
syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
syn::Data::Struct(_) => {
let msg = "#[serde(content = \"...\")] can only be used on enums";
cx.error_spanned_by(struct_token, msg);
cx.syn_error(meta.error(msg));
}
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
syn::Data::Union(_) => {
let msg = "#[serde(content = \"...\")] can only be used on enums";
cx.error_spanned_by(union_token, msg);
cx.syn_error(meta.error(msg));
}
}
}
@@ -522,6 +575,10 @@ impl Container {
serialize: rename_all_ser_rule.get().unwrap_or(RenameRule::None),
deserialize: rename_all_de_rule.get().unwrap_or(RenameRule::None),
},
rename_all_fields_rules: RenameAllRules {
serialize: rename_all_fields_ser_rule.get().unwrap_or(RenameRule::None),
deserialize: rename_all_fields_de_rule.get().unwrap_or(RenameRule::None),
},
ser_bound: ser_bound.get(),
de_bound: de_bound.get(),
tag: decide_tag(cx, item, untagged, internal_tag, content),
@@ -534,6 +591,7 @@ impl Container {
serde_path: serde_path.get(),
is_packed,
expecting: expecting.get(),
non_exhaustive,
}
}
@@ -541,8 +599,12 @@ impl Container {
&self.name
}
pub fn rename_all_rules(&self) -> &RenameAllRules {
&self.rename_all_rules
pub fn rename_all_rules(&self) -> RenameAllRules {
self.rename_all_rules
}
pub fn rename_all_fields_rules(&self) -> RenameAllRules {
self.rename_all_fields_rules
}
pub fn transparent(&self) -> bool {
@@ -615,6 +677,10 @@ impl Container {
pub fn expecting(&self) -> Option<&str> {
self.expecting.as_ref().map(String::as_ref)
}
pub fn non_exhaustive(&self) -> bool {
self.non_exhaustive
}
}
fn decide_tag(
@@ -650,7 +716,7 @@ fn decide_tag(
}
TagType::Internal { tag }
}
(Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {
(Some((untagged_tokens, ())), Some((tag_tokens, _)), None) => {
let msg = "enum cannot be both untagged and internally tagged";
cx.error_spanned_by(untagged_tokens, msg);
cx.error_spanned_by(tag_tokens, msg);
@@ -661,14 +727,14 @@ fn decide_tag(
cx.error_spanned_by(content_tokens, msg);
TagType::External
}
(Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {
(Some((untagged_tokens, ())), None, Some((content_tokens, _))) => {
let msg = "untagged enum cannot have #[serde(content = \"...\")]";
cx.error_spanned_by(untagged_tokens, msg);
cx.error_spanned_by(content_tokens, msg);
TagType::External
}
(None, Some((_, tag)), Some((_, content))) => TagType::Adjacent { tag, content },
(Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {
(Some((untagged_tokens, ())), Some((tag_tokens, _)), Some((content_tokens, _))) => {
let msg = "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]";
cx.error_spanned_by(untagged_tokens, msg);
cx.error_spanned_by(tag_tokens, msg);
@@ -690,7 +756,7 @@ fn decide_identifier(
variant_identifier.0.get_with_tokens(),
) {
(_, None, None) => Identifier::No,
(_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
(_, Some((field_identifier_tokens, ())), Some((variant_identifier_tokens, ()))) => {
let msg =
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set";
cx.error_spanned_by(field_identifier_tokens, msg);
@@ -734,6 +800,7 @@ pub struct Variant {
serialize_with: Option<syn::ExprPath>,
deserialize_with: Option<syn::ExprPath>,
borrow: Option<BorrowAttribute>,
untagged: bool,
}
struct BorrowAttribute {
@@ -756,12 +823,19 @@ impl Variant {
let mut serialize_with = Attr::none(cx, SERIALIZE_WITH);
let mut deserialize_with = Attr::none(cx, DESERIALIZE_WITH);
let mut borrow = Attr::none(cx, BORROW);
let mut untagged = BoolAttr::none(cx, UNTAGGED);
for attr in &variant.attrs {
if attr.path() != SERDE {
continue;
}
if let syn::Meta::List(meta) = &attr.meta {
if meta.tokens.is_empty() {
continue;
}
}
if let Err(err) = attr.parse_nested_meta(|meta| {
if meta.path == RENAME {
// #[serde(rename = "foo")]
@@ -867,6 +941,8 @@ impl Variant {
cx.error_spanned_by(variant, msg);
}
}
} else if meta.path == UNTAGGED {
untagged.set_true(&meta.path);
} else {
let path = meta.path.to_token_stream().to_string().replace(' ', "");
return Err(
@@ -893,6 +969,7 @@ impl Variant {
serialize_with: serialize_with.get(),
deserialize_with: deserialize_with.get(),
borrow: borrow.get(),
untagged: untagged.get(),
}
}
@@ -900,21 +977,24 @@ impl Variant {
&self.name
}
pub fn aliases(&self) -> Vec<String> {
pub fn aliases(&self) -> &BTreeSet<String> {
self.name.deserialize_aliases()
}
pub fn rename_by_rules(&mut self, rules: &RenameAllRules) {
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
if !self.name.serialize_renamed {
self.name.serialize = rules.serialize.apply_to_variant(&self.name.serialize);
}
if !self.name.deserialize_renamed {
self.name.deserialize = rules.deserialize.apply_to_variant(&self.name.deserialize);
}
self.name
.deserialize_aliases
.insert(self.name.deserialize.clone());
}
pub fn rename_all_rules(&self) -> &RenameAllRules {
&self.rename_all_rules
pub fn rename_all_rules(&self) -> RenameAllRules {
self.rename_all_rules
}
pub fn ser_bound(&self) -> Option<&[syn::WherePredicate]> {
@@ -944,6 +1024,10 @@ impl Variant {
pub fn deserialize_with(&self) -> Option<&syn::ExprPath> {
self.deserialize_with.as_ref()
}
pub fn untagged(&self) -> bool {
self.untagged
}
}
/// Represents field attribute information
@@ -1033,6 +1117,12 @@ impl Field {
continue;
}
if let syn::Meta::List(meta) = &attr.meta {
if meta.tokens.is_empty() {
continue;
}
}
if let Err(err) = attr.parse_nested_meta(|meta| {
if meta.path == RENAME {
// #[serde(rename = "foo")]
@@ -1229,17 +1319,20 @@ impl Field {
&self.name
}
pub fn aliases(&self) -> Vec<String> {
pub fn aliases(&self) -> &BTreeSet<String> {
self.name.deserialize_aliases()
}
pub fn rename_by_rules(&mut self, rules: &RenameAllRules) {
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
if !self.name.serialize_renamed {
self.name.serialize = rules.serialize.apply_to_field(&self.name.serialize);
}
if !self.name.deserialize_renamed {
self.name.deserialize = rules.deserialize.apply_to_field(&self.name.deserialize);
}
self.name
.deserialize_aliases
.insert(self.name.deserialize.clone());
}
pub fn skip_serializing(&self) -> bool {
@@ -1381,21 +1474,33 @@ fn get_lit_str2(
meta_item_name: Symbol,
meta: &ParseNestedMeta,
) -> syn::Result<Option<syn::LitStr>> {
match meta.value()?.parse()? {
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit),
..
}) => Ok(Some(lit)),
expr => {
let expr: syn::Expr = meta.value()?.parse()?;
let mut value = &expr;
while let syn::Expr::Group(e) = value {
value = &e.expr;
}
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit),
..
}) = value
{
let suffix = lit.suffix();
if !suffix.is_empty() {
cx.error_spanned_by(
expr,
format!(
"expected serde {} attribute to be a string: `{} = \"...\"`",
attr_name, meta_item_name
),
lit,
format!("unexpected suffix `{}` on string literal", suffix),
);
Ok(None)
}
Ok(Some(lit.clone()))
} else {
cx.error_spanned_by(
expr,
format!(
"expected serde {} attribute to be a string: `{} = \"...\"`",
attr_name, meta_item_name
),
);
Ok(None)
}
}
+13 -10
View File
@@ -1,13 +1,8 @@
//! Code to convert the Rust-styled field/variant (e.g. `my_field`, `MyType`) to the
//! case of the source (e.g. `my-field`, `MY_FIELD`).
// See https://users.rust-lang.org/t/psa-dealing-with-warning-unused-import-std-ascii-asciiext-in-today-s-nightly/13726
#[allow(deprecated, unused_imports)]
use std::ascii::AsciiExt;
use std::fmt::{self, Debug, Display};
use self::RenameRule::*;
use std::fmt::{self, Debug, Display};
/// The different possible ways to change case of fields in a struct, or variants in an enum.
#[derive(Copy, Clone, PartialEq)]
@@ -59,8 +54,8 @@ impl RenameRule {
}
/// Apply a renaming rule to an enum variant, returning the version expected in the source.
pub fn apply_to_variant(&self, variant: &str) -> String {
match *self {
pub fn apply_to_variant(self, variant: &str) -> String {
match self {
None | PascalCase => variant.to_owned(),
LowerCase => variant.to_ascii_lowercase(),
UpperCase => variant.to_ascii_uppercase(),
@@ -84,8 +79,8 @@ impl RenameRule {
}
/// Apply a renaming rule to a struct field, returning the version expected in the source.
pub fn apply_to_field(&self, field: &str) -> String {
match *self {
pub fn apply_to_field(self, field: &str) -> String {
match self {
None | LowerCase | SnakeCase => field.to_owned(),
UpperCase => field.to_ascii_uppercase(),
PascalCase => {
@@ -112,6 +107,14 @@ impl RenameRule {
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
}
}
/// Returns the `RenameRule` if it is not `None`, `rule_b` otherwise.
pub fn or(self, rule_b: Self) -> Self {
match self {
None => rule_b,
_ => self,
}
}
}
pub struct ParseError<'a> {
+75 -44
View File
@@ -1,11 +1,12 @@
use internals::ast::{Container, Data, Field, Style};
use internals::attr::{Identifier, TagType};
use internals::{ungroup, Ctxt, Derive};
use crate::internals::ast::{Container, Data, Field, Style};
use crate::internals::attr::{Default, Identifier, TagType};
use crate::internals::{ungroup, Ctxt, Derive};
use syn::{Member, Type};
/// Cross-cutting checks that require looking at more than a single attrs
/// object. Simpler checks should happen when parsing and building the attrs.
// Cross-cutting checks that require looking at more than a single attrs object.
// Simpler checks should happen when parsing and building the attrs.
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
check_default_on_tuple(cx, cont);
check_remote_generic(cx, cont);
check_getter(cx, cont);
check_flatten(cx, cont);
@@ -17,18 +18,51 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
check_from_and_try_from(cx, cont);
}
/// Remote derive definition type must have either all of the generics of the
/// remote type:
///
/// #[serde(remote = "Generic")]
/// struct Generic<T> {…}
///
/// or none of them, i.e. defining impls for one concrete instantiation of the
/// remote type only:
///
/// #[serde(remote = "Generic<T>")]
/// struct ConcreteDef {…}
///
// If some field of a tuple struct is marked #[serde(default)] then all fields
// after it must also be marked with that attribute, or the struct must have a
// container-level serde(default) attribute. A field's default value is only
// used for tuple fields if the sequence is exhausted at that point; that means
// all subsequent fields will fail to deserialize if they don't have their own
// default.
fn check_default_on_tuple(cx: &Ctxt, cont: &Container) {
if let Default::None = cont.attrs.default() {
if let Data::Struct(Style::Tuple, fields) = &cont.data {
let mut first_default_index = None;
for (i, field) in fields.iter().enumerate() {
// Skipped fields automatically get the #[serde(default)]
// attribute. We are interested only on non-skipped fields here.
if field.attrs.skip_deserializing() {
continue;
}
if let Default::None = field.attrs.default() {
if let Some(first) = first_default_index {
cx.error_spanned_by(
field.ty,
format!("field must have #[serde(default)] because previous field {} has #[serde(default)]", first),
);
}
continue;
}
if first_default_index.is_none() {
first_default_index = Some(i);
}
}
}
}
}
// Remote derive definition type must have either all of the generics of the
// remote type:
//
// #[serde(remote = "Generic")]
// struct Generic<T> {…}
//
// or none of them, i.e. defining impls for one concrete instantiation of the
// remote type only:
//
// #[serde(remote = "Generic<T>")]
// struct ConcreteDef {…}
//
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
if let Some(remote) = cont.attrs.remote() {
let local_has_generic = !cont.generics.params.is_empty();
@@ -39,8 +73,8 @@ fn check_remote_generic(cx: &Ctxt, cont: &Container) {
}
}
/// Getters are only allowed inside structs (not enums) with the `remote`
/// attribute.
// Getters are only allowed inside structs (not enums) with the `remote`
// attribute.
fn check_getter(cx: &Ctxt, cont: &Container) {
match cont.data {
Data::Enum(_) => {
@@ -62,7 +96,7 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
}
}
/// Flattening has some restrictions we can test.
// Flattening has some restrictions we can test.
fn check_flatten(cx: &Ctxt, cont: &Container) {
match &cont.data {
Data::Enum(variants) => {
@@ -101,18 +135,16 @@ fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
}
}
/// The `other` attribute must be used at most once and it must be the last
/// variant of an enum.
///
/// Inside a `variant_identifier` all variants must be unit variants. Inside a
/// `field_identifier` all but possibly one variant must be unit variants. The
/// last variant may be a newtype variant which is an implicit "other" case.
// The `other` attribute must be used at most once and it must be the last
// variant of an enum.
//
// Inside a `variant_identifier` all variants must be unit variants. Inside a
// `field_identifier` all but possibly one variant must be unit variants. The
// last variant may be a newtype variant which is an implicit "other" case.
fn check_identifier(cx: &Ctxt, cont: &Container) {
let variants = match &cont.data {
Data::Enum(variants) => variants,
Data::Struct(_, _) => {
return;
}
Data::Struct(_, _) => return,
};
for (i, variant) in variants.iter().enumerate() {
@@ -189,17 +221,15 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
}
}
/// Skip-(de)serializing attributes are not allowed on variants marked
/// (de)serialize_with.
// Skip-(de)serializing attributes are not allowed on variants marked
// (de)serialize_with.
fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
let variants = match &cont.data {
Data::Enum(variants) => variants,
Data::Struct(_, _) => {
return;
}
Data::Struct(_, _) => return,
};
for variant in variants.iter() {
for variant in variants {
if variant.attrs.serialize_with().is_some() {
if variant.attrs.skip_serializing() {
cx.error_spanned_by(
@@ -264,10 +294,9 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
}
}
/// The tag of an internally-tagged struct variant must not be
/// the same as either one of its fields, as this would result in
/// duplicate keys in the serialized output and/or ambiguity in
/// the to-be-deserialized input.
// The tag of an internally-tagged struct variant must not be the same as either
// one of its fields, as this would result in duplicate keys in the serialized
// output and/or ambiguity in the to-be-deserialized input.
fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
let variants = match &cont.data {
Data::Enum(variants) => variants,
@@ -290,8 +319,10 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
match variant.style {
Style::Struct => {
for field in &variant.fields {
let check_ser = !field.attrs.skip_serializing();
let check_de = !field.attrs.skip_deserializing();
let check_ser =
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
let check_de =
!(field.attrs.skip_deserializing() || variant.attrs.skip_deserializing());
let name = field.attrs.name();
let ser_name = name.serialize_name();
@@ -313,8 +344,8 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
}
}
/// In the case of adjacently-tagged enums, the type and the
/// contents tag must differ, for the same reason.
// In the case of adjacently-tagged enums, the type and the contents tag must
// differ, for the same reason.
fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
let (type_tag, content_tag) = match cont.attrs.tag() {
TagType::Adjacent { tag, content } => (tag, content),
@@ -332,7 +363,7 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
}
}
/// Enums and unit structs cannot be transparent.
// Enums and unit structs cannot be transparent.
fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
if !cont.attrs.transparent() {
return;
+12 -6
View File
@@ -2,7 +2,6 @@ use quote::ToTokens;
use std::cell::RefCell;
use std::fmt::Display;
use std::thread;
use syn;
/// A type to collect errors together and format them.
///
@@ -44,12 +43,19 @@ impl Ctxt {
}
/// Consume this object, producing a formatted error string if there are errors.
pub fn check(self) -> Result<(), Vec<syn::Error>> {
let errors = self.errors.borrow_mut().take().unwrap();
match errors.len() {
0 => Ok(()),
_ => Err(errors),
pub fn check(self) -> syn::Result<()> {
let mut errors = self.errors.borrow_mut().take().unwrap().into_iter();
let mut combined = match errors.next() {
Some(first) => first,
None => return Ok(()),
};
for rest in errors {
combined.combine(rest);
}
Err(combined)
}
}
+5 -6
View File
@@ -1,19 +1,18 @@
pub mod ast;
pub mod attr;
mod ctxt;
pub use self::ctxt::Ctxt;
mod receiver;
pub use self::receiver::replace_receiver;
mod case;
mod check;
mod ctxt;
mod receiver;
mod respan;
mod symbol;
use syn::Type;
pub use self::ctxt::Ctxt;
pub use self::receiver::replace_receiver;
#[derive(Copy, Clone)]
pub enum Derive {
Serialize,
+2 -2
View File
@@ -1,11 +1,11 @@
use internals::respan::respan;
use crate::internals::respan::respan;
use proc_macro2::Span;
use quote::ToTokens;
use std::mem;
use syn::punctuated::Punctuated;
use syn::{
parse_quote, Data, DeriveInput, Expr, ExprPath, GenericArgument, GenericParam, Generics, Macro,
Path, PathArguments, QSelf, ReturnType, Type, TypeParamBound, TypePath, WherePredicate,
Path, PathArguments, QSelf, ReturnType, Token, Type, TypeParamBound, TypePath, WherePredicate,
};
pub fn replace_receiver(input: &mut DeriveInput) {
+2
View File
@@ -19,10 +19,12 @@ pub const FLATTEN: Symbol = Symbol("flatten");
pub const FROM: Symbol = Symbol("from");
pub const GETTER: Symbol = Symbol("getter");
pub const INTO: Symbol = Symbol("into");
pub const NON_EXHAUSTIVE: Symbol = Symbol("non_exhaustive");
pub const OTHER: Symbol = Symbol("other");
pub const REMOTE: Symbol = Symbol("remote");
pub const RENAME: Symbol = Symbol("rename");
pub const RENAME_ALL: Symbol = Symbol("rename_all");
pub const RENAME_ALL_FIELDS: Symbol = Symbol("rename_all_fields");
pub const REPR: Symbol = Symbol("repr");
pub const SERDE: Symbol = Symbol("serde");
pub const SERIALIZE: Symbol = Symbol("serialize");
+7 -15
View File
@@ -1,7 +1,7 @@
//! This crate provides Serde's two derive macros.
//!
//! ```edition2018
//! # use serde_derive::{Serialize, Deserialize};
//! ```edition2021
//! # use serde_derive::{Deserialize, Serialize};
//! #
//! #[derive(Serialize, Deserialize)]
//! # struct S;
@@ -13,8 +13,7 @@
//!
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.157")]
#![allow(unknown_lints, bare_trait_objects)]
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.185")]
// Ignored clippy lints
#![allow(
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
@@ -63,17 +62,16 @@
)]
#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))]
#[macro_use]
extern crate proc_macro2;
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro;
extern crate proc_macro2;
mod internals;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::DeriveInput;
#[macro_use]
@@ -86,13 +84,12 @@ mod dummy;
mod pretend;
mod ser;
mod this;
mod try;
#[proc_macro_derive(Serialize, attributes(serde))]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as DeriveInput);
ser::expand_derive_serialize(&mut input)
.unwrap_or_else(to_compile_errors)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
@@ -100,11 +97,6 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as DeriveInput);
de::expand_derive_deserialize(&mut input)
.unwrap_or_else(to_compile_errors)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
quote!(#(#compile_errors)*)
}
+9 -25
View File
@@ -1,7 +1,6 @@
use crate::internals::ast::{Container, Data, Field, Style, Variant};
use proc_macro2::TokenStream;
use quote::format_ident;
use internals::ast::{Container, Data, Field, Style, Variant};
use quote::{format_ident, quote};
// Suppress dead_code warnings that would otherwise appear when using a remote
// derive. Other than this pretend code, a struct annotated with remote derive
@@ -97,29 +96,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
#[cfg(not(no_ptr_addr_of))]
{
quote! {
match _serde::__private::None::<&#type_ident #ty_generics> {
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
#(
let _ = _serde::__private::ptr::addr_of!(__v.#members);
)*
}
_ => {}
}
}
}
#[cfg(no_ptr_addr_of)]
{
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
quote! {
match _serde::__private::None::<#type_ident #ty_generics> {
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
_ => {}
quote! {
match _serde::__private::None::<&#type_ident #ty_generics> {
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
#(
let _ = _serde::__private::ptr::addr_of!(__v.#members);
)*
}
_ => {}
}
}
}
+98 -79
View File
@@ -1,18 +1,13 @@
use crate::fragment::{Fragment, Match, Stmts};
use crate::internals::ast::{Container, Data, Field, Style, Variant};
use crate::internals::{attr, replace_receiver, Ctxt, Derive};
use crate::{bound, dummy, pretend, this};
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{self, Ident, Index, Member};
use syn::{parse_quote, Ident, Index, Member};
use bound;
use dummy;
use fragment::{Fragment, Match, Stmts};
use internals::ast::{Container, Data, Field, Style, Variant};
use internals::{attr, replace_receiver, Ctxt, Derive};
use pretend;
use this;
pub fn expand_derive_serialize(
input: &mut syn::DeriveInput,
) -> Result<TokenStream, Vec<syn::Error>> {
pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
replace_receiver(input);
let ctxt = Ctxt::new();
@@ -59,8 +54,6 @@ pub fn expand_derive_serialize(
Ok(dummy::wrap_in_const(
cont.attrs.custom_serde_path(),
"SERIALIZE",
ident,
impl_block,
))
}
@@ -289,7 +282,7 @@ fn serialize_tuple_struct(
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_struct(__serializer, #type_name, #len));
let #let_mut __serde_state = _serde::Serializer::serialize_tuple_struct(__serializer, #type_name, #len)?;
#(#serialize_stmts)*
_serde::ser::SerializeTupleStruct::end(__serde_state)
}
@@ -311,7 +304,7 @@ fn serialize_struct_tag_field(cattrs: &attr::Container, struct_trait: &StructTra
let type_name = cattrs.name().serialize_name();
let func = struct_trait.serialize_field(Span::call_site());
quote! {
try!(#func(&mut __serde_state, #tag, #type_name));
#func(&mut __serde_state, #tag, #type_name)?;
}
}
_ => quote! {},
@@ -352,7 +345,7 @@ fn serialize_struct_as_struct(
);
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct(__serializer, #type_name, #len));
let #let_mut __serde_state = _serde::Serializer::serialize_struct(__serializer, #type_name, #len)?;
#tag_field
#(#serialize_fields)*
_serde::ser::SerializeStruct::end(__serde_state)
@@ -396,7 +389,7 @@ fn serialize_struct_as_map(
};
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_map(__serializer, #len));
let #let_mut __serde_state = _serde::Serializer::serialize_map(__serializer, #len)?;
#tag_field
#(#serialize_fields)*
_serde::ser::SerializeMap::end(__serde_state)
@@ -408,7 +401,7 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont
let self_var = &params.self_var;
let arms: Vec<_> = variants
let mut arms: Vec<_> = variants
.iter()
.enumerate()
.map(|(variant_index, variant)| {
@@ -416,6 +409,12 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont
})
.collect();
if cattrs.remote().is_some() && cattrs.non_exhaustive() {
arms.push(quote! {
ref unrecognized => _serde::__private::Err(_serde::ser::Error::custom(_serde::__private::ser::CannotSerializeVariant(unrecognized))),
});
}
quote_expr! {
match *#self_var {
#(#arms)*
@@ -477,17 +476,26 @@ fn serialize_variant(
}
};
let body = Match(match cattrs.tag() {
attr::TagType::External => {
let body = Match(match (cattrs.tag(), variant.attrs.untagged()) {
(attr::TagType::External, false) => {
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
}
attr::TagType::Internal { tag } => {
(attr::TagType::Internal { tag }, false) => {
serialize_internally_tagged_variant(params, variant, cattrs, tag)
}
attr::TagType::Adjacent { tag, content } => {
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
(attr::TagType::Adjacent { tag, content }, false) => {
serialize_adjacently_tagged_variant(
params,
variant,
cattrs,
variant_index,
tag,
content,
)
}
(attr::TagType::None, _) | (_, true) => {
serialize_untagged_variant(params, variant, cattrs)
}
attr::TagType::None => serialize_untagged_variant(params, variant, cattrs),
});
quote! {
@@ -564,7 +572,7 @@ fn serialize_externally_tagged_variant(
},
params,
&variant.fields,
&type_name,
type_name,
),
}
}
@@ -598,10 +606,10 @@ fn serialize_internally_tagged_variant(
match effective_style(variant) {
Style::Unit => {
quote_block! {
let mut __struct = try!(_serde::Serializer::serialize_struct(
__serializer, #type_name, 1));
try!(_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name));
let mut __struct = _serde::Serializer::serialize_struct(
__serializer, #type_name, 1)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name)?;
_serde::ser::SerializeStruct::end(__struct)
}
}
@@ -629,7 +637,7 @@ fn serialize_internally_tagged_variant(
StructVariant::InternallyTagged { tag, variant_name },
params,
&variant.fields,
&type_name,
type_name,
),
Style::Tuple => unreachable!("checked in serde_derive_internals"),
}
@@ -639,12 +647,20 @@ fn serialize_adjacently_tagged_variant(
params: &Parameters,
variant: &Variant,
cattrs: &attr::Container,
variant_index: u32,
tag: &str,
content: &str,
) -> Fragment {
let this_type = &params.this_type;
let type_name = cattrs.name().serialize_name();
let variant_name = variant.attrs.name().serialize_name();
let serialize_variant = quote! {
&_serde::__private::ser::AdjacentlyTaggedEnumVariant {
enum_name: #type_name,
variant_index: #variant_index,
variant_name: #variant_name,
}
};
let inner = Stmts(if let Some(path) = variant.attrs.serialize_with() {
let ser = wrap_serialize_variant_with(params, path, variant);
@@ -655,10 +671,10 @@ fn serialize_adjacently_tagged_variant(
match effective_style(variant) {
Style::Unit => {
return quote_block! {
let mut __struct = try!(_serde::Serializer::serialize_struct(
__serializer, #type_name, 1));
try!(_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name));
let mut __struct = _serde::Serializer::serialize_struct(
__serializer, #type_name, 1)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #serialize_variant)?;
_serde::ser::SerializeStruct::end(__struct)
};
}
@@ -672,12 +688,12 @@ fn serialize_adjacently_tagged_variant(
let span = field.original.span();
let func = quote_spanned!(span=> _serde::ser::SerializeStruct::serialize_field);
return quote_block! {
let mut __struct = try!(_serde::Serializer::serialize_struct(
__serializer, #type_name, 2));
try!(_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name));
try!(#func(
&mut __struct, #content, #field_expr));
let mut __struct = _serde::Serializer::serialize_struct(
__serializer, #type_name, 2)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #serialize_variant)?;
#func(
&mut __struct, #content, #field_expr)?;
_serde::ser::SerializeStruct::end(__struct)
};
}
@@ -688,7 +704,7 @@ fn serialize_adjacently_tagged_variant(
StructVariant::Untagged,
params,
&variant.fields,
&variant_name,
variant_name,
),
}
});
@@ -719,6 +735,7 @@ fn serialize_adjacently_tagged_variant(
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
quote_block! {
#[doc(hidden)]
struct __AdjacentlyTagged #wrapper_generics #where_clause {
data: (#(&'__a #fields_ty,)*),
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
@@ -736,15 +753,15 @@ fn serialize_adjacently_tagged_variant(
}
}
let mut __struct = try!(_serde::Serializer::serialize_struct(
__serializer, #type_name, 2));
try!(_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #variant_name));
try!(_serde::ser::SerializeStruct::serialize_field(
let mut __struct = _serde::Serializer::serialize_struct(
__serializer, #type_name, 2)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #tag, #serialize_variant)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __struct, #content, &__AdjacentlyTagged {
data: (#(#fields_ident,)*),
phantom: _serde::__private::PhantomData::<#this_type #ty_generics>,
}));
})?;
_serde::ser::SerializeStruct::end(__struct)
}
}
@@ -783,16 +800,16 @@ fn serialize_untagged_variant(
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
Style::Struct => {
let type_name = cattrs.name().serialize_name();
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, &type_name)
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, type_name)
}
}
}
enum TupleVariant {
enum TupleVariant<'a> {
ExternallyTagged {
type_name: String,
type_name: &'a str,
variant_index: u32,
variant_name: String,
variant_name: &'a str,
},
Untagged,
}
@@ -834,21 +851,21 @@ fn serialize_tuple_variant(
variant_name,
} => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_variant(
let #let_mut __serde_state = _serde::Serializer::serialize_tuple_variant(
__serializer,
#type_name,
#variant_index,
#variant_name,
#len));
#len)?;
#(#serialize_stmts)*
_serde::ser::SerializeTupleVariant::end(__serde_state)
}
}
TupleVariant::Untagged => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple(
let #let_mut __serde_state = _serde::Serializer::serialize_tuple(
__serializer,
#len));
#len)?;
#(#serialize_stmts)*
_serde::ser::SerializeTuple::end(__serde_state)
}
@@ -859,11 +876,11 @@ fn serialize_tuple_variant(
enum StructVariant<'a> {
ExternallyTagged {
variant_index: u32,
variant_name: String,
variant_name: &'a str,
},
InternallyTagged {
tag: &'a str,
variant_name: String,
variant_name: &'a str,
},
Untagged,
}
@@ -911,40 +928,40 @@ fn serialize_struct_variant(
variant_name,
} => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct_variant(
let #let_mut __serde_state = _serde::Serializer::serialize_struct_variant(
__serializer,
#name,
#variant_index,
#variant_name,
#len,
));
)?;
#(#serialize_fields)*
_serde::ser::SerializeStructVariant::end(__serde_state)
}
}
StructVariant::InternallyTagged { tag, variant_name } => {
quote_block! {
let mut __serde_state = try!(_serde::Serializer::serialize_struct(
let mut __serde_state = _serde::Serializer::serialize_struct(
__serializer,
#name,
#len + 1,
));
try!(_serde::ser::SerializeStruct::serialize_field(
)?;
_serde::ser::SerializeStruct::serialize_field(
&mut __serde_state,
#tag,
#variant_name,
));
)?;
#(#serialize_fields)*
_serde::ser::SerializeStruct::end(__serde_state)
}
}
StructVariant::Untagged => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct(
let #let_mut __serde_state = _serde::Serializer::serialize_struct(
__serializer,
#name,
#len,
));
)?;
#(#serialize_fields)*
_serde::ser::SerializeStruct::end(__serde_state)
}
@@ -982,6 +999,7 @@ fn serialize_struct_variant_with_flatten(
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
quote_block! {
#[doc(hidden)]
struct __EnumFlatten #wrapper_generics #where_clause {
data: (#(&'__a #fields_ty,)*),
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
@@ -993,9 +1011,9 @@ fn serialize_struct_variant_with_flatten(
__S: _serde::Serializer,
{
let (#(#members,)*) = self.data;
let #let_mut __serde_state = try!(_serde::Serializer::serialize_map(
let #let_mut __serde_state = _serde::Serializer::serialize_map(
__serializer,
_serde::__private::None));
_serde::__private::None)?;
#(#serialize_fields)*
_serde::ser::SerializeMap::end(__serde_state)
}
@@ -1014,23 +1032,23 @@ fn serialize_struct_variant_with_flatten(
}
StructVariant::InternallyTagged { tag, variant_name } => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_map(
let #let_mut __serde_state = _serde::Serializer::serialize_map(
__serializer,
_serde::__private::None));
try!(_serde::ser::SerializeMap::serialize_entry(
_serde::__private::None)?;
_serde::ser::SerializeMap::serialize_entry(
&mut __serde_state,
#tag,
#variant_name,
));
)?;
#(#serialize_fields)*
_serde::ser::SerializeMap::end(__serde_state)
}
}
StructVariant::Untagged => {
quote_block! {
let #let_mut __serde_state = try!(_serde::Serializer::serialize_map(
let #let_mut __serde_state = _serde::Serializer::serialize_map(
__serializer,
_serde::__private::None));
_serde::__private::None)?;
#(#serialize_fields)*
_serde::ser::SerializeMap::end(__serde_state)
}
@@ -1075,7 +1093,7 @@ fn serialize_tuple_struct_visitor(
let span = field.original.span();
let func = tuple_trait.serialize_element(span);
let ser = quote! {
try!(#func(&mut __serde_state, #field_expr));
#func(&mut __serde_state, #field_expr)?;
};
match skip {
@@ -1119,12 +1137,12 @@ fn serialize_struct_visitor(
let ser = if field.attrs.flatten() {
let func = quote_spanned!(span=> _serde::Serialize::serialize);
quote! {
try!(#func(&#field_expr, _serde::__private::ser::FlatMapSerializer(&mut __serde_state)));
#func(&#field_expr, _serde::__private::ser::FlatMapSerializer(&mut __serde_state))?;
}
} else {
let func = struct_trait.serialize_field(span);
quote! {
try!(#func(&mut __serde_state, #key_expr, #field_expr));
#func(&mut __serde_state, #key_expr, #field_expr)?;
}
};
@@ -1136,7 +1154,7 @@ fn serialize_struct_visitor(
if !#skip {
#ser
} else {
try!(#skip_func(&mut __serde_state, #key_expr));
#skip_func(&mut __serde_state, #key_expr)?;
}
}
} else {
@@ -1212,6 +1230,7 @@ fn wrap_serialize_with(
});
quote!({
#[doc(hidden)]
struct __SerializeWith #wrapper_impl_generics #where_clause {
values: (#(&'__a #field_tys, )*),
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
@@ -1235,7 +1254,7 @@ fn wrap_serialize_with(
// Serialization of an empty struct results in code like:
//
// let mut __serde_state = try!(serializer.serialize_struct("S", 0));
// let mut __serde_state = serializer.serialize_struct("S", 0)?;
// _serde::ser::SerializeStruct::end(__serde_state)
//
// where we want to omit the `mut` to avoid a warning.
+1 -1
View File
@@ -1,4 +1,4 @@
use internals::ast::Container;
use crate::internals::ast::Container;
use syn::{Path, PathArguments, Token};
pub fn this_type(cont: &Container) -> Path {
-24
View File
@@ -1,24 +0,0 @@
use proc_macro2::{Punct, Spacing, TokenStream};
// None of our generated code requires the `From::from` error conversion
// performed by the standard library's `try!` macro. With this simplified macro
// we see a significant improvement in type checking and borrow checking time of
// the generated code and a slight improvement in binary size.
pub fn replacement() -> TokenStream {
// Cannot pass `$expr` to `quote!` prior to Rust 1.17.0 so interpolate it.
let dollar = Punct::new('$', Spacing::Alone);
quote! {
#[allow(unused_macros)]
macro_rules! try {
(#dollar __expr:expr) => {
match #dollar __expr {
_serde::__private::Ok(__val) => __val,
_serde::__private::Err(__err) => {
return _serde::__private::Err(__err);
}
}
}
}
}
}
-1
View File
@@ -1 +0,0 @@
msrv = "1.31.0"
+4 -3
View File
@@ -1,11 +1,11 @@
[package]
name = "serde_derive_internals"
version = "0.26.0" # remember to update html_root_url
version = "0.28.0" # remember to update html_root_url
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
description = "AST representation used by Serde derive macros. Unstable."
documentation = "https://docs.rs/serde_derive_internals"
exclude = ["build.rs"]
homepage = "https://serde.rs"
include = ["lib.rs", "src/**/*.rs", "LICENSE-APACHE", "LICENSE-MIT"]
keywords = ["serde", "serialization"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/serde-rs/serde"
@@ -17,7 +17,8 @@ path = "lib.rs"
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
syn = { version = "2.0.28", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] }
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]
+3 -5
View File
@@ -1,5 +1,4 @@
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.26.0")]
#![allow(unknown_lints, bare_trait_objects)]
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.28.0")]
// Ignored clippy lints
#![allow(
clippy::cognitive_complexity,
@@ -30,6 +29,7 @@
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::return_self_not_must_use,
clippy::similar_names,
clippy::single_match_else,
clippy::struct_excessive_bools,
@@ -38,11 +38,9 @@
clippy::wildcard_imports
)]
#[macro_use]
extern crate syn;
extern crate proc_macro2;
extern crate quote;
extern crate syn;
#[cfg_attr(serde_build_from_git, path = "../serde_derive/src/internals/mod.rs")]
#[cfg_attr(not(serde_build_from_git), path = "src/mod.rs")]
-1
View File
@@ -1 +0,0 @@
msrv = "1.13.0"
-28
View File
@@ -1,28 +0,0 @@
[package]
name = "serde_test"
version = "1.0.157" # remember to update html_root_url
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
build = "build.rs"
categories = ["development-tools::testing"]
description = "Token De/Serializer for testing De/Serialize implementations"
documentation = "https://docs.rs/serde_test"
homepage = "https://serde.rs"
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
keywords = ["serde", "serialization", "testing", "dev-dependencies"]
license = "MIT OR Apache-2.0"
readme = "crates-io.md"
repository = "https://github.com/serde-rs/serde"
rust-version = "1.19"
[dependencies]
serde = { version = "1.0.60", path = "../serde" }
[dev-dependencies]
serde = { version = "1.0", path = "../serde" }
serde_derive = { version = "1.0", path = "../serde_derive" }
[lib]
doc-scrape-examples = false
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
-1
View File
@@ -1 +0,0 @@
../LICENSE-APACHE
-1
View File
@@ -1 +0,0 @@
../LICENSE-MIT
-1
View File
@@ -1 +0,0 @@
../README.md
-50
View File
@@ -1,50 +0,0 @@
use std::env;
use std::process::Command;
use std::str::{self, FromStr};
// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};
// #[track_caller] stabilized in Rust 1.46:
// https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller
if minor < 46 {
println!("cargo:rustc-cfg=no_track_caller");
}
}
fn rustc_minor_version() -> Option<u32> {
let rustc = match env::var_os("RUSTC") {
Some(rustc) => rustc,
None => return None,
};
let output = match Command::new(rustc).arg("--version").output() {
Ok(output) => output,
Err(_) => return None,
};
let version = match str::from_utf8(&output.stdout) {
Ok(version) => version,
Err(_) => return None,
};
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let next = match pieces.next() {
Some(next) => next,
None => return None,
};
u32::from_str(next).ok()
}
-1
View File
@@ -1 +0,0 @@
../crates-io.md
-223
View File
@@ -1,223 +0,0 @@
use serde::{Deserialize, Serialize};
use de::Deserializer;
use ser::Serializer;
use token::Token;
use std::fmt::Debug;
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct S {
/// a: u8,
/// b: u8,
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
where
T: Serialize + Deserialize<'de> + PartialEq + Debug,
{
assert_ser_tokens(value, tokens);
assert_de_tokens(value, tokens);
}
/// Asserts that `value` serializes to the given `tokens`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_ser_tokens, Token};
/// #
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct S {
/// a: u8,
/// b: u8,
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_ser_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_ser_tokens<T: ?Sized>(value: &T, tokens: &[Token])
where
T: Serialize,
{
let mut ser = Serializer::new(tokens);
match value.serialize(&mut ser) {
Ok(_) => {}
Err(err) => panic!("value failed to serialize: {}", err),
}
if ser.remaining() > 0 {
panic!("{} remaining tokens", ser.remaining());
}
}
/// Asserts that `value` serializes to the given `tokens`, and then yields
/// `error`.
///
/// ```edition2018
/// use std::sync::{Arc, Mutex};
/// use std::thread;
///
/// use serde::Serialize;
/// use serde_test::{assert_ser_tokens_error, Token};
///
/// #[derive(Serialize)]
/// struct Example {
/// lock: Arc<Mutex<u32>>,
/// }
///
/// fn main() {
/// let example = Example { lock: Arc::new(Mutex::new(0)) };
/// let lock = example.lock.clone();
///
/// let _ = thread::spawn(move || {
/// // This thread will acquire the mutex first, unwrapping the result
/// // of `lock` because the lock has not been poisoned.
/// let _guard = lock.lock().unwrap();
///
/// // This panic while holding the lock (`_guard` is in scope) will
/// // poison the mutex.
/// panic!()
/// }).join();
///
/// let expected = &[
/// Token::Struct { name: "Example", len: 1 },
/// Token::Str("lock"),
/// ];
/// let error = "lock poison error while serializing";
/// assert_ser_tokens_error(&example, expected, error);
/// }
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_ser_tokens_error<T: ?Sized>(value: &T, tokens: &[Token], error: &str)
where
T: Serialize,
{
let mut ser = Serializer::new(tokens);
match value.serialize(&mut ser) {
Ok(_) => panic!("value serialized successfully"),
Err(e) => assert_eq!(e, *error),
}
if ser.remaining() > 0 {
panic!("{} remaining tokens", ser.remaining());
}
}
/// Asserts that the given `tokens` deserialize into `value`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_de_tokens, Token};
/// #
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct S {
/// a: u8,
/// b: u8,
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_de_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
where
T: Deserialize<'de> + PartialEq + Debug,
{
let mut de = Deserializer::new(tokens);
let mut deserialized_val = match T::deserialize(&mut de) {
Ok(v) => {
assert_eq!(v, *value);
v
}
Err(e) => panic!("tokens failed to deserialize: {}", e),
};
if de.remaining() > 0 {
panic!("{} remaining tokens", de.remaining());
}
// Do the same thing for deserialize_in_place. This isn't *great* because a
// no-op impl of deserialize_in_place can technically succeed here. Still,
// this should catch a lot of junk.
let mut de = Deserializer::new(tokens);
match T::deserialize_in_place(&mut de, &mut deserialized_val) {
Ok(()) => {
assert_eq!(deserialized_val, *value);
}
Err(e) => panic!("tokens failed to deserialize_in_place: {}", e),
}
if de.remaining() > 0 {
panic!("{} remaining tokens", de.remaining());
}
}
/// Asserts that the given `tokens` yield `error` when deserializing.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_de_tokens_error, Token};
/// #
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// #[serde(deny_unknown_fields)]
/// struct S {
/// a: u8,
/// b: u8,
/// }
///
/// assert_de_tokens_error::<S>(
/// &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("x"),
/// ],
/// "unknown field `x`, expected `a` or `b`",
/// );
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_de_tokens_error<'de, T>(tokens: &'de [Token], error: &str)
where
T: Deserialize<'de>,
{
let mut de = Deserializer::new(tokens);
match T::deserialize(&mut de) {
Ok(_) => panic!("tokens deserialized successfully"),
Err(e) => assert_eq!(e, *error),
}
// There may be one token left if a peek caused the error
de.next_token_opt();
if de.remaining() > 0 {
panic!("{} remaining tokens", de.remaining());
}
}
-847
View File
@@ -1,847 +0,0 @@
use std::fmt;
use serde::ser::{
SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
SerializeTupleStruct, SerializeTupleVariant,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Readable<T: ?Sized>(T);
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Compact<T: ?Sized>(T);
/// Trait to determine whether a value is represented in human-readable or
/// compact form.
///
/// ```edition2018
/// use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// use serde_test::{assert_tokens, Configure, Token};
///
/// #[derive(Debug, PartialEq)]
/// struct Example(u8, u8);
///
/// impl Serialize for Example {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where
/// S: Serializer,
/// {
/// if serializer.is_human_readable() {
/// format!("{}.{}", self.0, self.1).serialize(serializer)
/// } else {
/// (self.0, self.1).serialize(serializer)
/// }
/// }
/// }
///
/// impl<'de> Deserialize<'de> for Example {
/// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
/// where
/// D: Deserializer<'de>,
/// {
/// use serde::de::Error;
/// if deserializer.is_human_readable() {
/// let s = String::deserialize(deserializer)?;
/// let parts: Vec<_> = s.split('.').collect();
/// Ok(Example(
/// parts[0].parse().map_err(D::Error::custom)?,
/// parts[1].parse().map_err(D::Error::custom)?,
/// ))
/// } else {
/// let (x, y) = Deserialize::deserialize(deserializer)?;
/// Ok(Example(x, y))
/// }
/// }
/// }
///
/// fn main() {
/// assert_tokens(
/// &Example(1, 0).compact(),
/// &[
/// Token::Tuple { len: 2 },
/// Token::U8(1),
/// Token::U8(0),
/// Token::TupleEnd,
/// ],
/// );
/// assert_tokens(&Example(1, 0).readable(), &[Token::Str("1.0")]);
/// }
/// ```
pub trait Configure {
/// Marks `self` as using `is_human_readable == true`
fn readable(self) -> Readable<Self>
where
Self: Sized,
{
Readable(self)
}
/// Marks `self` as using `is_human_readable == false`
fn compact(self) -> Compact<Self>
where
Self: Sized,
{
Compact(self)
}
}
impl<T: ?Sized> Configure for T {}
impl<T: ?Sized> Serialize for Readable<T>
where
T: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(Readable(serializer))
}
}
impl<T: ?Sized> Serialize for Compact<T>
where
T: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(Compact(serializer))
}
}
impl<'de, T> Deserialize<'de> for Readable<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
T::deserialize(Readable(deserializer)).map(Readable)
}
}
impl<'de, T> Deserialize<'de> for Compact<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
T::deserialize(Compact(deserializer)).map(Compact)
}
}
impl<'de, T> DeserializeSeed<'de> for Readable<T>
where
T: DeserializeSeed<'de>,
{
type Value = T::Value;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
self.0.deserialize(Readable(deserializer))
}
}
impl<'de, T> DeserializeSeed<'de> for Compact<T>
where
T: DeserializeSeed<'de>,
{
type Value = T::Value;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
self.0.deserialize(Compact(deserializer))
}
}
macro_rules! forward_method {
($name: ident (self $(, $arg: ident : $arg_type: ty)* ) -> $return_type: ty) => {
fn $name (self $(, $arg : $arg_type)* ) -> $return_type {
(self.0).$name( $($arg),* )
}
};
}
macro_rules! forward_serialize_methods {
( $( $name: ident $arg_type: ty ),* ) => {
$(
forward_method!($name(self, v : $arg_type) -> Result<Self::Ok, Self::Error>);
)*
};
}
macro_rules! impl_serializer {
($wrapper:ident, $is_human_readable:expr) => {
impl<S> Serializer for $wrapper<S>
where
S: Serializer,
{
type Ok = S::Ok;
type Error = S::Error;
type SerializeSeq = $wrapper<S::SerializeSeq>;
type SerializeTuple = $wrapper<S::SerializeTuple>;
type SerializeTupleStruct = $wrapper<S::SerializeTupleStruct>;
type SerializeTupleVariant = $wrapper<S::SerializeTupleVariant>;
type SerializeMap = $wrapper<S::SerializeMap>;
type SerializeStruct = $wrapper<S::SerializeStruct>;
type SerializeStructVariant = $wrapper<S::SerializeStructVariant>;
fn is_human_readable(&self) -> bool {
$is_human_readable
}
forward_serialize_methods! {
serialize_bool bool,
serialize_i8 i8,
serialize_i16 i16,
serialize_i32 i32,
serialize_i64 i64,
serialize_u8 u8,
serialize_u16 u16,
serialize_u32 u32,
serialize_u64 u64,
serialize_f32 f32,
serialize_f64 f64,
serialize_char char,
serialize_str &str,
serialize_bytes &[u8],
serialize_unit_struct &'static str
}
fn serialize_unit(self) -> Result<S::Ok, S::Error> {
self.0.serialize_unit()
}
fn serialize_unit_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
) -> Result<S::Ok, S::Error> {
self.0.serialize_unit_variant(name, variant_index, variant)
}
fn serialize_newtype_struct<T: ?Sized>(
self,
name: &'static str,
value: &T,
) -> Result<S::Ok, S::Error>
where
T: Serialize,
{
self.0.serialize_newtype_struct(name, &$wrapper(value))
}
fn serialize_newtype_variant<T: ?Sized>(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<S::Ok, S::Error>
where
T: Serialize,
{
self.0
.serialize_newtype_variant(name, variant_index, variant, &$wrapper(value))
}
fn serialize_none(self) -> Result<S::Ok, Self::Error> {
self.0.serialize_none()
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<S::Ok, Self::Error>
where
T: Serialize,
{
self.0.serialize_some(&$wrapper(value))
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
self.0.serialize_seq(len).map($wrapper)
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
self.0.serialize_tuple(len).map($wrapper)
}
fn serialize_tuple_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
self.0.serialize_tuple_struct(name, len).map($wrapper)
}
fn serialize_tuple_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
self.0
.serialize_tuple_variant(name, variant_index, variant, len)
.map($wrapper)
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
self.0.serialize_map(len).map($wrapper)
}
fn serialize_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
self.0.serialize_struct(name, len).map($wrapper)
}
fn serialize_struct_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
self.0
.serialize_struct_variant(name, variant_index, variant, len)
.map($wrapper)
}
}
impl<S> SerializeSeq for $wrapper<S>
where
S: SerializeSeq,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_element(&$wrapper(value))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeTuple for $wrapper<S>
where
S: SerializeTuple,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_element(&$wrapper(value))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeTupleStruct for $wrapper<S>
where
S: SerializeTupleStruct,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_field(&$wrapper(value))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeTupleVariant for $wrapper<S>
where
S: SerializeTupleVariant,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_field(&$wrapper(value))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeMap for $wrapper<S>
where
S: SerializeMap,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_key(&$wrapper(key))
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_value(&$wrapper(value))
}
fn serialize_entry<K: ?Sized, V: ?Sized>(
&mut self,
key: &K,
value: &V,
) -> Result<(), S::Error>
where
K: Serialize,
V: Serialize,
{
self.0.serialize_entry(key, &$wrapper(value))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeStruct for $wrapper<S>
where
S: SerializeStruct,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: ?Sized>(
&mut self,
name: &'static str,
field: &T,
) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_field(name, &$wrapper(field))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
impl<S> SerializeStructVariant for $wrapper<S>
where
S: SerializeStructVariant,
{
type Ok = S::Ok;
type Error = S::Error;
fn serialize_field<T: ?Sized>(
&mut self,
name: &'static str,
field: &T,
) -> Result<(), S::Error>
where
T: Serialize,
{
self.0.serialize_field(name, &$wrapper(field))
}
fn end(self) -> Result<S::Ok, S::Error> {
self.0.end()
}
}
};
}
impl_serializer!(Readable, true);
impl_serializer!(Compact, false);
use serde::de::{DeserializeSeed, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor};
macro_rules! forward_deserialize_methods {
( $wrapper : ident ( $( $name: ident ),* ) ) => {
$(
fn $name<V>(self, visitor: V) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
(self.0).$name($wrapper(visitor))
}
)*
};
}
macro_rules! impl_deserializer {
($wrapper:ident, $is_human_readable:expr) => {
impl<'de, D> Deserializer<'de> for $wrapper<D>
where
D: Deserializer<'de>,
{
type Error = D::Error;
forward_deserialize_methods! {
$wrapper (
deserialize_any,
deserialize_bool,
deserialize_u8,
deserialize_u16,
deserialize_u32,
deserialize_u64,
deserialize_i8,
deserialize_i16,
deserialize_i32,
deserialize_i64,
deserialize_f32,
deserialize_f64,
deserialize_char,
deserialize_str,
deserialize_string,
deserialize_bytes,
deserialize_byte_buf,
deserialize_option,
deserialize_unit,
deserialize_seq,
deserialize_map,
deserialize_identifier,
deserialize_ignored_any
)
}
fn deserialize_unit_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.deserialize_unit_struct(name, $wrapper(visitor))
}
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.deserialize_newtype_struct(name, $wrapper(visitor))
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.deserialize_tuple(len, $wrapper(visitor))
}
fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0
.deserialize_tuple_struct(name, len, $wrapper(visitor))
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.deserialize_struct(name, fields, $wrapper(visitor))
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.deserialize_enum(name, variants, $wrapper(visitor))
}
fn is_human_readable(&self) -> bool {
$is_human_readable
}
}
impl<'de, D> Visitor<'de> for $wrapper<D>
where
D: Visitor<'de>,
{
type Value = D::Value;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.0.expecting(formatter)
}
fn visit_bool<E>(self, v: bool) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_bool(v)
}
fn visit_i8<E>(self, v: i8) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_i8(v)
}
fn visit_i16<E>(self, v: i16) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_i16(v)
}
fn visit_i32<E>(self, v: i32) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_i32(v)
}
fn visit_i64<E>(self, v: i64) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_i64(v)
}
fn visit_u8<E>(self, v: u8) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_u8(v)
}
fn visit_u16<E>(self, v: u16) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_u16(v)
}
fn visit_u32<E>(self, v: u32) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_u32(v)
}
fn visit_u64<E>(self, v: u64) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_u64(v)
}
fn visit_f32<E>(self, v: f32) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_f32(v)
}
fn visit_f64<E>(self, v: f64) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_f64(v)
}
fn visit_char<E>(self, v: char) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_char(v)
}
fn visit_str<E>(self, v: &str) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_str(v)
}
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_borrowed_str(v)
}
fn visit_string<E>(self, v: String) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_string(v)
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_bytes(v)
}
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_borrowed_bytes(v)
}
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_byte_buf(v)
}
fn visit_none<E>(self) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_none()
}
fn visit_some<D2>(self, deserializer: D2) -> Result<Self::Value, D2::Error>
where
D2: Deserializer<'de>,
{
self.0.visit_some($wrapper(deserializer))
}
fn visit_unit<E>(self) -> Result<D::Value, E>
where
E: Error,
{
self.0.visit_unit()
}
fn visit_newtype_struct<D2>(self, deserializer: D2) -> Result<Self::Value, D2::Error>
where
D2: Deserializer<'de>,
{
self.0.visit_newtype_struct($wrapper(deserializer))
}
fn visit_seq<V>(self, seq: V) -> Result<D::Value, V::Error>
where
V: SeqAccess<'de>,
{
self.0.visit_seq($wrapper(seq))
}
fn visit_map<V>(self, map: V) -> Result<D::Value, V::Error>
where
V: MapAccess<'de>,
{
self.0.visit_map($wrapper(map))
}
fn visit_enum<V>(self, data: V) -> Result<D::Value, V::Error>
where
V: EnumAccess<'de>,
{
self.0.visit_enum($wrapper(data))
}
}
impl<'de, D> SeqAccess<'de> for $wrapper<D>
where
D: SeqAccess<'de>,
{
type Error = D::Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, D::Error>
where
T: DeserializeSeed<'de>,
{
self.0.next_element_seed($wrapper(seed))
}
fn size_hint(&self) -> Option<usize> {
self.0.size_hint()
}
}
impl<'de, D> MapAccess<'de> for $wrapper<D>
where
D: MapAccess<'de>,
{
type Error = D::Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, D::Error>
where
K: DeserializeSeed<'de>,
{
self.0.next_key_seed($wrapper(seed))
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, D::Error>
where
V: DeserializeSeed<'de>,
{
self.0.next_value_seed($wrapper(seed))
}
fn next_entry_seed<K, V>(
&mut self,
kseed: K,
vseed: V,
) -> Result<Option<(K::Value, V::Value)>, D::Error>
where
K: DeserializeSeed<'de>,
V: DeserializeSeed<'de>,
{
self.0.next_entry_seed($wrapper(kseed), $wrapper(vseed))
}
fn size_hint(&self) -> Option<usize> {
self.0.size_hint()
}
}
impl<'de, D> EnumAccess<'de> for $wrapper<D>
where
D: EnumAccess<'de>,
{
type Error = D::Error;
type Variant = $wrapper<D::Variant>;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where
V: DeserializeSeed<'de>,
{
self.0
.variant_seed($wrapper(seed))
.map(|(value, variant)| (value, $wrapper(variant)))
}
}
impl<'de, D> VariantAccess<'de> for $wrapper<D>
where
D: VariantAccess<'de>,
{
type Error = D::Error;
fn unit_variant(self) -> Result<(), D::Error> {
self.0.unit_variant()
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, D::Error>
where
T: DeserializeSeed<'de>,
{
self.0.newtype_variant_seed($wrapper(seed))
}
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.tuple_variant(len, $wrapper(visitor))
}
fn struct_variant<V>(
self,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, D::Error>
where
V: Visitor<'de>,
{
self.0.struct_variant(fields, $wrapper(visitor))
}
}
};
}
impl_deserializer!(Readable, true);
impl_deserializer!(Compact, false);
-683
View File
@@ -1,683 +0,0 @@
use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
use serde::de::{
self, Deserialize, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess,
VariantAccess, Visitor,
};
use error::Error;
use token::Token;
#[derive(Debug)]
pub struct Deserializer<'de> {
tokens: &'de [Token],
}
macro_rules! assert_next_token {
($de:expr, $expected:expr) => {
match $de.next_token_opt() {
Some(token) if token == $expected => {}
Some(other) => panic!(
"expected Token::{} but deserialization wants Token::{}",
other, $expected
),
None => panic!(
"end of tokens but deserialization wants Token::{}",
$expected
),
}
};
}
macro_rules! unexpected {
($token:expr) => {
panic!("deserialization did not expect this token: {}", $token)
};
}
macro_rules! end_of_tokens {
() => {
panic!("ran out of tokens to deserialize")
};
}
impl<'de> Deserializer<'de> {
pub fn new(tokens: &'de [Token]) -> Self {
Deserializer { tokens: tokens }
}
fn peek_token_opt(&self) -> Option<Token> {
self.tokens.first().cloned()
}
fn peek_token(&self) -> Token {
match self.peek_token_opt() {
Some(token) => token,
None => end_of_tokens!(),
}
}
pub fn next_token_opt(&mut self) -> Option<Token> {
match self.tokens.split_first() {
Some((&first, rest)) => {
self.tokens = rest;
Some(first)
}
None => None,
}
}
fn next_token(&mut self) -> Token {
match self.tokens.split_first() {
Some((&first, rest)) => {
self.tokens = rest;
first
}
None => end_of_tokens!(),
}
}
pub fn remaining(&self) -> usize {
self.tokens.len()
}
fn visit_seq<V>(
&mut self,
len: Option<usize>,
end: Token,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let value = visitor.visit_seq(DeserializerSeqVisitor {
de: self,
len: len,
end: end,
})?;
assert_next_token!(self, end);
Ok(value)
}
fn visit_map<V>(
&mut self,
len: Option<usize>,
end: Token,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let value = visitor.visit_map(DeserializerMapVisitor {
de: self,
len: len,
end: end,
})?;
assert_next_token!(self, end);
Ok(value)
}
}
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
type Error = Error;
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf unit seq map identifier ignored_any
}
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
let token = self.next_token();
match token {
Token::Bool(v) => visitor.visit_bool(v),
Token::I8(v) => visitor.visit_i8(v),
Token::I16(v) => visitor.visit_i16(v),
Token::I32(v) => visitor.visit_i32(v),
Token::I64(v) => visitor.visit_i64(v),
Token::U8(v) => visitor.visit_u8(v),
Token::U16(v) => visitor.visit_u16(v),
Token::U32(v) => visitor.visit_u32(v),
Token::U64(v) => visitor.visit_u64(v),
Token::F32(v) => visitor.visit_f32(v),
Token::F64(v) => visitor.visit_f64(v),
Token::Char(v) => visitor.visit_char(v),
Token::Str(v) => visitor.visit_str(v),
Token::BorrowedStr(v) => visitor.visit_borrowed_str(v),
Token::String(v) => visitor.visit_string(v.to_owned()),
Token::Bytes(v) => visitor.visit_bytes(v),
Token::BorrowedBytes(v) => visitor.visit_borrowed_bytes(v),
Token::ByteBuf(v) => visitor.visit_byte_buf(v.to_vec()),
Token::None => visitor.visit_none(),
Token::Some => visitor.visit_some(self),
Token::Unit | Token::UnitStruct { .. } => visitor.visit_unit(),
Token::NewtypeStruct { .. } => visitor.visit_newtype_struct(self),
Token::Seq { len } => self.visit_seq(len, Token::SeqEnd, visitor),
Token::Tuple { len } => self.visit_seq(Some(len), Token::TupleEnd, visitor),
Token::TupleStruct { len, .. } => {
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
}
Token::Map { len } => self.visit_map(len, Token::MapEnd, visitor),
Token::Struct { len, .. } => self.visit_map(Some(len), Token::StructEnd, visitor),
Token::Enum { .. } => {
let variant = self.next_token();
let next = self.peek_token();
match (variant, next) {
(Token::Str(variant), Token::Unit) => {
self.next_token();
visitor.visit_str(variant)
}
(Token::BorrowedStr(variant), Token::Unit) => {
self.next_token();
visitor.visit_borrowed_str(variant)
}
(Token::String(variant), Token::Unit) => {
self.next_token();
visitor.visit_string(variant.to_string())
}
(Token::Bytes(variant), Token::Unit) => {
self.next_token();
visitor.visit_bytes(variant)
}
(Token::BorrowedBytes(variant), Token::Unit) => {
self.next_token();
visitor.visit_borrowed_bytes(variant)
}
(Token::ByteBuf(variant), Token::Unit) => {
self.next_token();
visitor.visit_byte_buf(variant.to_vec())
}
(Token::U8(variant), Token::Unit) => {
self.next_token();
visitor.visit_u8(variant)
}
(Token::U16(variant), Token::Unit) => {
self.next_token();
visitor.visit_u16(variant)
}
(Token::U32(variant), Token::Unit) => {
self.next_token();
visitor.visit_u32(variant)
}
(Token::U64(variant), Token::Unit) => {
self.next_token();
visitor.visit_u64(variant)
}
(variant, Token::Unit) => unexpected!(variant),
(variant, _) => {
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
}
}
}
Token::UnitVariant { variant, .. } => visitor.visit_str(variant),
Token::NewtypeVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
self,
Token::Str(variant),
EnumFormat::Any,
)),
Token::TupleVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
self,
Token::Str(variant),
EnumFormat::Seq,
)),
Token::StructVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
self,
Token::Str(variant),
EnumFormat::Map,
)),
Token::SeqEnd
| Token::TupleEnd
| Token::TupleStructEnd
| Token::MapEnd
| Token::StructEnd
| Token::TupleVariantEnd
| Token::StructVariantEnd => {
unexpected!(token);
}
}
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::Unit | Token::None => {
self.next_token();
visitor.visit_none()
}
Token::Some => {
self.next_token();
visitor.visit_some(self)
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_enum<V>(
self,
name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::Enum { name: n } if name == n => {
self.next_token();
visitor.visit_enum(DeserializerEnumVisitor { de: self })
}
Token::UnitVariant { name: n, .. }
| Token::NewtypeVariant { name: n, .. }
| Token::TupleVariant { name: n, .. }
| Token::StructVariant { name: n, .. }
if name == n =>
{
visitor.visit_enum(DeserializerEnumVisitor { de: self })
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::UnitStruct { .. } => {
assert_next_token!(self, Token::UnitStruct { name: name });
visitor.visit_unit()
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::NewtypeStruct { .. } => {
assert_next_token!(self, Token::NewtypeStruct { name: name });
visitor.visit_newtype_struct(self)
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::Unit | Token::UnitStruct { .. } => {
self.next_token();
visitor.visit_unit()
}
Token::Seq { .. } => {
self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor)
}
Token::Tuple { .. } => {
self.next_token();
self.visit_seq(Some(len), Token::TupleEnd, visitor)
}
Token::TupleStruct { .. } => {
self.next_token();
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::Unit => {
self.next_token();
visitor.visit_unit()
}
Token::UnitStruct { .. } => {
assert_next_token!(self, Token::UnitStruct { name: name });
visitor.visit_unit()
}
Token::Seq { .. } => {
self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor)
}
Token::Tuple { .. } => {
self.next_token();
self.visit_seq(Some(len), Token::TupleEnd, visitor)
}
Token::TupleStruct { len: n, .. } => {
assert_next_token!(self, Token::TupleStruct { name: name, len: n });
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
}
_ => self.deserialize_any(visitor),
}
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.peek_token() {
Token::Struct { len: n, .. } => {
assert_next_token!(self, Token::Struct { name: name, len: n });
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
}
Token::Map { .. } => {
self.next_token();
self.visit_map(Some(fields.len()), Token::MapEnd, visitor)
}
_ => self.deserialize_any(visitor),
}
}
fn is_human_readable(&self) -> bool {
panic!(
"Types which have different human-readable and compact representations \
must explicitly mark their test cases with `serde_test::Configure`"
);
}
}
//////////////////////////////////////////////////////////////////////////
struct DeserializerSeqVisitor<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
len: Option<usize>,
end: Token,
}
impl<'de, 'a> SeqAccess<'de> for DeserializerSeqVisitor<'a, 'de> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
where
T: DeserializeSeed<'de>,
{
if self.de.peek_token_opt() == Some(self.end) {
return Ok(None);
}
self.len = self.len.map(|len| len.saturating_sub(1));
seed.deserialize(&mut *self.de).map(Some)
}
fn size_hint(&self) -> Option<usize> {
self.len
}
}
//////////////////////////////////////////////////////////////////////////
struct DeserializerMapVisitor<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
len: Option<usize>,
end: Token,
}
impl<'de, 'a> MapAccess<'de> for DeserializerMapVisitor<'a, 'de> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where
K: DeserializeSeed<'de>,
{
if self.de.peek_token_opt() == Some(self.end) {
return Ok(None);
}
self.len = self.len.map(|len| len.saturating_sub(1));
seed.deserialize(&mut *self.de).map(Some)
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where
V: DeserializeSeed<'de>,
{
seed.deserialize(&mut *self.de)
}
fn size_hint(&self) -> Option<usize> {
self.len
}
}
//////////////////////////////////////////////////////////////////////////
struct DeserializerEnumVisitor<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
}
impl<'de, 'a> EnumAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
type Error = Error;
type Variant = Self;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
where
V: DeserializeSeed<'de>,
{
match self.de.peek_token() {
Token::UnitVariant { variant: v, .. }
| Token::NewtypeVariant { variant: v, .. }
| Token::TupleVariant { variant: v, .. }
| Token::StructVariant { variant: v, .. } => {
let de = v.into_deserializer();
let value = seed.deserialize(de)?;
Ok((value, self))
}
_ => {
let value = seed.deserialize(&mut *self.de)?;
Ok((value, self))
}
}
}
}
impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
type Error = Error;
fn unit_variant(self) -> Result<(), Error> {
match self.de.peek_token() {
Token::UnitVariant { .. } => {
self.de.next_token();
Ok(())
}
_ => Deserialize::deserialize(self.de),
}
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where
T: DeserializeSeed<'de>,
{
match self.de.peek_token() {
Token::NewtypeVariant { .. } => {
self.de.next_token();
seed.deserialize(self.de)
}
_ => seed.deserialize(self.de),
}
}
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.de.peek_token() {
Token::TupleVariant { len: enum_len, .. } => {
let token = self.de.next_token();
if len == enum_len {
self.de
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
} else {
unexpected!(token);
}
}
Token::Seq {
len: Some(enum_len),
} => {
let token = self.de.next_token();
if len == enum_len {
self.de.visit_seq(Some(len), Token::SeqEnd, visitor)
} else {
unexpected!(token);
}
}
_ => de::Deserializer::deserialize_any(self.de, visitor),
}
}
fn struct_variant<V>(
self,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.de.peek_token() {
Token::StructVariant { len: enum_len, .. } => {
let token = self.de.next_token();
if fields.len() == enum_len {
self.de
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
} else {
unexpected!(token);
}
}
Token::Map {
len: Some(enum_len),
} => {
let token = self.de.next_token();
if fields.len() == enum_len {
self.de
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
} else {
unexpected!(token);
}
}
_ => de::Deserializer::deserialize_any(self.de, visitor),
}
}
}
//////////////////////////////////////////////////////////////////////////
struct EnumMapVisitor<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
variant: Option<Token>,
format: EnumFormat,
}
enum EnumFormat {
Seq,
Map,
Any,
}
impl<'a, 'de> EnumMapVisitor<'a, 'de> {
fn new(de: &'a mut Deserializer<'de>, variant: Token, format: EnumFormat) -> Self {
EnumMapVisitor {
de: de,
variant: Some(variant),
format: format,
}
}
}
impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where
K: DeserializeSeed<'de>,
{
match self.variant.take() {
Some(Token::Str(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
Some(Token::Bytes(variant)) => seed
.deserialize(BytesDeserializer { value: variant })
.map(Some),
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
Some(other) => unexpected!(other),
None => Ok(None),
}
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where
V: DeserializeSeed<'de>,
{
match self.format {
EnumFormat::Seq => {
let value = {
let visitor = DeserializerSeqVisitor {
de: self.de,
len: None,
end: Token::TupleVariantEnd,
};
seed.deserialize(SeqAccessDeserializer::new(visitor))?
};
assert_next_token!(self.de, Token::TupleVariantEnd);
Ok(value)
}
EnumFormat::Map => {
let value = {
let visitor = DeserializerMapVisitor {
de: self.de,
len: None,
end: Token::StructVariantEnd,
};
seed.deserialize(MapAccessDeserializer::new(visitor))?
};
assert_next_token!(self.de, Token::StructVariantEnd);
Ok(value)
}
EnumFormat::Any => seed.deserialize(&mut *self.de),
}
}
}
struct BytesDeserializer {
value: &'static [u8],
}
impl<'de> de::Deserializer<'de> for BytesDeserializer {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_bytes(self.value)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
-43
View File
@@ -1,43 +0,0 @@
use std::error;
use std::fmt::{self, Display};
use serde::{de, ser};
#[derive(Clone, Debug)]
pub struct Error {
msg: String,
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error {
msg: msg.to_string(),
}
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error {
msg: msg.to_string(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&self.msg)
}
}
impl error::Error for Error {
fn description(&self) -> &str {
&self.msg
}
}
impl PartialEq<str> for Error {
fn eq(&self, other: &str) -> bool {
self.msg == other
}
}
-188
View File
@@ -1,188 +0,0 @@
//! This crate provides a convenient concise way to write unit tests for
//! implementations of [`Serialize`] and [`Deserialize`].
//!
//! [`Serialize`]: serde::ser::Serialize
//! [`Deserialize`]: serde::de::Deserialize
//!
//! The `Serialize` impl for a value can be characterized by the sequence of
//! [`Serializer`] calls that are made in the course of serializing the value,
//! so `serde_test` provides a [`Token`] abstraction which corresponds roughly
//! to `Serializer` method calls. There is an [`assert_ser_tokens`] function to
//! test that a value serializes to a particular sequence of method calls, an
//! [`assert_de_tokens`] function to test that a value can be deserialized from
//! a particular sequence of method calls, and an [`assert_tokens`] function to
//! test both directions. There are also functions to test expected failure
//! conditions.
//!
//! [`Serializer`]: serde::ser::Serializer
//!
//! Here is an example from the [`linked-hash-map`] crate.
//!
//! [`linked-hash-map`]: https://github.com/contain-rs/linked-hash-map
//!
//! ```edition2018
//! # const IGNORE: &str = stringify! {
//! use linked_hash_map::LinkedHashMap;
//! # };
//! use serde_test::{Token, assert_tokens};
//!
//! # use std::fmt;
//! # use std::marker::PhantomData;
//! #
//! # use serde::ser::{Serialize, Serializer, SerializeMap};
//! # use serde::de::{Deserialize, Deserializer, Visitor, MapAccess};
//! #
//! # // Dumb imitation of LinkedHashMap.
//! # #[derive(PartialEq, Debug)]
//! # struct LinkedHashMap<K, V>(Vec<(K, V)>);
//! #
//! # impl<K, V> LinkedHashMap<K, V> {
//! # fn new() -> Self {
//! # LinkedHashMap(Vec::new())
//! # }
//! #
//! # fn insert(&mut self, k: K, v: V) {
//! # self.0.push((k, v));
//! # }
//! # }
//! #
//! # impl<K, V> Serialize for LinkedHashMap<K, V>
//! # where
//! # K: Serialize,
//! # V: Serialize,
//! # {
//! # fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
//! # where
//! # S: Serializer,
//! # {
//! # let mut map = serializer.serialize_map(Some(self.0.len()))?;
//! # for &(ref k, ref v) in &self.0 {
//! # map.serialize_entry(k, v)?;
//! # }
//! # map.end()
//! # }
//! # }
//! #
//! # struct LinkedHashMapVisitor<K, V>(PhantomData<(K, V)>);
//! #
//! # impl<'de, K, V> Visitor<'de> for LinkedHashMapVisitor<K, V>
//! # where
//! # K: Deserialize<'de>,
//! # V: Deserialize<'de>,
//! # {
//! # type Value = LinkedHashMap<K, V>;
//! #
//! # fn expecting(&self, _: &mut fmt::Formatter) -> fmt::Result {
//! # unimplemented!()
//! # }
//! #
//! # fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
//! # where
//! # M: MapAccess<'de>,
//! # {
//! # let mut map = LinkedHashMap::new();
//! # while let Some((key, value)) = access.next_entry()? {
//! # map.insert(key, value);
//! # }
//! # Ok(map)
//! # }
//! # }
//! #
//! # impl<'de, K, V> Deserialize<'de> for LinkedHashMap<K, V>
//! # where
//! # K: Deserialize<'de>,
//! # V: Deserialize<'de>,
//! # {
//! # fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
//! # where
//! # D: Deserializer<'de>,
//! # {
//! # deserializer.deserialize_map(LinkedHashMapVisitor(PhantomData))
//! # }
//! # }
//! #
//! #[test]
//! # fn not_a_test_ser_de_empty() {}
//! fn test_ser_de_empty() {
//! let map = LinkedHashMap::<char, u32>::new();
//!
//! assert_tokens(&map, &[
//! Token::Map { len: Some(0) },
//! Token::MapEnd,
//! ]);
//! }
//!
//! #[test]
//! # fn not_a_test_ser_de() {}
//! fn test_ser_de() {
//! let mut map = LinkedHashMap::new();
//! map.insert('b', 20);
//! map.insert('a', 10);
//! map.insert('c', 30);
//!
//! assert_tokens(&map, &[
//! Token::Map { len: Some(3) },
//! Token::Char('b'),
//! Token::I32(20),
//!
//! Token::Char('a'),
//! Token::I32(10),
//!
//! Token::Char('c'),
//! Token::I32(30),
//! Token::MapEnd,
//! ]);
//! }
//! #
//! # fn main() {
//! # test_ser_de_empty();
//! # test_ser_de();
//! # }
//! ```
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.157")]
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
// Ignored clippy lints
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
// Ignored clippy_pedantic lints
#![cfg_attr(
feature = "cargo-clippy",
allow(
cloned_instead_of_copied,
doc_link_with_quotes, // https://github.com/rust-lang/rust-clippy/issues/8961
empty_line_after_outer_attr,
manual_assert,
missing_docs_in_private_items,
missing_panics_doc,
module_name_repetitions,
must_use_candidate,
redundant_field_names,
too_many_lines,
type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
use_debug,
use_self
)
)]
#[macro_use]
extern crate serde;
mod de;
mod error;
mod ser;
mod assert;
mod configure;
mod token;
pub use assert::{
assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_ser_tokens_error,
assert_tokens,
};
pub use token::Token;
pub use configure::{Compact, Configure, Readable};
// Not public API.
#[doc(hidden)]
pub use de::Deserializer;
-464
View File
@@ -1,464 +0,0 @@
use serde::{ser, Serialize};
use error::Error;
use token::Token;
/// A `Serializer` that ensures that a value serializes to a given list of
/// tokens.
#[derive(Debug)]
pub struct Serializer<'a> {
tokens: &'a [Token],
}
impl<'a> Serializer<'a> {
/// Creates the serializer.
pub fn new(tokens: &'a [Token]) -> Self {
Serializer { tokens: tokens }
}
/// Pulls the next token off of the serializer, ignoring it.
fn next_token(&mut self) -> Option<Token> {
if let Some((&first, rest)) = self.tokens.split_first() {
self.tokens = rest;
Some(first)
} else {
None
}
}
pub fn remaining(&self) -> usize {
self.tokens.len()
}
}
macro_rules! assert_next_token {
($ser:expr, $actual:ident) => {{
assert_next_token!($ser, stringify!($actual), Token::$actual, true);
}};
($ser:expr, $actual:ident($v:expr)) => {{
assert_next_token!(
$ser,
format_args!(concat!(stringify!($actual), "({:?})"), $v),
Token::$actual(v),
v == $v
);
}};
($ser:expr, $actual:ident { $($k:ident),* }) => {{
let compare = ($($k,)*);
let field_format = || {
use std::fmt::Write;
let mut buffer = String::new();
$(
write!(&mut buffer, concat!(stringify!($k), ": {:?}, "), $k).unwrap();
)*
buffer
};
assert_next_token!(
$ser,
format_args!(concat!(stringify!($actual), " {{ {}}}"), field_format()),
Token::$actual { $($k),* },
($($k,)*) == compare
);
}};
($ser:expr, $actual:expr, $pat:pat, $guard:expr) => {
match $ser.next_token() {
Some($pat) if $guard => {}
Some(expected) => {
panic!("expected Token::{} but serialized as {}",
expected, $actual);
}
None => {
panic!("expected end of tokens, but {} was serialized",
$actual);
}
}
};
}
impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
type SerializeSeq = Self;
type SerializeTuple = Self;
type SerializeTupleStruct = Self;
type SerializeTupleVariant = Variant<'s, 'a>;
type SerializeMap = Self;
type SerializeStruct = Self;
type SerializeStructVariant = Variant<'s, 'a>;
fn serialize_bool(self, v: bool) -> Result<(), Error> {
assert_next_token!(self, Bool(v));
Ok(())
}
fn serialize_i8(self, v: i8) -> Result<(), Error> {
assert_next_token!(self, I8(v));
Ok(())
}
fn serialize_i16(self, v: i16) -> Result<(), Error> {
assert_next_token!(self, I16(v));
Ok(())
}
fn serialize_i32(self, v: i32) -> Result<(), Error> {
assert_next_token!(self, I32(v));
Ok(())
}
fn serialize_i64(self, v: i64) -> Result<(), Error> {
assert_next_token!(self, I64(v));
Ok(())
}
fn serialize_u8(self, v: u8) -> Result<(), Error> {
assert_next_token!(self, U8(v));
Ok(())
}
fn serialize_u16(self, v: u16) -> Result<(), Error> {
assert_next_token!(self, U16(v));
Ok(())
}
fn serialize_u32(self, v: u32) -> Result<(), Error> {
assert_next_token!(self, U32(v));
Ok(())
}
fn serialize_u64(self, v: u64) -> Result<(), Error> {
assert_next_token!(self, U64(v));
Ok(())
}
fn serialize_f32(self, v: f32) -> Result<(), Error> {
assert_next_token!(self, F32(v));
Ok(())
}
fn serialize_f64(self, v: f64) -> Result<(), Error> {
assert_next_token!(self, F64(v));
Ok(())
}
fn serialize_char(self, v: char) -> Result<(), Error> {
assert_next_token!(self, Char(v));
Ok(())
}
fn serialize_str(self, v: &str) -> Result<(), Error> {
match self.tokens.first() {
Some(&Token::BorrowedStr(_)) => assert_next_token!(self, BorrowedStr(v)),
Some(&Token::String(_)) => assert_next_token!(self, String(v)),
_ => assert_next_token!(self, Str(v)),
}
Ok(())
}
fn serialize_bytes(self, v: &[u8]) -> Result<(), Self::Error> {
match self.tokens.first() {
Some(&Token::BorrowedBytes(_)) => assert_next_token!(self, BorrowedBytes(v)),
Some(&Token::ByteBuf(_)) => assert_next_token!(self, ByteBuf(v)),
_ => assert_next_token!(self, Bytes(v)),
}
Ok(())
}
fn serialize_unit(self) -> Result<(), Error> {
assert_next_token!(self, Unit);
Ok(())
}
fn serialize_unit_struct(self, name: &'static str) -> Result<(), Error> {
assert_next_token!(self, UnitStruct { name });
Ok(())
}
fn serialize_unit_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<(), Error> {
if self.tokens.first() == Some(&Token::Enum { name: name }) {
self.next_token();
assert_next_token!(self, Str(variant));
assert_next_token!(self, Unit);
} else {
assert_next_token!(self, UnitVariant { name, variant });
}
Ok(())
}
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
where
T: Serialize,
{
assert_next_token!(self, NewtypeStruct { name });
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<(), Error>
where
T: Serialize,
{
if self.tokens.first() == Some(&Token::Enum { name: name }) {
self.next_token();
assert_next_token!(self, Str(variant));
} else {
assert_next_token!(self, NewtypeVariant { name, variant });
}
value.serialize(self)
}
fn serialize_none(self) -> Result<(), Error> {
assert_next_token!(self, None);
Ok(())
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
where
T: Serialize,
{
assert_next_token!(self, Some);
value.serialize(self)
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
assert_next_token!(self, Seq { len });
Ok(self)
}
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
assert_next_token!(self, Tuple { len });
Ok(self)
}
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
assert_next_token!(self, TupleStruct { name, len });
Ok(self)
}
fn serialize_tuple_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Error> {
if self.tokens.first() == Some(&Token::Enum { name: name }) {
self.next_token();
assert_next_token!(self, Str(variant));
let len = Some(len);
assert_next_token!(self, Seq { len });
Ok(Variant {
ser: self,
end: Token::SeqEnd,
})
} else {
assert_next_token!(self, TupleVariant { name, variant, len });
Ok(Variant {
ser: self,
end: Token::TupleVariantEnd,
})
}
}
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
assert_next_token!(self, Map { len });
Ok(self)
}
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
assert_next_token!(self, Struct { name, len });
Ok(self)
}
fn serialize_struct_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant, Error> {
if self.tokens.first() == Some(&Token::Enum { name: name }) {
self.next_token();
assert_next_token!(self, Str(variant));
let len = Some(len);
assert_next_token!(self, Map { len });
Ok(Variant {
ser: self,
end: Token::MapEnd,
})
} else {
assert_next_token!(self, StructVariant { name, variant, len });
Ok(Variant {
ser: self,
end: Token::StructVariantEnd,
})
}
}
fn is_human_readable(&self) -> bool {
panic!(
"Types which have different human-readable and compact representations \
must explicitly mark their test cases with `serde_test::Configure`"
);
}
}
pub struct Variant<'s, 'a: 's> {
ser: &'s mut Serializer<'a>,
end: Token,
}
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where
T: Serialize,
{
value.serialize(&mut **self)
}
fn end(self) -> Result<(), Error> {
assert_next_token!(self, SeqEnd);
Ok(())
}
}
impl<'s, 'a> ser::SerializeTuple for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where
T: Serialize,
{
value.serialize(&mut **self)
}
fn end(self) -> Result<(), Error> {
assert_next_token!(self, TupleEnd);
Ok(())
}
}
impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where
T: Serialize,
{
value.serialize(&mut **self)
}
fn end(self) -> Result<(), Error> {
assert_next_token!(self, TupleStructEnd);
Ok(())
}
}
impl<'s, 'a> ser::SerializeTupleVariant for Variant<'s, 'a> {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where
T: Serialize,
{
value.serialize(&mut *self.ser)
}
fn end(self) -> Result<(), Error> {
match self.end {
Token::TupleVariantEnd => assert_next_token!(self.ser, TupleVariantEnd),
Token::SeqEnd => assert_next_token!(self.ser, SeqEnd),
_ => unreachable!(),
}
Ok(())
}
}
impl<'s, 'a> ser::SerializeMap for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
key.serialize(&mut **self)
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
value.serialize(&mut **self)
}
fn end(self) -> Result<(), Self::Error> {
assert_next_token!(self, MapEnd);
Ok(())
}
}
impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
key.serialize(&mut **self)?;
value.serialize(&mut **self)
}
fn end(self) -> Result<(), Self::Error> {
assert_next_token!(self, StructEnd);
Ok(())
}
}
impl<'s, 'a> ser::SerializeStructVariant for Variant<'s, 'a> {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
key.serialize(&mut *self.ser)?;
value.serialize(&mut *self.ser)
}
fn end(self) -> Result<(), Self::Error> {
match self.end {
Token::StructVariantEnd => assert_next_token!(self.ser, StructVariantEnd),
Token::MapEnd => assert_next_token!(self.ser, MapEnd),
_ => unreachable!(),
}
Ok(())
}
}
-519
View File
@@ -1,519 +0,0 @@
use std::fmt::{self, Debug, Display};
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Token {
/// A serialized `bool`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&true, &[Token::Bool(true)]);
/// ```
Bool(bool),
/// A serialized `i8`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0i8, &[Token::I8(0)]);
/// ```
I8(i8),
/// A serialized `i16`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0i16, &[Token::I16(0)]);
/// ```
I16(i16),
/// A serialized `i32`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0i32, &[Token::I32(0)]);
/// ```
I32(i32),
/// A serialized `i64`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0i64, &[Token::I64(0)]);
/// ```
I64(i64),
/// A serialized `u8`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0u8, &[Token::U8(0)]);
/// ```
U8(u8),
/// A serialized `u16`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0u16, &[Token::U16(0)]);
/// ```
U16(u16),
/// A serialized `u32`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0u32, &[Token::U32(0)]);
/// ```
U32(u32),
/// A serialized `u64`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0u64, &[Token::U64(0)]);
/// ```
U64(u64),
/// A serialized `f32`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0f32, &[Token::F32(0.0)]);
/// ```
F32(f32),
/// A serialized `f64`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&0f64, &[Token::F64(0.0)]);
/// ```
F64(f64),
/// A serialized `char`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&'\n', &[Token::Char('\n')]);
/// ```
Char(char),
/// A serialized `str`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let s = String::from("transient");
/// assert_tokens(&s, &[Token::Str("transient")]);
/// ```
Str(&'static str),
/// A borrowed `str`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let s: &str = "borrowed";
/// assert_tokens(&s, &[Token::BorrowedStr("borrowed")]);
/// ```
BorrowedStr(&'static str),
/// A serialized `String`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let s = String::from("owned");
/// assert_tokens(&s, &[Token::String("owned")]);
/// ```
String(&'static str),
/// A serialized `[u8]`
Bytes(&'static [u8]),
/// A borrowed `[u8]`.
BorrowedBytes(&'static [u8]),
/// A serialized `ByteBuf`
ByteBuf(&'static [u8]),
/// A serialized `Option<T>` containing none.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let opt = None::<char>;
/// assert_tokens(&opt, &[Token::None]);
/// ```
None,
/// The header to a serialized `Option<T>` containing some value.
///
/// The tokens of the value follow after this header.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let opt = Some('c');
/// assert_tokens(&opt, &[
/// Token::Some,
/// Token::Char('c'),
/// ]);
/// ```
Some,
/// A serialized `()`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// assert_tokens(&(), &[Token::Unit]);
/// ```
Unit,
/// A serialized unit struct of the given name.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct X;
///
/// assert_tokens(&X, &[Token::UnitStruct { name: "X" }]);
/// # }
/// ```
UnitStruct { name: &'static str },
/// A unit variant of an enum.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum E {
/// A,
/// }
///
/// let a = E::A;
/// assert_tokens(&a, &[Token::UnitVariant { name: "E", variant: "A" }]);
/// # }
/// ```
UnitVariant {
name: &'static str,
variant: &'static str,
},
/// The header to a serialized newtype struct of the given name.
///
/// After this header is the value contained in the newtype struct.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct N(String);
///
/// let n = N("newtype".to_owned());
/// assert_tokens(&n, &[
/// Token::NewtypeStruct { name: "N" },
/// Token::String("newtype"),
/// ]);
/// # }
/// ```
NewtypeStruct { name: &'static str },
/// The header to a newtype variant of an enum.
///
/// After this header is the value contained in the newtype variant.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum E {
/// B(u8),
/// }
///
/// let b = E::B(0);
/// assert_tokens(&b, &[
/// Token::NewtypeVariant { name: "E", variant: "B" },
/// Token::U8(0),
/// ]);
/// # }
/// ```
NewtypeVariant {
name: &'static str,
variant: &'static str,
},
/// The header to a sequence.
///
/// After this header are the elements of the sequence, followed by
/// `SeqEnd`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let vec = vec!['a', 'b', 'c'];
/// assert_tokens(&vec, &[
/// Token::Seq { len: Some(3) },
/// Token::Char('a'),
/// Token::Char('b'),
/// Token::Char('c'),
/// Token::SeqEnd,
/// ]);
/// ```
Seq { len: Option<usize> },
/// An indicator of the end of a sequence.
SeqEnd,
/// The header to a tuple.
///
/// After this header are the elements of the tuple, followed by `TupleEnd`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// let tuple = ('a', 100);
/// assert_tokens(&tuple, &[
/// Token::Tuple { len: 2 },
/// Token::Char('a'),
/// Token::I32(100),
/// Token::TupleEnd,
/// ]);
/// ```
Tuple { len: usize },
/// An indicator of the end of a tuple.
TupleEnd,
/// The header to a tuple struct.
///
/// After this header are the fields of the tuple struct, followed by
/// `TupleStructEnd`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct T(u8, u8);
///
/// let t = T(0, 0);
/// assert_tokens(&t, &[
/// Token::TupleStruct { name: "T", len: 2 },
/// Token::U8(0),
/// Token::U8(0),
/// Token::TupleStructEnd,
/// ]);
/// # }
/// ```
TupleStruct { name: &'static str, len: usize },
/// An indicator of the end of a tuple struct.
TupleStructEnd,
/// The header to a tuple variant of an enum.
///
/// After this header are the fields of the tuple variant, followed by
/// `TupleVariantEnd`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum E {
/// C(u8, u8),
/// }
///
/// let c = E::C(0, 0);
/// assert_tokens(&c, &[
/// Token::TupleVariant { name: "E", variant: "C", len: 2 },
/// Token::U8(0),
/// Token::U8(0),
/// Token::TupleVariantEnd,
/// ]);
/// # }
/// ```
TupleVariant {
name: &'static str,
variant: &'static str,
len: usize,
},
/// An indicator of the end of a tuple variant.
TupleVariantEnd,
/// The header to a map.
///
/// After this header are the entries of the map, followed by `MapEnd`.
///
/// ```edition2018
/// # use serde_test::{assert_tokens, Token};
/// #
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert('A', 65);
/// map.insert('Z', 90);
///
/// assert_tokens(&map, &[
/// Token::Map { len: Some(2) },
/// Token::Char('A'),
/// Token::I32(65),
/// Token::Char('Z'),
/// Token::I32(90),
/// Token::MapEnd,
/// ]);
/// ```
Map { len: Option<usize> },
/// An indicator of the end of a map.
MapEnd,
/// The header of a struct.
///
/// After this header are the fields of the struct, followed by `StructEnd`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct S {
/// a: u8,
/// b: u8,
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// # }
/// ```
Struct { name: &'static str, len: usize },
/// An indicator of the end of a struct.
StructEnd,
/// The header of a struct variant of an enum.
///
/// After this header are the fields of the struct variant, followed by
/// `StructVariantEnd`.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum E {
/// D { d: u8 },
/// }
///
/// let d = E::D { d: 0 };
/// assert_tokens(&d, &[
/// Token::StructVariant { name: "E", variant: "D", len: 1 },
/// Token::Str("d"),
/// Token::U8(0),
/// Token::StructVariantEnd,
/// ]);
/// # }
/// ```
StructVariant {
name: &'static str,
variant: &'static str,
len: usize,
},
/// An indicator of the end of a struct variant.
StructVariantEnd,
/// The header to an enum of the given name.
///
/// ```edition2018
/// # use serde::{Serialize, Deserialize};
/// # use serde_test::{assert_tokens, Token};
/// #
/// # fn main() {
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum E {
/// A,
/// B(u8),
/// C(u8, u8),
/// D { d: u8 },
/// }
///
/// let a = E::A;
/// assert_tokens(&a, &[
/// Token::Enum { name: "E" },
/// Token::Str("A"),
/// Token::Unit,
/// ]);
///
/// let b = E::B(0);
/// assert_tokens(&b, &[
/// Token::Enum { name: "E" },
/// Token::Str("B"),
/// Token::U8(0),
/// ]);
///
/// let c = E::C(0, 0);
/// assert_tokens(&c, &[
/// Token::Enum { name: "E" },
/// Token::Str("C"),
/// Token::Seq { len: Some(2) },
/// Token::U8(0),
/// Token::U8(0),
/// Token::SeqEnd,
/// ]);
///
/// let d = E::D { d: 0 };
/// assert_tokens(&d, &[
/// Token::Enum { name: "E" },
/// Token::Str("D"),
/// Token::Map { len: Some(1) },
/// Token::Str("d"),
/// Token::U8(0),
/// Token::MapEnd,
/// ]);
/// # }
/// ```
Enum { name: &'static str },
}
impl Display for Token {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(self, formatter)
}
}
+3 -3
View File
@@ -12,10 +12,10 @@ unstable = ["serde/unstable"]
serde = { path = "../serde" }
[dev-dependencies]
automod = "1.0"
automod = "1.0.1"
fnv = "1.0"
rustversion = "1.0"
serde = { path = "../serde", features = ["rc", "derive"] }
serde = { path = "../serde", features = ["rc"] }
serde_derive = { path = "../serde_derive", features = ["deserialize_in_place"] }
serde_test = { path = "../serde_test" }
serde_test = "1.0.176"
trybuild = { version = "1.0.66", features = ["diff"] }
+2 -1
View File
@@ -7,6 +7,7 @@ publish = false
[dependencies]
libc = { version = "0.2", default-features = false }
serde = { path = "../../serde", default-features = false, features = ["derive"] }
serde = { path = "../../serde", default-features = false }
serde_derive = { path = "../../serde_derive" }
[workspace]
+2 -1
View File
@@ -1,3 +1,4 @@
#![allow(internal_features)]
#![feature(lang_items, start)]
#![no_std]
@@ -19,7 +20,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
//////////////////////////////////////////////////////////////////////////////
use serde::{Serialize, Deserialize};
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Unit;
+1 -1
View File
@@ -1,4 +1,4 @@
use serde::Deserialize;
use serde_derive::Deserialize;
#[derive(Deserialize)]
pub struct Nested;
+11
View File
@@ -0,0 +1,11 @@
use serde_derive::Deserialize;
macro_rules! bug {
($serde_path:literal) => {
#[derive(Deserialize)]
#[serde(crate = $serde_path)]
pub struct Struct;
};
}
bug!("serde");
+5
View File
@@ -0,0 +1,5 @@
use serde_derive::Serialize;
#[derive(Serialize)]
#[serde()]
pub struct S;
File diff suppressed because it is too large Load Diff
+18 -15
View File
@@ -4,9 +4,10 @@
clippy::used_underscore_binding
)]
use serde::{Deserialize, Deserializer};
use serde::de::value::{BorrowedStrDeserializer, MapDeserializer};
use serde::de::{Deserialize, Deserializer, IntoDeserializer};
use serde_derive::Deserialize;
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
use std::borrow::Cow;
#[test]
@@ -130,20 +131,22 @@ fn test_cow() {
borrowed: Cow<'b, str>,
}
let tokens = &[
Token::Struct {
name: "Cows",
len: 2,
},
Token::Str("copied"),
Token::BorrowedStr("copied"),
Token::Str("borrowed"),
Token::BorrowedStr("borrowed"),
Token::StructEnd,
];
struct BorrowedStr(&'static str);
let mut de = serde_test::Deserializer::new(tokens);
let cows = Cows::deserialize(&mut de).unwrap();
impl<'de> IntoDeserializer<'de> for BorrowedStr {
type Deserializer = BorrowedStrDeserializer<'de, serde::de::value::Error>;
fn into_deserializer(self) -> Self::Deserializer {
BorrowedStrDeserializer::new(self.0)
}
}
let de = MapDeserializer::new(IntoIterator::into_iter([
("copied", BorrowedStr("copied")),
("borrowed", BorrowedStr("borrowed")),
]));
let cows = Cows::deserialize(de).unwrap();
match cows.copied {
Cow::Owned(ref s) if s == "copied" => {}
+38 -31
View File
@@ -10,10 +10,15 @@
)]
#![cfg_attr(feature = "unstable", feature(never_type))]
use fnv::FnvHasher;
use serde::de::{Deserialize, DeserializeOwned, Deserializer, IntoDeserializer};
use serde_derive::Deserialize;
use serde_test::{assert_de_tokens, Configure, Token};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::default::Default;
use std::ffi::{CStr, CString, OsString};
use std::fmt::Debug;
use std::iter;
use std::net;
use std::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
@@ -26,16 +31,10 @@ use std::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
AtomicUsize, Ordering,
};
use std::sync::{Arc, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH};
#[cfg(target_arch = "x86_64")]
use std::sync::atomic::{AtomicI64, AtomicU64};
use fnv::FnvHasher;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer};
use serde_test::{assert_de_tokens, Configure, Token};
use std::sync::{Arc, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH};
#[macro_use]
mod macros;
@@ -45,6 +44,9 @@ mod macros;
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
struct UnitStruct;
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
struct GenericUnitStruct<const N: u8>;
#[derive(PartialEq, Debug, Deserialize)]
struct NewtypeStruct(i32);
@@ -199,12 +201,11 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
]
.into_iter()
.chain(ignorable_tokens.iter().copied())
.chain(vec![Token::MapEnd].into_iter())
.chain(iter::once(Token::MapEnd))
.collect();
let mut de = serde_test::Deserializer::new(&concated_tokens);
let base = IgnoreBase::deserialize(&mut de).unwrap();
assert_eq!(base, IgnoreBase { a: 1 });
let expected = IgnoreBase { a: 1 };
assert_de_tokens(&expected, &concated_tokens);
}
//////////////////////////////////////////////////////////////////////////
@@ -883,6 +884,17 @@ fn test_unit_struct() {
test(UnitStruct, &[Token::UnitStruct { name: "UnitStruct" }]);
}
#[test]
fn test_generic_unit_struct() {
test(GenericUnitStruct::<8>, &[Token::Unit]);
test(
GenericUnitStruct::<8>,
&[Token::UnitStruct {
name: "GenericUnitStruct",
}],
);
}
#[test]
fn test_newtype_struct() {
test(
@@ -2245,39 +2257,34 @@ fn test_cstr() {
#[test]
fn test_atomics() {
fn test<L, A, T>(load: L, val: T, token: Token)
fn test<L, A, T>(load: L, val: T)
where
L: Fn(&A, Ordering) -> T,
A: DeserializeOwned,
T: PartialEq + Debug,
T: PartialEq + Debug + Copy + for<'de> IntoDeserializer<'de>,
{
let tokens = &[token];
let mut de = serde_test::Deserializer::new(tokens);
match A::deserialize(&mut de) {
match A::deserialize(val.into_deserializer()) {
Ok(v) => {
let loaded = load(&v, Ordering::Relaxed);
assert_eq!(val, loaded);
}
Err(e) => panic!("tokens failed to deserialize: {}", e),
};
if de.remaining() > 0 {
panic!("{} remaining tokens", de.remaining());
}
}
test(AtomicBool::load, true, Token::Bool(true));
test(AtomicI8::load, -127, Token::I8(-127i8));
test(AtomicI16::load, -510, Token::I16(-510i16));
test(AtomicI32::load, -131072, Token::I32(-131072i32));
test(AtomicIsize::load, -131072isize, Token::I32(-131072));
test(AtomicU8::load, 127, Token::U8(127u8));
test(AtomicU16::load, 510u16, Token::U16(510u16));
test(AtomicU32::load, 131072u32, Token::U32(131072u32));
test(AtomicUsize::load, 131072usize, Token::U32(131072));
test(AtomicBool::load, true);
test(AtomicI8::load, -127i8);
test(AtomicI16::load, -510i16);
test(AtomicI32::load, -131072i32);
test(AtomicIsize::load, -131072isize);
test(AtomicU8::load, 127u8);
test(AtomicU16::load, 510u16);
test(AtomicU32::load, 131072u32);
test(AtomicUsize::load, 131072usize);
#[cfg(target_arch = "x86_64")]
{
test(AtomicI64::load, -8589934592, Token::I64(-8589934592));
test(AtomicU64::load, 8589934592u64, Token::U64(8589934592));
test(AtomicI64::load, -8589934592i64);
test(AtomicU64::load, 8589934592u64);
}
}
+2 -2
View File
@@ -5,8 +5,8 @@
)]
#![cfg_attr(feature = "unstable", feature(never_type))]
use serde::de::IntoDeserializer;
use serde::Deserialize;
use serde::de::{Deserialize, IntoDeserializer};
use serde_derive::Deserialize;
use serde_test::{assert_de_tokens_error, Token};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::{CStr, CString};
+14 -5
View File
@@ -20,9 +20,9 @@
clippy::type_repetition_in_bounds
)]
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Deserialize, DeserializeOwned, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
use std::borrow::Cow;
use std::marker::PhantomData;
use std::option::Option as StdOption;
@@ -404,7 +404,7 @@ fn test_gen() {
}
mod vis {
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
pub struct S;
@@ -636,7 +636,7 @@ fn test_gen() {
mod restricted {
mod inner {
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Restricted {
@@ -662,6 +662,7 @@ fn test_gen() {
#[derive(Deserialize)]
struct ImplicitlyBorrowedOption<'a> {
#[allow(dead_code)]
option: std::option::Option<&'a str>,
}
@@ -692,7 +693,9 @@ fn test_gen() {
#[derive(Deserialize)]
struct RelObject<'a> {
#[allow(dead_code)]
ty: &'a str,
#[allow(dead_code)]
id: String,
}
@@ -736,6 +739,7 @@ fn test_gen() {
($field:ty) => {
#[derive(Deserialize)]
struct MacroRules<'a> {
#[allow(dead_code)]
field: $field,
}
};
@@ -752,6 +756,7 @@ fn test_gen() {
#[derive(Deserialize)]
struct BorrowLifetimeInsideMacro<'a> {
#[serde(borrow = "'a")]
#[allow(dead_code)]
f: mac!(Cow<'a, str>),
}
@@ -760,6 +765,10 @@ fn test_gen() {
#[serde(serialize_with = "vec_first_element")]
vec: Vec<Self>,
}
#[derive(Deserialize)]
#[serde(bound(deserialize = "[&'de str; N]: Copy"))]
struct GenericUnitStruct<const N: usize>;
}
//////////////////////////////////////////////////////////////////////////
+159 -59
View File
@@ -2,87 +2,187 @@
#![allow(clippy::derive_partial_eq_without_eq)]
use serde::Deserialize;
use serde_test::{assert_de_tokens, Token};
use serde_derive::Deserialize;
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
mod variant_identifier {
use super::*;
#[test]
fn test_variant_identifier() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(variant_identifier)]
enum V {
Aaa,
#[serde(alias = "Ccc", alias = "Ddd")]
Bbb,
}
assert_de_tokens(&V::Aaa, &[Token::U8(0)]);
assert_de_tokens(&V::Aaa, &[Token::U16(0)]);
assert_de_tokens(&V::Aaa, &[Token::U32(0)]);
assert_de_tokens(&V::Aaa, &[Token::U64(0)]);
assert_de_tokens(&V::Aaa, &[Token::Str("Aaa")]);
assert_de_tokens(&V::Aaa, &[Token::Bytes(b"Aaa")]);
#[test]
fn variant1() {
assert_de_tokens(&V::Aaa, &[Token::U8(0)]);
assert_de_tokens(&V::Aaa, &[Token::U16(0)]);
assert_de_tokens(&V::Aaa, &[Token::U32(0)]);
assert_de_tokens(&V::Aaa, &[Token::U64(0)]);
assert_de_tokens(&V::Aaa, &[Token::Str("Aaa")]);
assert_de_tokens(&V::Aaa, &[Token::Bytes(b"Aaa")]);
}
#[test]
fn aliases() {
assert_de_tokens(&V::Bbb, &[Token::U8(1)]);
assert_de_tokens(&V::Bbb, &[Token::U16(1)]);
assert_de_tokens(&V::Bbb, &[Token::U32(1)]);
assert_de_tokens(&V::Bbb, &[Token::U64(1)]);
assert_de_tokens(&V::Bbb, &[Token::Str("Bbb")]);
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Bbb")]);
assert_de_tokens(&V::Bbb, &[Token::Str("Ccc")]);
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Ccc")]);
assert_de_tokens(&V::Bbb, &[Token::Str("Ddd")]);
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Ddd")]);
}
#[test]
fn unknown() {
assert_de_tokens_error::<V>(
&[Token::U8(42)],
"invalid value: integer `42`, expected variant index 0 <= i < 2",
);
assert_de_tokens_error::<V>(
&[Token::U16(42)],
"invalid value: integer `42`, expected variant index 0 <= i < 2",
);
assert_de_tokens_error::<V>(
&[Token::U32(42)],
"invalid value: integer `42`, expected variant index 0 <= i < 2",
);
assert_de_tokens_error::<V>(
&[Token::U64(42)],
"invalid value: integer `42`, expected variant index 0 <= i < 2",
);
assert_de_tokens_error::<V>(
&[Token::Str("Unknown")],
"unknown variant `Unknown`, expected one of `Aaa`, `Bbb`, `Ccc`, `Ddd`",
);
assert_de_tokens_error::<V>(
&[Token::Bytes(b"Unknown")],
"unknown variant `Unknown`, expected one of `Aaa`, `Bbb`, `Ccc`, `Ddd`",
);
}
}
#[test]
fn test_field_identifier() {
mod field_identifier {
use super::*;
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F {
Aaa,
#[serde(alias = "ccc", alias = "ddd")]
Bbb,
}
assert_de_tokens(&F::Aaa, &[Token::U8(0)]);
assert_de_tokens(&F::Aaa, &[Token::U16(0)]);
assert_de_tokens(&F::Aaa, &[Token::U32(0)]);
assert_de_tokens(&F::Aaa, &[Token::U64(0)]);
assert_de_tokens(&F::Aaa, &[Token::Str("aaa")]);
assert_de_tokens(&F::Aaa, &[Token::Bytes(b"aaa")]);
}
#[test]
fn test_unit_fallthrough() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F {
Aaa,
Bbb,
#[serde(other)]
Other,
#[test]
fn field1() {
assert_de_tokens(&F::Aaa, &[Token::U8(0)]);
assert_de_tokens(&F::Aaa, &[Token::U16(0)]);
assert_de_tokens(&F::Aaa, &[Token::U32(0)]);
assert_de_tokens(&F::Aaa, &[Token::U64(0)]);
assert_de_tokens(&F::Aaa, &[Token::Str("aaa")]);
assert_de_tokens(&F::Aaa, &[Token::Bytes(b"aaa")]);
}
assert_de_tokens(&F::Other, &[Token::U8(42)]);
assert_de_tokens(&F::Other, &[Token::U16(42)]);
assert_de_tokens(&F::Other, &[Token::U32(42)]);
assert_de_tokens(&F::Other, &[Token::U64(42)]);
assert_de_tokens(&F::Other, &[Token::Str("x")]);
}
#[test]
fn aliases() {
assert_de_tokens(&F::Bbb, &[Token::U8(1)]);
assert_de_tokens(&F::Bbb, &[Token::U16(1)]);
assert_de_tokens(&F::Bbb, &[Token::U32(1)]);
assert_de_tokens(&F::Bbb, &[Token::U64(1)]);
#[test]
fn test_newtype_fallthrough() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F {
Aaa,
Bbb,
Other(String),
assert_de_tokens(&F::Bbb, &[Token::Str("bbb")]);
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"bbb")]);
assert_de_tokens(&F::Bbb, &[Token::Str("ccc")]);
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"ccc")]);
assert_de_tokens(&F::Bbb, &[Token::Str("ddd")]);
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"ddd")]);
}
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
}
#[test]
fn test_newtype_fallthrough_generic() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F<T> {
Aaa,
Bbb,
Other(T),
#[test]
fn unknown() {
assert_de_tokens_error::<F>(
&[Token::U8(42)],
"invalid value: integer `42`, expected field index 0 <= i < 2",
);
assert_de_tokens_error::<F>(
&[Token::U16(42)],
"invalid value: integer `42`, expected field index 0 <= i < 2",
);
assert_de_tokens_error::<F>(
&[Token::U32(42)],
"invalid value: integer `42`, expected field index 0 <= i < 2",
);
assert_de_tokens_error::<F>(
&[Token::U64(42)],
"invalid value: integer `42`, expected field index 0 <= i < 2",
);
assert_de_tokens_error::<F>(
&[Token::Str("unknown")],
"unknown field `unknown`, expected one of `aaa`, `bbb`, `ccc`, `ddd`",
);
assert_de_tokens_error::<F>(
&[Token::Bytes(b"unknown")],
"unknown field `unknown`, expected one of `aaa`, `bbb`, `ccc`, `ddd`",
);
}
assert_de_tokens(&F::Other(42u8), &[Token::U8(42)]);
assert_de_tokens(&F::Other(42u16), &[Token::U16(42)]);
assert_de_tokens(&F::Other(42u32), &[Token::U32(42)]);
assert_de_tokens(&F::Other(42u64), &[Token::U64(42)]);
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
#[test]
fn unit_fallthrough() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F {
Aaa,
Bbb,
#[serde(other)]
Other,
}
assert_de_tokens(&F::Other, &[Token::U8(42)]);
assert_de_tokens(&F::Other, &[Token::U16(42)]);
assert_de_tokens(&F::Other, &[Token::U32(42)]);
assert_de_tokens(&F::Other, &[Token::U64(42)]);
assert_de_tokens(&F::Other, &[Token::Str("x")]);
}
#[test]
fn newtype_fallthrough() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F {
Aaa,
Bbb,
Other(String),
}
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
}
#[test]
fn newtype_fallthrough_generic() {
#[derive(Deserialize, Debug, PartialEq)]
#[serde(field_identifier, rename_all = "snake_case")]
enum F<T> {
Aaa,
Bbb,
Other(T),
}
assert_de_tokens(&F::Other(42u8), &[Token::U8(42)]);
assert_de_tokens(&F::Other(42u16), &[Token::U16(42)]);
assert_de_tokens(&F::Other(42u32), &[Token::U32(42)]);
assert_de_tokens(&F::Other(42u64), &[Token::U64(42)]);
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
}
}
+4 -2
View File
@@ -2,9 +2,11 @@
use serde::de::value::{Error, MapDeserializer, SeqDeserializer};
use serde::de::{
DeserializeSeed, EnumAccess, IgnoredAny, IntoDeserializer, VariantAccess, Visitor,
Deserialize, DeserializeSeed, Deserializer, EnumAccess, IgnoredAny, IntoDeserializer,
VariantAccess, Visitor,
};
use serde::{forward_to_deserialize_any, Deserialize, Deserializer};
use serde::forward_to_deserialize_any;
use serde_derive::Deserialize;
#[derive(PartialEq, Debug, Deserialize)]
enum Target {
+211 -18
View File
@@ -8,11 +8,10 @@
mod bytes;
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use serde_test::{
assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_tokens, Token,
};
use std::collections::BTreeMap;
use std::marker::PhantomData;
@@ -473,7 +472,10 @@ fn test_adjacently_tagged_newtype_struct() {
},
Token::U32(5),
Token::Str("t"),
Token::Str("Newtype"),
Token::UnitVariant {
name: "E",
variant: "Newtype",
},
Token::StructEnd,
],
);
@@ -1067,7 +1069,10 @@ fn test_adjacently_tagged_enum() {
len: 1,
},
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::StructEnd,
],
);
@@ -1081,7 +1086,10 @@ fn test_adjacently_tagged_enum() {
len: 2,
},
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::StructEnd,
],
);
@@ -1095,7 +1103,10 @@ fn test_adjacently_tagged_enum() {
len: 2,
},
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::Str("c"),
Token::Unit,
Token::StructEnd,
@@ -1113,7 +1124,10 @@ fn test_adjacently_tagged_enum() {
Token::Str("c"),
Token::Unit,
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::StructEnd,
],
);
@@ -1129,7 +1143,10 @@ fn test_adjacently_tagged_enum() {
Token::Str("f"),
Token::Unit,
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::Str("g"),
Token::Unit,
Token::Str("c"),
@@ -1149,7 +1166,10 @@ fn test_adjacently_tagged_enum() {
len: 2,
},
Token::Str("t"),
Token::Str("Newtype"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::Str("c"),
Token::U8(1),
Token::StructEnd,
@@ -1167,7 +1187,10 @@ fn test_adjacently_tagged_enum() {
Token::Str("c"),
Token::U8(1),
Token::Str("t"),
Token::Str("Newtype"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::StructEnd,
],
);
@@ -1181,7 +1204,10 @@ fn test_adjacently_tagged_enum() {
len: 1,
},
Token::Str("t"),
Token::Str("Newtype"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::StructEnd,
],
);
@@ -1195,7 +1221,10 @@ fn test_adjacently_tagged_enum() {
len: 2,
},
Token::Str("t"),
Token::Str("Tuple"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Tuple",
},
Token::Str("c"),
Token::Tuple { len: 2 },
Token::U8(1),
@@ -1219,7 +1248,10 @@ fn test_adjacently_tagged_enum() {
Token::U8(1),
Token::TupleEnd,
Token::Str("t"),
Token::Str("Tuple"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Tuple",
},
Token::StructEnd,
],
);
@@ -1233,7 +1265,10 @@ fn test_adjacently_tagged_enum() {
len: 2,
},
Token::Str("t"),
Token::Str("Struct"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Struct",
},
Token::Str("c"),
Token::Struct {
name: "Struct",
@@ -1263,7 +1298,48 @@ fn test_adjacently_tagged_enum() {
Token::U8(1),
Token::StructEnd,
Token::Str("t"),
Token::Str("Struct"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Struct",
},
Token::StructEnd,
],
);
// integer field keys
assert_de_tokens(
&AdjacentlyTagged::Newtype::<u8>(1),
&[
Token::Struct {
name: "AdjacentlyTagged",
len: 2,
},
Token::U64(1), // content field
Token::U8(1),
Token::U64(0), // tag field
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::StructEnd,
],
);
// byte-array field keys
assert_de_tokens(
&AdjacentlyTagged::Newtype::<u8>(1),
&[
Token::Struct {
name: "AdjacentlyTagged",
len: 2,
},
Token::Bytes(b"c"),
Token::U8(1),
Token::Bytes(b"t"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::StructEnd,
],
);
@@ -1285,7 +1361,10 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
len: 2,
},
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::Str("c"),
Token::Unit,
Token::StructEnd,
@@ -1299,7 +1378,10 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
len: 2,
},
Token::Str("t"),
Token::Str("Unit"),
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::Str("c"),
Token::Unit,
Token::Str("h"),
@@ -1330,6 +1412,35 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
],
r#"invalid value: string "h", expected "t" or "c""#,
);
assert_de_tokens_error::<AdjacentlyTagged>(
&[
Token::Struct {
name: "AdjacentlyTagged",
len: 2,
},
Token::U64(0), // tag field
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::U64(3),
],
r#"invalid value: integer `3`, expected "t" or "c""#,
);
assert_de_tokens_error::<AdjacentlyTagged>(
&[
Token::Struct {
name: "AdjacentlyTagged",
len: 2,
},
Token::Bytes(b"c"),
Token::Unit,
Token::Bytes(b"h"),
],
r#"invalid value: byte array, expected "t" or "c""#,
);
}
#[test]
@@ -1372,6 +1483,9 @@ fn test_enum_in_internally_tagged_enum() {
],
);
// Reaches crate::private::de::content::VariantDeserializer::tuple_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
@@ -1390,6 +1504,9 @@ fn test_enum_in_internally_tagged_enum() {
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Map case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
@@ -1407,6 +1524,23 @@ fn test_enum_in_internally_tagged_enum() {
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_de_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Struct"),
Token::Seq { len: Some(1) },
Token::U8(1), // f
Token::SeqEnd,
Token::MapEnd,
],
);
}
#[test]
@@ -1485,7 +1619,10 @@ fn test_internally_tagged_struct_with_flattened_field() {
Token::Str("tag_struct"),
Token::Str("Struct"),
Token::Str("tag_enum"),
Token::Str("A"),
Token::UnitVariant {
name: "Enum",
variant: "A",
},
Token::Str("content"),
Token::U64(0),
Token::MapEnd,
@@ -1866,6 +2003,62 @@ fn test_rename_all() {
);
}
#[test]
fn test_rename_all_fields() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all_fields = "kebab-case")]
enum E {
V1,
V2(bool),
V3 {
a_field: bool,
another_field: bool,
#[serde(rename = "last-field")]
yet_another_field: bool,
},
#[serde(rename_all = "snake_case")]
V4 {
a_field: bool,
},
}
assert_tokens(
&E::V3 {
a_field: true,
another_field: true,
yet_another_field: true,
},
&[
Token::StructVariant {
name: "E",
variant: "V3",
len: 3,
},
Token::Str("a-field"),
Token::Bool(true),
Token::Str("another-field"),
Token::Bool(true),
Token::Str("last-field"),
Token::Bool(true),
Token::StructVariantEnd,
],
);
assert_tokens(
&E::V4 { a_field: true },
&[
Token::StructVariant {
name: "E",
variant: "V4",
len: 1,
},
Token::Str("a_field"),
Token::Bool(true),
Token::StructVariantEnd,
],
);
}
#[test]
fn test_untagged_newtype_variant_containing_unit_struct_not_map() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
+21 -1
View File
@@ -1,6 +1,6 @@
#![allow(clippy::redundant_field_names)]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
mod remote {
pub struct Unit;
@@ -125,6 +125,9 @@ struct Test {
#[serde(with = "EnumConcrete")]
enum_concrete: remote::EnumGeneric<u8>,
#[serde(with = "ErrorKindDef")]
io_error_kind: ErrorKind,
}
#[derive(Serialize, Deserialize)]
@@ -197,6 +200,23 @@ enum EnumConcrete {
Variant(u8),
}
#[derive(Debug)]
enum ErrorKind {
NotFound,
PermissionDenied,
#[allow(dead_code)]
ConnectionRefused,
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "ErrorKind")]
#[non_exhaustive]
enum ErrorKindDef {
NotFound,
PermissionDenied,
// ...
}
impl From<PrimitivePrivDef> for remote::PrimitivePriv {
fn from(def: PrimitivePrivDef) -> Self {
remote::PrimitivePriv::new(def.0)
-1
View File
@@ -1,5 +1,4 @@
use serde_test::{assert_tokens, Configure, Token};
use std::net;
#[macro_use]
+1 -1
View File
@@ -1,6 +1,6 @@
#![allow(clippy::used_underscore_binding)]
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
#[test]
fn test_self() {
+7 -9
View File
@@ -1,6 +1,9 @@
#![allow(clippy::derive_partial_eq_without_eq, clippy::unreadable_literal)]
#![cfg_attr(feature = "unstable", feature(never_type))]
use fnv::FnvHasher;
use serde_derive::Serialize;
use serde_test::{assert_ser_tokens, assert_ser_tokens_error, Configure, Token};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::CString;
@@ -9,21 +12,16 @@ use std::num::Wrapping;
use std::ops::Bound;
use std::path::{Path, PathBuf};
use std::rc::{Rc, Weak as RcWeak};
#[cfg(unix)]
use std::str;
use std::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
AtomicUsize,
};
use std::sync::{Arc, Mutex, RwLock, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH};
#[cfg(unix)]
use std::str;
#[cfg(target_arch = "x86_64")]
use std::sync::atomic::{AtomicI64, AtomicU64};
use fnv::FnvHasher;
use serde::Serialize;
use serde_test::{assert_ser_tokens, assert_ser_tokens_error, Configure, Token};
use std::sync::{Arc, Mutex, RwLock, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH};
#[macro_use]
mod macros;
+1 -1
View File
@@ -5,7 +5,7 @@
#[test]
fn test_gen_custom_serde() {
#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(crate = "fake_serde")]
struct Foo;
+2 -2
View File
@@ -1,8 +1,8 @@
#![allow(clippy::derive_partial_eq_without_eq, clippy::similar_names)]
use serde::de::value::{self, MapAccessDeserializer};
use serde::de::{IntoDeserializer, MapAccess, Visitor};
use serde::{Deserialize, Deserializer};
use serde::de::{Deserialize, Deserializer, IntoDeserializer, MapAccess, Visitor};
use serde_derive::Deserialize;
use serde_test::{assert_de_tokens, Token};
use std::fmt;
@@ -1,5 +1,4 @@
use serde_derive::Serialize;
use std::collections::HashMap;
#[derive(Serialize)]
@@ -1,5 +1,5 @@
error: #[serde(flatten)] cannot be used on newtype structs
--> tests/ui/conflict/flatten-newtype-struct.rs:6:12
--> tests/ui/conflict/flatten-newtype-struct.rs:5:12
|
6 | struct Foo(#[serde(flatten)] HashMap<String, String>);
5 | struct Foo(#[serde(flatten)] HashMap<String, String>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1,5 +1,4 @@
use serde_derive::Serialize;
use std::collections::HashMap;
#[derive(Serialize)]
@@ -1,5 +1,5 @@
error: #[serde(flatten)] cannot be used on tuple structs
--> tests/ui/conflict/flatten-tuple-struct.rs:6:17
--> tests/ui/conflict/flatten-tuple-struct.rs:5:17
|
6 | struct Foo(u32, #[serde(flatten)] HashMap<String, String>);
5 | struct Foo(u32, #[serde(flatten)] HashMap<String, String>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1,5 +1,5 @@
error: #[serde(default)] can only be used on structs with named fields
--> tests/ui/default-attribute/enum.rs:5:1
error: #[serde(default)] can only be used on structs
--> tests/ui/default-attribute/enum.rs:4:9
|
5 | enum E {
| ^^^^
4 | #[serde(default)]
| ^^^^^^^
@@ -1,5 +1,5 @@
error: #[serde(default = "...")] can only be used on structs with named fields
--> tests/ui/default-attribute/enum_path.rs:5:1
error: #[serde(default = "...")] can only be used on structs
--> tests/ui/default-attribute/enum_path.rs:4:9
|
5 | enum E {
| ^^^^
4 | #[serde(default = "default_e")]
| ^^^^^^^^^^^^^^^^^^^^^
@@ -1,7 +0,0 @@
use serde_derive::Deserialize;
#[derive(Deserialize)]
#[serde(default)]
struct T(u8, u8);
fn main() {}
@@ -1,5 +0,0 @@
error: #[serde(default)] can only be used on structs with named fields
--> tests/ui/default-attribute/nameless_struct_fields.rs:5:9
|
5 | struct T(u8, u8);
| ^^^^^^^^
@@ -1,7 +0,0 @@
use serde_derive::Deserialize;
#[derive(Deserialize)]
#[serde(default = "default_t")]
struct T(u8, u8);
fn main() {}
@@ -1,5 +0,0 @@
error: #[serde(default = "...")] can only be used on structs with named fields
--> tests/ui/default-attribute/nameless_struct_fields_path.rs:5:9
|
5 | struct T(u8, u8);
| ^^^^^^^^
@@ -0,0 +1,47 @@
use serde_derive::Deserialize;
// No errors expected.
#[derive(Deserialize)]
struct T0(u8, u8);
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
#[derive(Deserialize)]
struct T1(u8, #[serde(default)] u8);
// ERROR: The first field can get default value only if sequence is empty, but
// that mean that all other fields cannot be deserialized without errors.
#[derive(Deserialize)]
struct T2(#[serde(default)] u8, u8, u8);
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
// - If no fields are provided, both get default value.
#[derive(Deserialize)]
struct T3(#[serde(default)] u8, #[serde(default)] u8);
////////////////////////////////////////////////////////////////////////////////
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T4(u8, u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T5(#[serde(default)] u8, u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T6(u8, #[serde(default)] u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T7(#[serde(default)] u8, #[serde(default)] u8);
fn main() {}
@@ -0,0 +1,11 @@
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:16:33
|
16 | struct T2(#[serde(default)] u8, u8, u8);
| ^^
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct.rs:16:37
|
16 | struct T2(#[serde(default)] u8, u8, u8);
| ^^
@@ -0,0 +1,76 @@
use serde_derive::Deserialize;
fn d<T>() -> T {
unimplemented!()
}
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
#[derive(Deserialize)]
struct T1(u8, #[serde(default = "d")] u8);
// ERROR: The first field can get default value only if sequence is empty, but
// that mean that all other fields cannot be deserialized without errors.
#[derive(Deserialize)]
struct T2(#[serde(default = "d")] u8, u8, u8);
// No errors expected:
// - If both fields are provided, both get value from data.
// - If only one field is provided, the second gets default value.
// - If no fields are provided, both get default value.
#[derive(Deserialize)]
struct T3(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
////////////////////////////////////////////////////////////////////////////////
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T1D(#[serde(default = "d")] u8, u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T2D(u8, #[serde(default = "d")] u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize, Default)]
#[serde(default)]
struct T3D(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
////////////////////////////////////////////////////////////////////////////////
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T1Path(#[serde(default)] u8, u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T2Path(u8, #[serde(default)] u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T3Path(#[serde(default)] u8, #[serde(default)] u8);
////////////////////////////////////////////////////////////////////////////////
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T1PathD(#[serde(default = "d")] u8, u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T2PathD(u8, #[serde(default = "d")] u8);
// No errors expected -- missing fields get default values.
#[derive(Deserialize)]
#[serde(default = "d")]
struct T3PathD(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
fn main() {}
@@ -0,0 +1,11 @@
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:16:39
|
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
| ^^
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
--> tests/ui/default-attribute/tuple_struct_path.rs:16:43
|
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
| ^^
@@ -0,0 +1,10 @@
use serde_derive::Serialize;
#[derive(Serialize)]
enum E {
#[serde(untagged)]
A(u8),
B(String),
}
fn main() {}

Some files were not shown because too many files have changed in this diff Show More