serde-1.0.217/.cargo_vcs_info.json0000644000000001430000000000100123040ustar { "git": { "sha1": "930401b0dd58a809fce34da091b8aa3d6083cb33" }, "path_in_vcs": "serde" }serde-1.0.217/Cargo.lock0000644000000043570000000000100102720ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "proc-macro2" version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "unicode-ident 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "quote" version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde" version = "1.0.217" dependencies = [ "serde_derive 1.0.217 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive" version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", "syn 2.0.92 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" version = "2.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-ident 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum proc-macro2 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)" = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" "checksum quote 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" "checksum serde_derive 1.0.217 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" "checksum syn 2.0.92 (registry+https://github.com/rust-lang/crates.io-index)" = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126" "checksum unicode-ident 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" serde-1.0.217/Cargo.toml0000644000000032750000000000100103130ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" rust-version = "1.31" name = "serde" version = "1.0.217" authors = [ "Erick Tryzelaar ", "David Tolnay ", ] build = "build.rs" autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A generic serialization/deserialization framework" homepage = "https://serde.rs" documentation = "https://docs.rs/serde" readme = "crates-io.md" keywords = [ "serde", "serialization", "no_std", ] categories = [ "encoding", "no-std", "no-std::no-alloc", ] license = "MIT OR Apache-2.0" repository = "https://github.com/serde-rs/serde" [package.metadata.docs.rs] features = [ "derive", "rc", "unstable", ] rustdoc-args = ["--generate-link-to-definition"] targets = ["x86_64-unknown-linux-gnu"] [package.metadata.playground] features = [ "derive", "rc", ] [features] alloc = [] default = ["std"] derive = ["serde_derive"] rc = [] std = [] unstable = [] [lib] name = "serde" path = "src/lib.rs" doc-scrape-examples = false [dependencies.serde_derive] version = "1" optional = true [dev-dependencies.serde_derive] version = "1" [target."cfg(any())".dependencies.serde_derive] version = "=1.0.217" serde-1.0.217/Cargo.toml.orig000064400000000000000000000045771046102023000140020ustar 00000000000000[package] name = "serde" version = "1.0.217" authors = ["Erick Tryzelaar ", "David Tolnay "] build = "build.rs" 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" 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.31" [dependencies] serde_derive = { version = "1", optional = true, path = "../serde_derive" } [dev-dependencies] serde_derive = { version = "1", path = "../serde_derive" } [lib] doc-scrape-examples = false [package.metadata.playground] features = ["derive", "rc"] [package.metadata.docs.rs] features = ["derive", "rc", "unstable"] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = ["--generate-link-to-definition"] # This cfg cannot be enabled, but it still forces Cargo to keep serde_derive's # version in lockstep with serde's, even if someone depends on the two crates # separately with serde's "derive" feature disabled. Every serde_derive release # is compatible with exactly one serde release because the generated code # involves nonpublic APIs which are not bound by semver. [target.'cfg(any())'.dependencies] serde_derive = { version = "=1.0.217", path = "../serde_derive" } ### FEATURES ################################################################# [features] default = ["std"] # Provide derive(Serialize, Deserialize) macros. derive = ["serde_derive"] # Provide impls for common standard library types like Vec and HashMap. # Requires a dependency on the Rust standard library. std = [] # Provide impls for types that require unstable functionality. For tracking and # discussion of unstable functionality please refer to this issue: # # https://github.com/serde-rs/serde/issues/812 unstable = [] # Provide impls for types in the Rust core allocation and collections library # including String, Box, Vec, and Cow. This is a subset of std but may # be enabled without depending on all of std. alloc = [] # Opt into impls for Rc and Arc. Serializing and deserializing these types # does not preserve identity and may result in multiple copies of the same data. # Be sure that this is what you want before enabling this feature. rc = [] serde-1.0.217/LICENSE-APACHE000064400000000000000000000227731046102023000130350ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS serde-1.0.217/LICENSE-MIT000064400000000000000000000017771046102023000125460ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. serde-1.0.217/README.md000064400000000000000000000104331046102023000123560ustar 00000000000000# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde msrv]][Rust 1.31] [![serde_derive msrv]][Rust 1.61] [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 msrv]: https://img.shields.io/crates/msrv/serde.svg?label=serde%20msrv&color=lightgray [serde_derive msrv]: https://img.shields.io/crates/msrv/serde_derive.svg?label=serde_derive%20msrv&color=lightgray [Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html [Rust 1.61]: https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html **Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.** --- You may be looking for: - [An overview of Serde](https://serde.rs/) - [Data formats supported by Serde](https://serde.rs/#data-formats) - [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html) - [Examples](https://serde.rs/examples.html) - [API documentation](https://docs.rs/serde) - [Release notes](https://github.com/serde-rs/serde/releases) ## Serde in action
Click to show Cargo.toml. Run this code in the playground. ```toml [dependencies] # The core APIs, including the Serialize and Deserialize traits. Always # required when using Serde. The "derive" feature is only required when # using #[derive(Serialize, Deserialize)] to make Serde work with structs # and enums defined in your crate. serde = { version = "1.0", features = ["derive"] } # Each data format lives in its own crate; the sample code below uses JSON # but you may be using a different one. serde_json = "1.0" ```

```rust use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct Point { x: i32, y: i32, } fn main() { let point = Point { x: 1, y: 2 }; // Convert the Point to a JSON string. let serialized = serde_json::to_string(&point).unwrap(); // Prints serialized = {"x":1,"y":2} println!("serialized = {}", serialized); // Convert the JSON string back to a Point. let deserialized: Point = serde_json::from_str(&serialized).unwrap(); // Prints deserialized = Point { x: 1, y: 2 } println!("deserialized = {:?}", deserialized); } ``` ## Getting help Serde is one of the most widely used Rust libraries so any place that Rustaceans congregate will be able to help you out. For chat, consider trying the [#rust-questions] or [#rust-beginners] channels of the unofficial community Discord (invite: ), the [#rust-usage] or [#beginners] channels of the official Rust Project Discord (invite: ), or the [#general][zulip] stream in Zulip. For asynchronous, consider the [\[rust\] tag on StackOverflow][stackoverflow], the [/r/rust] subreddit which has a pinned weekly easy questions post, or the Rust [Discourse forum][discourse]. It's acceptable to file a support issue in this repo but they tend not to get as many eyes as any of the above and may get closed without a response after some time. [#rust-questions]: https://discord.com/channels/273534239310479360/274215136414400513 [#rust-beginners]: https://discord.com/channels/273534239310479360/273541522815713281 [#rust-usage]: https://discord.com/channels/442252698964721669/443150878111694848 [#beginners]: https://discord.com/channels/442252698964721669/448238009733742612 [zulip]: https://rust-lang.zulipchat.com/#narrow/stream/122651-general [stackoverflow]: https://stackoverflow.com/questions/tagged/rust [/r/rust]: https://www.reddit.com/r/rust [discourse]: https://users.rust-lang.org
#### License Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. serde-1.0.217/build.rs000064400000000000000000000114731046102023000125510ustar 00000000000000use 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, }; if minor >= 77 { println!("cargo:rustc-check-cfg=cfg(no_core_cstr)"); println!("cargo:rustc-check-cfg=cfg(no_core_error)"); println!("cargo:rustc-check-cfg=cfg(no_core_net)"); println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); println!("cargo:rustc-check-cfg=cfg(no_core_try_from)"); println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); println!("cargo:rustc-check-cfg=cfg(no_float_copysign)"); println!("cargo:rustc-check-cfg=cfg(no_num_nonzero_signed)"); println!("cargo:rustc-check-cfg=cfg(no_relaxed_trait_bounds)"); println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); println!("cargo:rustc-check-cfg=cfg(no_systemtime_checked_add)"); println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)"); } let target = env::var("TARGET").unwrap(); let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; // 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 // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations if minor < 34 { println!("cargo:rustc-cfg=no_core_try_from"); println!("cargo:rustc-cfg=no_num_nonzero_signed"); println!("cargo:rustc-cfg=no_systemtime_checked_add"); println!("cargo:rustc-cfg=no_relaxed_trait_bounds"); } // f32::copysign and f64::copysign stabilized in Rust 1.35. // https://blog.rust-lang.org/2019/05/23/Rust-1.35.0.html#copy-the-sign-of-a-floating-point-number-onto-another if minor < 35 { println!("cargo:rustc-cfg=no_float_copysign"); } // Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60. if minor < 60 { println!("cargo:rustc-cfg=no_target_has_atomic"); // Allowlist of archs that support std::sync::atomic module. This is // based on rustc's compiler/rustc_target/src/spec/*.rs. let has_atomic64 = target.starts_with("x86_64") || target.starts_with("i686") || target.starts_with("aarch64") || target.starts_with("powerpc64") || target.starts_with("sparc64") || target.starts_with("mips64el") || target.starts_with("riscv64"); let has_atomic32 = has_atomic64 || emscripten; if minor < 34 || !has_atomic64 { println!("cargo:rustc-cfg=no_std_atomic64"); } if minor < 34 || !has_atomic32 { println!("cargo:rustc-cfg=no_std_atomic"); } } // Current minimum supported version of serde_derive crate is Rust 1.61. if minor < 61 { println!("cargo:rustc-cfg=no_serde_derive"); } // Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64. // https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc if minor < 64 { println!("cargo:rustc-cfg=no_core_cstr"); } // Support for core::num::Saturating and std::num::Saturating stabilized in Rust 1.74 // https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis if minor < 74 { println!("cargo:rustc-cfg=no_core_num_saturating"); } // Support for core::net stabilized in Rust 1.77. // https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html if minor < 77 { println!("cargo:rustc-cfg=no_core_net"); } // Support for the `#[diagnostic]` tool attribute namespace // https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes if minor < 78 { println!("cargo:rustc-cfg=no_diagnostic_namespace"); } // The Error trait became available in core in 1.81. // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror if minor < 81 { println!("cargo:rustc-cfg=no_core_error"); } } fn rustc_minor_version() -> Option { 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() } serde-1.0.217/crates-io.md000064400000000000000000000047531046102023000133170ustar 00000000000000 **Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.** --- You may be looking for: - [An overview of Serde](https://serde.rs/) - [Data formats supported by Serde](https://serde.rs/#data-formats) - [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html) - [Examples](https://serde.rs/examples.html) - [API documentation](https://docs.rs/serde) - [Release notes](https://github.com/serde-rs/serde/releases) ## Serde in action ```rust use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct Point { x: i32, y: i32, } fn main() { let point = Point { x: 1, y: 2 }; // Convert the Point to a JSON string. let serialized = serde_json::to_string(&point).unwrap(); // Prints serialized = {"x":1,"y":2} println!("serialized = {}", serialized); // Convert the JSON string back to a Point. let deserialized: Point = serde_json::from_str(&serialized).unwrap(); // Prints deserialized = Point { x: 1, y: 2 } println!("deserialized = {:?}", deserialized); } ``` ## Getting help Serde is one of the most widely used Rust libraries so any place that Rustaceans congregate will be able to help you out. For chat, consider trying the [#rust-questions] or [#rust-beginners] channels of the unofficial community Discord (invite: ), the [#rust-usage] or [#beginners] channels of the official Rust Project Discord (invite: ), or the [#general][zulip] stream in Zulip. For asynchronous, consider the [\[rust\] tag on StackOverflow][stackoverflow], the [/r/rust] subreddit which has a pinned weekly easy questions post, or the Rust [Discourse forum][discourse]. It's acceptable to file a support issue in this repo but they tend not to get as many eyes as any of the above and may get closed without a response after some time. [#rust-questions]: https://discord.com/channels/273534239310479360/274215136414400513 [#rust-beginners]: https://discord.com/channels/273534239310479360/273541522815713281 [#rust-usage]: https://discord.com/channels/442252698964721669/443150878111694848 [#beginners]: https://discord.com/channels/442252698964721669/448238009733742612 [zulip]: https://rust-lang.zulipchat.com/#narrow/stream/122651-general [stackoverflow]: https://stackoverflow.com/questions/tagged/rust [/r/rust]: https://www.reddit.com/r/rust [discourse]: https://users.rust-lang.org serde-1.0.217/src/de/ignored_any.rs000064400000000000000000000140121046102023000151170ustar 00000000000000use crate::lib::*; use crate::de::{ Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor, }; /// An efficient way of discarding data from a deserializer. /// /// Think of this like `serde_json::Value` in that it can be deserialized from /// any type, except that it does not store any information about the data that /// gets deserialized. /// /// ```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`. /// /// /// /// For example to deserialize only the element at index 3: /// /// /// /// ``` /// /// NthElement::new(3).deserialize(deserializer) /// /// ``` /// pub struct NthElement { /// n: usize, /// marker: PhantomData, /// } /// /// impl NthElement { /// pub fn new(n: usize) -> Self { /// NthElement { /// n: n, /// marker: PhantomData, /// } /// } /// } /// /// impl<'de, T> Visitor<'de> for NthElement /// where /// T: Deserialize<'de>, /// { /// type Value = T; /// /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!( /// formatter, /// "a sequence in which we care about element {}", /// self.n /// ) /// } /// /// fn visit_seq(self, mut seq: A) -> Result /// where /// A: SeqAccess<'de>, /// { /// // Skip over the first `n` elements. /// for i in 0..self.n { /// // It is an error if the sequence ends before we get to element `n`. /// if seq.next_element::()?.is_none() { /// return Err(de::Error::invalid_length(i, &self)); /// } /// } /// /// // Deserialize the one we care about. /// let nth = match seq.next_element()? { /// Some(nth) => nth, /// None => { /// return Err(de::Error::invalid_length(self.n, &self)); /// } /// }; /// /// // Skip over any remaining elements in the sequence after `n`. /// while let Some(IgnoredAny) = seq.next_element()? { /// // ignore /// } /// /// Ok(nth) /// } /// } /// /// impl<'de, T> DeserializeSeed<'de> for NthElement /// where /// T: Deserialize<'de>, /// { /// type Value = T; /// /// fn deserialize(self, deserializer: D) -> Result /// where /// D: Deserializer<'de>, /// { /// deserializer.deserialize_seq(self) /// } /// } /// /// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error> /// # where /// # D: Deserializer<'de>, /// # { /// // Deserialize only the sequence element at index 3 from this deserializer. /// // The element at index 3 is required to be a string. Elements before and /// // after index 3 are allowed to be of any type. /// let s: String = NthElement::new(3).deserialize(deserializer)?; /// # Ok(()) /// # } /// ``` #[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct IgnoredAny; impl<'de> Visitor<'de> for IgnoredAny { type Value = IgnoredAny; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("anything at all") } #[inline] fn visit_bool(self, x: bool) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_i64(self, x: i64) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_i128(self, x: i128) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_u64(self, x: u64) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_u128(self, x: u128) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_f64(self, x: f64) -> Result { let _ = x; Ok(IgnoredAny) } #[inline] fn visit_str(self, s: &str) -> Result where E: Error, { let _ = s; Ok(IgnoredAny) } #[inline] fn visit_none(self) -> Result { Ok(IgnoredAny) } #[inline] fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de>, { IgnoredAny::deserialize(deserializer) } #[inline] fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de>, { IgnoredAny::deserialize(deserializer) } #[inline] fn visit_unit(self) -> Result { Ok(IgnoredAny) } #[inline] fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { while let Some(IgnoredAny) = tri!(seq.next_element()) { // Gobble } Ok(IgnoredAny) } #[inline] fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { while let Some((IgnoredAny, IgnoredAny)) = tri!(map.next_entry()) { // Gobble } Ok(IgnoredAny) } #[inline] fn visit_bytes(self, bytes: &[u8]) -> Result where E: Error, { let _ = bytes; Ok(IgnoredAny) } fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { tri!(data.variant::()).1.newtype_variant() } } impl<'de> Deserialize<'de> for IgnoredAny { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_ignored_any(IgnoredAny) } } serde-1.0.217/src/de/impls.rs000064400000000000000000002766251046102023000137710ustar 00000000000000use crate::lib::*; use crate::de::{ Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, Unexpected, VariantAccess, Visitor, }; use crate::seed::InPlaceSeed; #[cfg(any(feature = "std", feature = "alloc"))] use crate::de::size_hint; //////////////////////////////////////////////////////////////////////////////// struct UnitVisitor; impl<'de> Visitor<'de> for UnitVisitor { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("unit") } fn visit_unit(self) -> Result where E: Error, { Ok(()) } } impl<'de> Deserialize<'de> for () { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_unit(UnitVisitor) } } #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] impl<'de> Deserialize<'de> for ! { fn deserialize(_deserializer: D) -> Result where D: Deserializer<'de>, { Err(Error::custom("cannot deserialize `!`")) } } //////////////////////////////////////////////////////////////////////////////// struct BoolVisitor; impl<'de> Visitor<'de> for BoolVisitor { type Value = bool; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a boolean") } fn visit_bool(self, v: bool) -> Result where E: Error, { Ok(v) } } impl<'de> Deserialize<'de> for bool { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_bool(BoolVisitor) } } //////////////////////////////////////////////////////////////////////////////// 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($($cfg)*)])* impl<'de> Deserialize<'de> for num::$nonzero { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct NonZeroVisitor; impl<'de> Visitor<'de> for NonZeroVisitor { type Value = num::$nonzero; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("a nonzero ", stringify!($primitive))) } $($($method!(nonzero $primitive $val : $visit);)*)* } deserializer.$deserialize(NonZeroVisitor) } } #[cfg(not(no_core_num_saturating))] impl<'de> Deserialize<'de> for Saturating<$primitive> { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct SaturatingVisitor; impl<'de> Visitor<'de> for SaturatingVisitor { type Value = Saturating<$primitive>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("integer with support for saturating semantics") } $($($method!(saturating $primitive $val : $visit);)*)* } deserializer.$deserialize(SaturatingVisitor) } } }; ($primitive:ident, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { impl<'de> Deserialize<'de> for $primitive { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct PrimitiveVisitor; impl<'de> Visitor<'de> for PrimitiveVisitor { type Value = $primitive; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(stringify!($primitive)) } $($($method!($val : $visit);)*)* } deserializer.$deserialize(PrimitiveVisitor) } } }; } macro_rules! num_self { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { Ok(v) } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if let Some(nonzero) = Self::Value::new(v) { Ok(nonzero) } else { Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) } } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { Ok(Saturating(v)) } }; } macro_rules! num_as_self { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { Ok(v as Self::Value) } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if let Some(nonzero) = Self::Value::new(v as $primitive) { Ok(nonzero) } else { Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) } } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { Ok(Saturating(v as $primitive)) } }; } macro_rules! num_as_copysign_self { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { #[cfg(any(no_float_copysign, not(feature = "std")))] { Ok(v as Self::Value) } #[cfg(all(not(no_float_copysign), feature = "std"))] { // Preserve sign of NaN. The `as` produces a nondeterministic sign. let sign = if v.is_sign_positive() { 1.0 } else { -1.0 }; Ok((v as Self::Value).copysign(sign)) } } }; } macro_rules! int_to_int { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { if Self::Value::min_value() as i64 <= v as i64 && v as i64 <= Self::Value::max_value() as i64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) } } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if $primitive::min_value() as i64 <= v as i64 && v as i64 <= $primitive::max_value() as i64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } } Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if (v as i64) < $primitive::MIN as i64 { Ok(Saturating($primitive::MIN)) } else if ($primitive::MAX as i64) < v as i64 { Ok(Saturating($primitive::MAX)) } else { Ok(Saturating(v as $primitive)) } } }; } macro_rules! int_to_uint { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { if 0 <= v && v as u64 <= Self::Value::max_value() as u64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) } } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if 0 < v && v as u64 <= $primitive::max_value() as u64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } } Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if v < 0 { Ok(Saturating(0)) } else if ($primitive::MAX as u64) < v as u64 { Ok(Saturating($primitive::MAX)) } else { Ok(Saturating(v as $primitive)) } } }; } macro_rules! uint_to_self { ($ty:ident : $visit:ident) => { #[inline] fn $visit(self, v: $ty) -> Result where E: Error, { if v as u64 <= Self::Value::max_value() as u64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) } } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if v as u64 <= $primitive::max_value() as u64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } } Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if v as u64 <= $primitive::MAX as u64 { Ok(Saturating(v as $primitive)) } else { Ok(Saturating($primitive::MAX)) } } }; } impl_deserialize_num! { i8, NonZeroI8 cfg(not(no_num_nonzero_signed)), deserialize_i8 num_self!(i8:visit_i8); int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { i16, NonZeroI16 cfg(not(no_num_nonzero_signed)), deserialize_i16 num_self!(i16:visit_i16); num_as_self!(i8:visit_i8); int_to_int!(i32:visit_i32 i64:visit_i64); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { i32, NonZeroI32 cfg(not(no_num_nonzero_signed)), deserialize_i32 num_self!(i32:visit_i32); num_as_self!(i8:visit_i8 i16:visit_i16); int_to_int!(i64:visit_i64); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { i64, NonZeroI64 cfg(not(no_num_nonzero_signed)), deserialize_i64 num_self!(i64:visit_i64); num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { isize, NonZeroIsize cfg(not(no_num_nonzero_signed)), deserialize_i64 num_as_self!(i8:visit_i8 i16:visit_i16); int_to_int!(i32:visit_i32 i64:visit_i64); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { u8, NonZeroU8, deserialize_u8 num_self!(u8:visit_u8); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { u16, NonZeroU16, deserialize_u16 num_self!(u16:visit_u16); num_as_self!(u8:visit_u8); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { u32, NonZeroU32, deserialize_u32 num_self!(u32:visit_u32); num_as_self!(u8:visit_u8 u16:visit_u16); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u64:visit_u64); } impl_deserialize_num! { u64, NonZeroU64, deserialize_u64 num_self!(u64:visit_u64); num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); } impl_deserialize_num! { usize, NonZeroUsize, deserialize_u64 num_as_self!(u8:visit_u8 u16:visit_u16); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { f32, deserialize_f32 num_self!(f32:visit_f32); num_as_copysign_self!(f64:visit_f64); num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { f64, deserialize_f64 num_self!(f64:visit_f64); num_as_copysign_self!(f32:visit_f32); num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } macro_rules! num_128 { ($ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if v as i128 >= Self::Value::min_value() as i128 && v as u128 <= Self::Value::max_value() as u128 { Ok(v as Self::Value) } else { Err(Error::invalid_value( Unexpected::Other(stringify!($ty)), &self, )) } } }; (nonzero $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if v as i128 >= $primitive::min_value() as i128 && v as u128 <= $primitive::max_value() as u128 { if let Some(nonzero) = Self::Value::new(v as $primitive) { Ok(nonzero) } else { Err(Error::invalid_value(Unexpected::Unsigned(0), &self)) } } else { Err(Error::invalid_value( Unexpected::Other(stringify!($ty)), &self, )) } } }; (saturating $primitive:ident $ty:ident : $visit:ident) => { fn $visit(self, v: $ty) -> Result where E: Error, { if (v as i128) < $primitive::MIN as i128 { Ok(Saturating($primitive::MIN)) } else if ($primitive::MAX as u128) < v as u128 { Ok(Saturating($primitive::MAX)) } else { Ok(Saturating(v as $primitive)) } } }; } impl_deserialize_num! { i128, NonZeroI128 cfg(not(no_num_nonzero_signed)), deserialize_i128 num_self!(i128:visit_i128); num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); num_128!(u128:visit_u128); } impl_deserialize_num! { u128, NonZeroU128, deserialize_u128 num_self!(u128:visit_u128); num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); num_128!(i128:visit_i128); } //////////////////////////////////////////////////////////////////////////////// struct CharVisitor; impl<'de> Visitor<'de> for CharVisitor { type Value = char; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a character") } #[inline] fn visit_char(self, v: char) -> Result where E: Error, { Ok(v) } #[inline] fn visit_str(self, v: &str) -> Result where E: Error, { let mut iter = v.chars(); match (iter.next(), iter.next()) { (Some(c), None) => Ok(c), _ => Err(Error::invalid_value(Unexpected::Str(v), &self)), } } } impl<'de> Deserialize<'de> for char { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_char(CharVisitor) } } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] struct StringVisitor; #[cfg(any(feature = "std", feature = "alloc"))] struct StringInPlaceVisitor<'a>(&'a mut String); #[cfg(any(feature = "std", feature = "alloc"))] impl<'de> Visitor<'de> for StringVisitor { type Value = String; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(v.to_owned()) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(v) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { match str::from_utf8(v) { Ok(s) => Ok(s.to_owned()), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { match String::from_utf8(v) { Ok(s) => Ok(s), Err(e) => Err(Error::invalid_value( Unexpected::Bytes(&e.into_bytes()), &self, )), } } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de> Visitor<'de> for StringInPlaceVisitor<'a> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_str(self, v: &str) -> Result where E: Error, { self.0.clear(); self.0.push_str(v); Ok(()) } fn visit_string(self, v: String) -> Result where E: Error, { *self.0 = v; Ok(()) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { match str::from_utf8(v) { Ok(s) => { self.0.clear(); self.0.push_str(s); Ok(()) } Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { match String::from_utf8(v) { Ok(s) => { *self.0 = s; Ok(()) } Err(e) => Err(Error::invalid_value( Unexpected::Bytes(&e.into_bytes()), &self, )), } } } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de> Deserialize<'de> for String { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_string(StringVisitor) } fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { deserializer.deserialize_string(StringInPlaceVisitor(place)) } } //////////////////////////////////////////////////////////////////////////////// struct StrVisitor; impl<'a> Visitor<'a> for StrVisitor { type Value = &'a str; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a borrowed string") } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(v) // so easy } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { str::from_utf8(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) } } impl<'de: 'a, 'a> Deserialize<'de> for &'a str { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_str(StrVisitor) } } //////////////////////////////////////////////////////////////////////////////// struct BytesVisitor; impl<'a> Visitor<'a> for BytesVisitor { type Value = &'a [u8]; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a borrowed byte array") } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { Ok(v) } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(v.as_bytes()) } } impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_bytes(BytesVisitor) } } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] struct CStringVisitor; #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] impl<'de> Visitor<'de> for CStringVisitor { type Value = CString; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("byte array") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let capacity = size_hint::cautious::(seq.size_hint()); let mut values = Vec::::with_capacity(capacity); while let Some(value) = tri!(seq.next_element()) { values.push(value); } CString::new(values).map_err(Error::custom) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { CString::new(v).map_err(Error::custom) } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { CString::new(v).map_err(Error::custom) } fn visit_str(self, v: &str) -> Result where E: Error, { CString::new(v).map_err(Error::custom) } fn visit_string(self, v: String) -> Result where E: Error, { CString::new(v).map_err(Error::custom) } } #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de> Deserialize<'de> for CString { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_byte_buf(CStringVisitor) } } macro_rules! forwarded_impl { ( $(#[$attr:meta])* ($($id:ident),*), $ty:ty, $func:expr ) => { $(#[$attr])* impl<'de $(, $id : Deserialize<'de>,)*> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Deserialize::deserialize(deserializer).map($func) } } } } forwarded_impl! { #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] (), Box, CString::into_boxed_c_str } forwarded_impl! { (T), Reverse, Reverse } //////////////////////////////////////////////////////////////////////////////// struct OptionVisitor { marker: PhantomData, } impl<'de, T> Visitor<'de> for OptionVisitor where T: Deserialize<'de>, { type Value = Option; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("option") } #[inline] fn visit_unit(self) -> Result where E: Error, { Ok(None) } #[inline] fn visit_none(self) -> Result where E: Error, { Ok(None) } #[inline] fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de>, { T::deserialize(deserializer).map(Some) } fn __private_visit_untagged_option(self, deserializer: D) -> Result where D: Deserializer<'de>, { Ok(T::deserialize(deserializer).ok()) } } impl<'de, T> Deserialize<'de> for Option where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_option(OptionVisitor { marker: PhantomData, }) } // The Some variant's repr is opaque, so we can't play cute tricks with its // tag to have deserialize_in_place build the content in place unconditionally. // // FIXME: investigate whether branching on the old value being Some to // deserialize_in_place the value is profitable (probably data-dependent?) } //////////////////////////////////////////////////////////////////////////////// struct PhantomDataVisitor { marker: PhantomData, } impl<'de, T> Visitor<'de> for PhantomDataVisitor where T: ?Sized, { type Value = PhantomData; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("unit") } #[inline] fn visit_unit(self) -> Result where E: Error, { Ok(PhantomData) } } impl<'de, T> Deserialize<'de> for PhantomData where T: ?Sized, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let visitor = PhantomDataVisitor { marker: PhantomData, }; deserializer.deserialize_unit_struct("PhantomData", visitor) } } //////////////////////////////////////////////////////////////////////////////// macro_rules! seq_impl { ( $(#[$attr:meta])* $ty:ident , $access:ident, $clear:expr, $with_capacity:expr, $reserve:expr, $insert:expr ) => { $(#[$attr])* impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty where T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, $($typaram: $bound1 $(+ $bound2)*,)* { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct SeqVisitor { marker: PhantomData<$ty>, } impl<'de, T $(, $typaram)*> Visitor<'de> for SeqVisitor where T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, $($typaram: $bound1 $(+ $bound2)*,)* { type Value = $ty; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } #[inline] fn visit_seq(self, mut $access: A) -> Result where A: SeqAccess<'de>, { let mut values = $with_capacity; while let Some(value) = tri!($access.next_element()) { $insert(&mut values, value); } Ok(values) } } let visitor = SeqVisitor { marker: PhantomData }; deserializer.deserialize_seq(visitor) } fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { struct SeqInPlaceVisitor<'a, T: 'a $(, $typaram: 'a)*>(&'a mut $ty); impl<'a, 'de, T $(, $typaram)*> Visitor<'de> for SeqInPlaceVisitor<'a, T $(, $typaram)*> where T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, $($typaram: $bound1 $(+ $bound2)*,)* { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } #[inline] fn visit_seq(mut self, mut $access: A) -> Result where A: SeqAccess<'de>, { $clear(&mut self.0); $reserve(&mut self.0, size_hint::cautious::($access.size_hint())); // FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList) while let Some(value) = tri!($access.next_element()) { $insert(&mut self.0, value); } Ok(()) } } deserializer.deserialize_seq(SeqInPlaceVisitor(place)) } } } } // Dummy impl of reserve #[cfg(any(feature = "std", feature = "alloc"))] fn nop_reserve(_seq: T, _n: usize) {} seq_impl!( #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BinaryHeap, seq, BinaryHeap::clear, BinaryHeap::with_capacity(size_hint::cautious::(seq.size_hint())), BinaryHeap::reserve, BinaryHeap::push ); seq_impl!( #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BTreeSet, seq, BTreeSet::clear, BTreeSet::new(), nop_reserve, BTreeSet::insert ); seq_impl!( #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] LinkedList, seq, LinkedList::clear, LinkedList::new(), nop_reserve, LinkedList::push_back ); seq_impl!( #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] HashSet, seq, HashSet::clear, HashSet::with_capacity_and_hasher(size_hint::cautious::(seq.size_hint()), S::default()), HashSet::reserve, HashSet::insert ); seq_impl!( #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] VecDeque, seq, VecDeque::clear, VecDeque::with_capacity(size_hint::cautious::(seq.size_hint())), VecDeque::reserve, VecDeque::push_back ); //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, T> Deserialize<'de> for Vec where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct VecVisitor { marker: PhantomData, } impl<'de, T> Visitor<'de> for VecVisitor where T: Deserialize<'de>, { type Value = Vec; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let capacity = size_hint::cautious::(seq.size_hint()); let mut values = Vec::::with_capacity(capacity); while let Some(value) = tri!(seq.next_element()) { values.push(value); } Ok(values) } } let visitor = VecVisitor { marker: PhantomData, }; deserializer.deserialize_seq(visitor) } fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { struct VecInPlaceVisitor<'a, T: 'a>(&'a mut Vec); impl<'a, 'de, T> Visitor<'de> for VecInPlaceVisitor<'a, T> where T: Deserialize<'de>, { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let hint = size_hint::cautious::(seq.size_hint()); if let Some(additional) = hint.checked_sub(self.0.len()) { self.0.reserve(additional); } for i in 0..self.0.len() { let next = { let next_place = InPlaceSeed(&mut self.0[i]); tri!(seq.next_element_seed(next_place)) }; if next.is_none() { self.0.truncate(i); return Ok(()); } } while let Some(value) = tri!(seq.next_element()) { self.0.push(value); } Ok(()) } } deserializer.deserialize_seq(VecInPlaceVisitor(place)) } } //////////////////////////////////////////////////////////////////////////////// struct ArrayVisitor { marker: PhantomData, } struct ArrayInPlaceVisitor<'a, A: 'a>(&'a mut A); impl ArrayVisitor { fn new() -> Self { ArrayVisitor { marker: PhantomData, } } } impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> { type Value = [T; 0]; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("an empty array") } #[inline] fn visit_seq(self, _: A) -> Result where A: SeqAccess<'de>, { Ok([]) } } // Does not require T: Deserialize<'de>. impl<'de, T> Deserialize<'de> for [T; 0] { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_tuple(0, ArrayVisitor::<[T; 0]>::new()) } } macro_rules! array_impls { ($($len:expr => ($($n:tt)+))+) => { $( impl<'de, T> Visitor<'de> for ArrayVisitor<[T; $len]> where T: Deserialize<'de>, { type Value = [T; $len]; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("an array of length ", $len)) } #[inline] fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { Ok([$( match tri!(seq.next_element()) { Some(val) => val, None => return Err(Error::invalid_length($n, &self)), } ),+]) } } impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; $len]> where T: Deserialize<'de>, { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("an array of length ", $len)) } #[inline] fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let mut fail_idx = None; for (idx, dest) in self.0[..].iter_mut().enumerate() { if tri!(seq.next_element_seed(InPlaceSeed(dest))).is_none() { fail_idx = Some(idx); break; } } if let Some(idx) = fail_idx { return Err(Error::invalid_length(idx, &self)); } Ok(()) } } impl<'de, T> Deserialize<'de> for [T; $len] where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new()) } fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { deserializer.deserialize_tuple($len, ArrayInPlaceVisitor(place)) } } )+ } } array_impls! { 1 => (0) 2 => (0 1) 3 => (0 1 2) 4 => (0 1 2 3) 5 => (0 1 2 3 4) 6 => (0 1 2 3 4 5) 7 => (0 1 2 3 4 5 6) 8 => (0 1 2 3 4 5 6 7) 9 => (0 1 2 3 4 5 6 7 8) 10 => (0 1 2 3 4 5 6 7 8 9) 11 => (0 1 2 3 4 5 6 7 8 9 10) 12 => (0 1 2 3 4 5 6 7 8 9 10 11) 13 => (0 1 2 3 4 5 6 7 8 9 10 11 12) 14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13) 15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14) 16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) 17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) 18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17) 19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) 20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) 21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) 22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21) 23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22) 24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23) 25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) 26 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25) 27 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26) 28 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27) 29 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28) 30 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29) 31 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30) 32 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) } //////////////////////////////////////////////////////////////////////////////// macro_rules! tuple_impls { ($($len:tt => ($($n:tt $name:ident)+))+) => { $( #[cfg_attr(docsrs, doc(hidden))] impl<'de, $($name),+> Deserialize<'de> for ($($name,)+) where $($name: Deserialize<'de>,)+ { tuple_impl_body!($len => ($($n $name)+)); } )+ }; } macro_rules! tuple_impl_body { ($len:tt => ($($n:tt $name:ident)+)) => { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct TupleVisitor<$($name,)+> { marker: PhantomData<($($name,)+)>, } impl<'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleVisitor<$($name,)+> { type Value = ($($name,)+); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("a tuple of size ", $len)) } #[inline] #[allow(non_snake_case)] fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { $( let $name = match tri!(seq.next_element()) { Some(value) => value, None => return Err(Error::invalid_length($n, &self)), }; )+ Ok(($($name,)+)) } } deserializer.deserialize_tuple($len, TupleVisitor { marker: PhantomData }) } #[inline] fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { struct TupleInPlaceVisitor<'a, $($name: 'a,)+>(&'a mut ($($name,)+)); impl<'a, 'de, $($name: Deserialize<'de>),+> Visitor<'de> for TupleInPlaceVisitor<'a, $($name,)+> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("a tuple of size ", $len)) } #[inline] #[allow(non_snake_case)] fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { $( if tri!(seq.next_element_seed(InPlaceSeed(&mut (self.0).$n))).is_none() { return Err(Error::invalid_length($n, &self)); } )+ Ok(()) } } deserializer.deserialize_tuple($len, TupleInPlaceVisitor(place)) } }; } #[cfg_attr(docsrs, doc(fake_variadic))] #[cfg_attr( docsrs, doc = "This trait is implemented for tuples up to 16 items long." )] impl<'de, T> Deserialize<'de> for (T,) where T: Deserialize<'de>, { tuple_impl_body!(1 => (0 T)); } tuple_impls! { 2 => (0 T0 1 T1) 3 => (0 T0 1 T1 2 T2) 4 => (0 T0 1 T1 2 T2 3 T3) 5 => (0 T0 1 T1 2 T2 3 T3 4 T4) 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5) 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6) 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7) 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8) 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9) 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10) 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11) 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12) 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13) 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14) 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15) } //////////////////////////////////////////////////////////////////////////////// macro_rules! map_impl { ( $(#[$attr:meta])* $ty:ident , $access:ident, $with_capacity:expr, ) => { $(#[$attr])* impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty where K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, V: Deserialize<'de>, $($typaram: $bound1 $(+ $bound2)*),* { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct MapVisitor { marker: PhantomData<$ty>, } impl<'de, K, V $(, $typaram)*> Visitor<'de> for MapVisitor where K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, V: Deserialize<'de>, $($typaram: $bound1 $(+ $bound2)*),* { type Value = $ty; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a map") } #[inline] fn visit_map(self, mut $access: A) -> Result where A: MapAccess<'de>, { let mut values = $with_capacity; while let Some((key, value)) = tri!($access.next_entry()) { values.insert(key, value); } Ok(values) } } let visitor = MapVisitor { marker: PhantomData }; deserializer.deserialize_map(visitor) } } } } map_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BTreeMap, map, BTreeMap::new(), } map_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] HashMap, map, HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default()), } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", not(no_core_net)))] macro_rules! parse_ip_impl { ($ty:ty, $expecting:expr, $size:tt) => { impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { if deserializer.is_human_readable() { deserializer.deserialize_str(FromStrVisitor::new($expecting)) } else { <[u8; $size]>::deserialize(deserializer).map(<$ty>::from) } } } }; } #[cfg(any(feature = "std", not(no_core_net)))] macro_rules! variant_identifier { ( $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) $expecting_message:expr, $variants_name:ident ) => { enum $name_kind { $($variant),* } static $variants_name: &[&str] = &[$(stringify!($variant)),*]; impl<'de> Deserialize<'de> for $name_kind { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct KindVisitor; impl<'de> Visitor<'de> for KindVisitor { type Value = $name_kind; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str($expecting_message) } fn visit_u64(self, value: u64) -> Result where E: Error, { match value { $( $index => Ok($name_kind :: $variant), )* _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self),), } } fn visit_str(self, value: &str) -> Result where E: Error, { match value { $( stringify!($variant) => Ok($name_kind :: $variant), )* _ => Err(Error::unknown_variant(value, $variants_name)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { $( $bytes => Ok($name_kind :: $variant), )* _ => { match str::from_utf8(value) { Ok(value) => Err(Error::unknown_variant(value, $variants_name)), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)), } } } } } deserializer.deserialize_identifier(KindVisitor) } } } } #[cfg(any(feature = "std", not(no_core_net)))] macro_rules! deserialize_enum { ( $name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*) $expecting_message:expr, $deserializer:expr ) => { variant_identifier! { $name_kind ($($variant; $bytes; $index),*) $expecting_message, VARIANTS } struct EnumVisitor; impl<'de> Visitor<'de> for EnumVisitor { type Value = $name; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(concat!("a ", stringify!($name))) } fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { match tri!(data.variant()) { $( ($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant), )* } } } $deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor) } } #[cfg(any(feature = "std", not(no_core_net)))] impl<'de> Deserialize<'de> for net::IpAddr { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { if deserializer.is_human_readable() { deserializer.deserialize_str(FromStrVisitor::new("IP address")) } else { use crate::lib::net::IpAddr; deserialize_enum! { IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) "`V4` or `V6`", deserializer } } } } #[cfg(any(feature = "std", not(no_core_net)))] parse_ip_impl!(net::Ipv4Addr, "IPv4 address", 4); #[cfg(any(feature = "std", not(no_core_net)))] parse_ip_impl!(net::Ipv6Addr, "IPv6 address", 16); #[cfg(any(feature = "std", not(no_core_net)))] macro_rules! parse_socket_impl { ( $ty:ty, $expecting:tt, $new:expr, ) => { impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { if deserializer.is_human_readable() { deserializer.deserialize_str(FromStrVisitor::new($expecting)) } else { <(_, u16)>::deserialize(deserializer).map($new) } } } }; } #[cfg(any(feature = "std", not(no_core_net)))] impl<'de> Deserialize<'de> for net::SocketAddr { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { if deserializer.is_human_readable() { deserializer.deserialize_str(FromStrVisitor::new("socket address")) } else { use crate::lib::net::SocketAddr; deserialize_enum! { SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) "`V4` or `V6`", deserializer } } } } #[cfg(any(feature = "std", not(no_core_net)))] parse_socket_impl! { net::SocketAddrV4, "IPv4 socket address", |(ip, port)| net::SocketAddrV4::new(ip, port), } #[cfg(any(feature = "std", not(no_core_net)))] parse_socket_impl! { net::SocketAddrV6, "IPv6 socket address", |(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0), } //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] struct PathVisitor; #[cfg(feature = "std")] impl<'a> Visitor<'a> for PathVisitor { type Value = &'a Path; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a borrowed path") } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(v.as_ref()) } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { str::from_utf8(v) .map(AsRef::as_ref) .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl<'de: 'a, 'a> Deserialize<'de> for &'a Path { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_str(PathVisitor) } } #[cfg(feature = "std")] struct PathBufVisitor; #[cfg(feature = "std")] impl<'de> Visitor<'de> for PathBufVisitor { type Value = PathBuf; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("path string") } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(From::from(v)) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(From::from(v)) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { str::from_utf8(v) .map(From::from) .map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { String::from_utf8(v) .map(From::from) .map_err(|e| Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)) } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl<'de> Deserialize<'de> for PathBuf { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_string(PathBufVisitor) } } forwarded_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] (), Box, PathBuf::into_boxed_path } //////////////////////////////////////////////////////////////////////////////// // If this were outside of the serde crate, it would just use: // // #[derive(Deserialize)] // #[serde(variant_identifier)] #[cfg(all(feature = "std", any(unix, windows)))] variant_identifier! { OsStringKind (Unix; b"Unix"; 0, Windows; b"Windows"; 1) "`Unix` or `Windows`", OSSTR_VARIANTS } #[cfg(all(feature = "std", any(unix, windows)))] struct OsStringVisitor; #[cfg(all(feature = "std", any(unix, windows)))] impl<'de> Visitor<'de> for OsStringVisitor { type Value = OsString; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("os string") } #[cfg(unix)] fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { use std::os::unix::ffi::OsStringExt; 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", )), } } #[cfg(windows)] fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { use std::os::windows::ffi::OsStringExt; match tri!(data.variant()) { (OsStringKind::Windows, v) => v .newtype_variant::>() .map(|vec| OsString::from_wide(&vec)), (OsStringKind::Unix, _) => Err(Error::custom( "cannot deserialize Unix OS string on Windows", )), } } } #[cfg(all(feature = "std", any(unix, windows)))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))] impl<'de> Deserialize<'de> for OsString { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor) } } //////////////////////////////////////////////////////////////////////////////// forwarded_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] (T), Box, Box::new } forwarded_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] (T), Box<[T]>, Vec::into_boxed_slice } forwarded_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] (), Box, String::into_boxed_str } forwarded_impl! { #[cfg(all(feature = "std", any(unix, windows)))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))] (), Box, OsString::into_boxed_os_str } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, 'a, T> Deserialize<'de> for Cow<'a, T> where T: ?Sized + ToOwned, T::Owned: Deserialize<'de>, { #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { T::Owned::deserialize(deserializer).map(Cow::Owned) } } //////////////////////////////////////////////////////////////////////////////// /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting /// `Weak` has a reference count of 0 and cannot be upgraded. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr( docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) )] impl<'de, T> Deserialize<'de> for RcWeak where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { tri!(Option::::deserialize(deserializer)); Ok(RcWeak::new()) } } /// This impl requires the [`"rc"`] Cargo feature of Serde. The resulting /// `Weak` has a reference count of 0 and cannot be upgraded. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr( docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) )] impl<'de, T> Deserialize<'de> for ArcWeak where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { tri!(Option::::deserialize(deserializer)); Ok(ArcWeak::new()) } } //////////////////////////////////////////////////////////////////////////////// macro_rules! box_forwarded_impl { ( $(#[$attr:meta])* $t:ident ) => { $(#[$attr])* impl<'de, T> Deserialize<'de> for $t where T: ?Sized, Box: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Box::deserialize(deserializer).map(Into::into) } } }; } box_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 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] Rc } box_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 #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] Arc } //////////////////////////////////////////////////////////////////////////////// impl<'de, T> Deserialize<'de> for Cell where T: Deserialize<'de> + Copy, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { T::deserialize(deserializer).map(Cell::new) } } forwarded_impl! { (T), RefCell, RefCell::new } forwarded_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] (T), Mutex, Mutex::new } forwarded_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] (T), RwLock, RwLock::new } //////////////////////////////////////////////////////////////////////////////// // This is a cleaned-up version of the impl generated by: // // #[derive(Deserialize)] // #[serde(deny_unknown_fields)] // struct Duration { // secs: u64, // nanos: u32, // } impl<'de> Deserialize<'de> for Duration { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { // If this were outside of the serde crate, it would just use: // // #[derive(Deserialize)] // #[serde(field_identifier, rename_all = "lowercase")] enum Field { Secs, Nanos, } impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result 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("`secs` or `nanos`") } fn visit_str(self, value: &str) -> Result where E: Error, { match value { "secs" => Ok(Field::Secs), "nanos" => Ok(Field::Nanos), _ => Err(Error::unknown_field(value, FIELDS)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { b"secs" => Ok(Field::Secs), b"nanos" => Ok(Field::Nanos), _ => { let value = crate::__private::from_utf8_lossy(value); Err(Error::unknown_field(&*value, FIELDS)) } } } } deserializer.deserialize_identifier(FieldVisitor) } } fn check_overflow(secs: u64, nanos: u32) -> Result<(), E> where E: Error, { static NANOS_PER_SEC: u32 = 1_000_000_000; match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { Some(_) => Ok(()), None => Err(E::custom("overflow deserializing Duration")), } } struct DurationVisitor; impl<'de> Visitor<'de> for DurationVisitor { type Value = Duration; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("struct Duration") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let secs: u64 = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(0, &self)); } }; let nanos: u32 = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(1, &self)); } }; tri!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut secs: Option = None; let mut nanos: Option = None; while let Some(key) = tri!(map.next_key()) { match key { Field::Secs => { if secs.is_some() { return Err(::duplicate_field("secs")); } secs = Some(tri!(map.next_value())); } Field::Nanos => { if nanos.is_some() { return Err(::duplicate_field("nanos")); } nanos = Some(tri!(map.next_value())); } } } let secs = match secs { Some(secs) => secs, None => return Err(::missing_field("secs")), }; let nanos = match nanos { Some(nanos) => nanos, None => return Err(::missing_field("nanos")), }; tri!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } } const FIELDS: &[&str] = &["secs", "nanos"]; deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor) } } //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl<'de> Deserialize<'de> for SystemTime { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { // Reuse duration enum Field { Secs, Nanos, } impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result 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("`secs_since_epoch` or `nanos_since_epoch`") } fn visit_str(self, value: &str) -> Result where E: Error, { match value { "secs_since_epoch" => Ok(Field::Secs), "nanos_since_epoch" => Ok(Field::Nanos), _ => Err(Error::unknown_field(value, FIELDS)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { b"secs_since_epoch" => Ok(Field::Secs), b"nanos_since_epoch" => Ok(Field::Nanos), _ => { let value = String::from_utf8_lossy(value); Err(Error::unknown_field(&value, FIELDS)) } } } } deserializer.deserialize_identifier(FieldVisitor) } } fn check_overflow(secs: u64, nanos: u32) -> Result<(), E> where E: Error, { static NANOS_PER_SEC: u32 = 1_000_000_000; match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { Some(_) => Ok(()), None => Err(E::custom("overflow deserializing SystemTime epoch offset")), } } struct DurationVisitor; impl<'de> Visitor<'de> for DurationVisitor { type Value = Duration; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("struct SystemTime") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let secs: u64 = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(0, &self)); } }; let nanos: u32 = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(1, &self)); } }; tri!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut secs: Option = None; let mut nanos: Option = None; while let Some(key) = tri!(map.next_key()) { match key { Field::Secs => { if secs.is_some() { return Err(::duplicate_field( "secs_since_epoch", )); } secs = Some(tri!(map.next_value())); } Field::Nanos => { if nanos.is_some() { return Err(::duplicate_field( "nanos_since_epoch", )); } nanos = Some(tri!(map.next_value())); } } } let secs = match secs { Some(secs) => secs, None => return Err(::missing_field("secs_since_epoch")), }; let nanos = match nanos { Some(nanos) => nanos, None => return Err(::missing_field("nanos_since_epoch")), }; tri!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } } 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) .ok_or_else(|| D::Error::custom("overflow deserializing SystemTime")); #[cfg(no_systemtime_checked_add)] let ret = Ok(UNIX_EPOCH + duration); ret } } //////////////////////////////////////////////////////////////////////////////// // Similar to: // // #[derive(Deserialize)] // #[serde(deny_unknown_fields)] // struct Range { // start: Idx, // end: Idx, // } impl<'de, Idx> Deserialize<'de> for Range where Idx: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let (start, end) = tri!(deserializer.deserialize_struct( "Range", range::FIELDS, range::RangeVisitor { expecting: "struct Range", phantom: PhantomData, }, )); Ok(start..end) } } impl<'de, Idx> Deserialize<'de> for RangeInclusive where Idx: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let (start, end) = tri!(deserializer.deserialize_struct( "RangeInclusive", range::FIELDS, range::RangeVisitor { expecting: "struct RangeInclusive", phantom: PhantomData, }, )); Ok(RangeInclusive::new(start, end)) } } mod range { use crate::lib::*; use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; pub const FIELDS: &[&str] = &["start", "end"]; // If this were outside of the serde crate, it would just use: // // #[derive(Deserialize)] // #[serde(field_identifier, rename_all = "lowercase")] enum Field { Start, End, } impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result 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` or `end`") } fn visit_str(self, value: &str) -> Result where E: Error, { match value { "start" => Ok(Field::Start), "end" => Ok(Field::End), _ => Err(Error::unknown_field(value, FIELDS)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { b"start" => Ok(Field::Start), 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 RangeVisitor { pub expecting: &'static str, pub phantom: PhantomData, } impl<'de, Idx> Visitor<'de> for RangeVisitor where Idx: Deserialize<'de>, { type Value = (Idx, Idx); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(self.expecting) } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let start: Idx = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(0, &self)); } }; let end: Idx = match tri!(seq.next_element()) { Some(value) => value, None => { return Err(Error::invalid_length(1, &self)); } }; Ok((start, end)) } fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut start: Option = None; let mut end: Option = None; while let Some(key) = tri!(map.next_key()) { match key { Field::Start => { if start.is_some() { return Err(::duplicate_field("start")); } start = Some(tri!(map.next_value())); } Field::End => { if end.is_some() { return Err(::duplicate_field("end")); } end = Some(tri!(map.next_value())); } } } let start = match start { Some(start) => start, None => return Err(::missing_field("start")), }; let end = match end { Some(end) => end, None => return Err(::missing_field("end")), }; Ok((start, end)) } } } //////////////////////////////////////////////////////////////////////////////// // Similar to: // // #[derive(Deserialize)] // #[serde(deny_unknown_fields)] // struct RangeFrom { // start: Idx, // } impl<'de, Idx> Deserialize<'de> for RangeFrom where Idx: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result 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] = &["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(deserializer: D) -> Result 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(self, value: &str) -> Result where E: Error, { match value { "start" => Ok(Field::Start), _ => Err(Error::unknown_field(value, FIELDS)), } } fn visit_bytes(self, value: &[u8]) -> Result 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 RangeFromVisitor { pub expecting: &'static str, pub phantom: PhantomData, } impl<'de, Idx> Visitor<'de> for RangeFromVisitor where Idx: Deserialize<'de>, { type Value = Idx; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(self.expecting) } fn visit_seq(self, mut seq: A) -> Result 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(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut start: Option = None; while let Some(key) = tri!(map.next_key()) { match key { Field::Start => { if start.is_some() { return Err(::duplicate_field("start")); } start = Some(tri!(map.next_value())); } } } let start = match start { Some(start) => start, None => return Err(::missing_field("start")), }; Ok(start) } } } //////////////////////////////////////////////////////////////////////////////// // Similar to: // // #[derive(Deserialize)] // #[serde(deny_unknown_fields)] // struct RangeTo { // end: Idx, // } impl<'de, Idx> Deserialize<'de> for RangeTo where Idx: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result 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] = &["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(deserializer: D) -> Result 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(self, value: &str) -> Result where E: Error, { match value { "end" => Ok(Field::End), _ => Err(Error::unknown_field(value, FIELDS)), } } fn visit_bytes(self, value: &[u8]) -> Result 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 RangeToVisitor { pub expecting: &'static str, pub phantom: PhantomData, } impl<'de, Idx> Visitor<'de> for RangeToVisitor where Idx: Deserialize<'de>, { type Value = Idx; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(self.expecting) } fn visit_seq(self, mut seq: A) -> Result 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(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut end: Option = None; while let Some(key) = tri!(map.next_key()) { match key { Field::End => { if end.is_some() { return Err(::duplicate_field("end")); } end = Some(tri!(map.next_value())); } } } let end = match end { Some(end) => end, None => return Err(::missing_field("end")), }; Ok(end) } } } //////////////////////////////////////////////////////////////////////////////// impl<'de, T> Deserialize<'de> for Bound where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { enum Field { Unbounded, Included, Excluded, } impl<'de> Deserialize<'de> for Field { #[inline] fn deserialize(deserializer: D) -> Result 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("`Unbounded`, `Included` or `Excluded`") } fn visit_u64(self, value: u64) -> Result where E: Error, { match value { 0 => Ok(Field::Unbounded), 1 => Ok(Field::Included), 2 => Ok(Field::Excluded), _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), } } fn visit_str(self, value: &str) -> Result where E: Error, { match value { "Unbounded" => Ok(Field::Unbounded), "Included" => Ok(Field::Included), "Excluded" => Ok(Field::Excluded), _ => Err(Error::unknown_variant(value, VARIANTS)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { b"Unbounded" => Ok(Field::Unbounded), b"Included" => Ok(Field::Included), b"Excluded" => Ok(Field::Excluded), _ => match str::from_utf8(value) { Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), Err(_) => { Err(Error::invalid_value(Unexpected::Bytes(value), &self)) } }, } } } deserializer.deserialize_identifier(FieldVisitor) } } struct BoundVisitor(PhantomData>); impl<'de, T> Visitor<'de> for BoundVisitor where T: Deserialize<'de>, { type Value = Bound; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("enum Bound") } fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { 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), } } } const VARIANTS: &[&str] = &["Unbounded", "Included", "Excluded"]; deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData)) } } //////////////////////////////////////////////////////////////////////////////// impl<'de, T, E> Deserialize<'de> for Result where T: Deserialize<'de>, E: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { // If this were outside of the serde crate, it would just use: // // #[derive(Deserialize)] // #[serde(variant_identifier)] enum Field { Ok, Err, } impl<'de> Deserialize<'de> for Field { #[inline] fn deserialize(deserializer: D) -> Result 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("`Ok` or `Err`") } fn visit_u64(self, value: u64) -> Result where E: Error, { match value { 0 => Ok(Field::Ok), 1 => Ok(Field::Err), _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &self)), } } fn visit_str(self, value: &str) -> Result where E: Error, { match value { "Ok" => Ok(Field::Ok), "Err" => Ok(Field::Err), _ => Err(Error::unknown_variant(value, VARIANTS)), } } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { b"Ok" => Ok(Field::Ok), b"Err" => Ok(Field::Err), _ => match str::from_utf8(value) { Ok(value) => Err(Error::unknown_variant(value, VARIANTS)), Err(_) => { Err(Error::invalid_value(Unexpected::Bytes(value), &self)) } }, } } } deserializer.deserialize_identifier(FieldVisitor) } } struct ResultVisitor(PhantomData>); impl<'de, T, E> Visitor<'de> for ResultVisitor where T: Deserialize<'de>, E: Deserialize<'de>, { type Value = Result; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("enum Result") } fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { match tri!(data.variant()) { (Field::Ok, v) => v.newtype_variant().map(Ok), (Field::Err, v) => v.newtype_variant().map(Err), } } } const VARIANTS: &[&str] = &["Ok", "Err"]; deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData)) } } //////////////////////////////////////////////////////////////////////////////// impl<'de, T> Deserialize<'de> for Wrapping where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Deserialize::deserialize(deserializer).map(Wrapping) } } #[cfg(all(feature = "std", not(no_std_atomic)))] macro_rules! atomic_impl { ($($ty:ident $size:expr)*) => { $( #[cfg(any(no_target_has_atomic, target_has_atomic = $size))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", target_has_atomic = $size))))] impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Deserialize::deserialize(deserializer).map(Self::new) } } )* }; } #[cfg(all(feature = "std", not(no_std_atomic)))] atomic_impl! { AtomicBool "8" AtomicI8 "8" AtomicI16 "16" AtomicI32 "32" AtomicIsize "ptr" AtomicU8 "8" AtomicU16 "16" AtomicU32 "32" AtomicUsize "ptr" } #[cfg(all(feature = "std", not(no_std_atomic64)))] atomic_impl! { AtomicI64 "64" AtomicU64 "64" } #[cfg(any(feature = "std", not(no_core_net)))] struct FromStrVisitor { expecting: &'static str, ty: PhantomData, } #[cfg(any(feature = "std", not(no_core_net)))] impl FromStrVisitor { fn new(expecting: &'static str) -> Self { FromStrVisitor { expecting, ty: PhantomData, } } } #[cfg(any(feature = "std", not(no_core_net)))] impl<'de, T> Visitor<'de> for FromStrVisitor where T: str::FromStr, T::Err: fmt::Display, { type Value = T; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(self.expecting) } fn visit_str(self, s: &str) -> Result where E: Error, { s.parse().map_err(Error::custom) } } serde-1.0.217/src/de/mod.rs000064400000000000000000002400051046102023000134030ustar 00000000000000//! Generic data structure deserialization framework. //! //! The two most important traits in this module are [`Deserialize`] and //! [`Deserializer`]. //! //! - **A type that implements `Deserialize` is a data structure** that can be //! deserialized from any data format supported by Serde, and conversely //! - **A type that implements `Deserializer` is a data format** that can //! deserialize any data structure supported by Serde. //! //! # The Deserialize trait //! //! Serde provides [`Deserialize`] implementations for many Rust primitive and //! standard library types. The complete list is below. All of these can be //! deserialized using Serde out of the box. //! //! Additionally, Serde provides a procedural macro called [`serde_derive`] to //! automatically generate [`Deserialize`] implementations for structs and enums //! in your program. See the [derive section of the manual] for how to use this. //! //! In rare cases it may be necessary to implement [`Deserialize`] manually for //! some type in your program. See the [Implementing `Deserialize`] section of //! the manual for more about this. //! //! Third-party crates may provide [`Deserialize`] implementations for types //! that they expose. For example the [`linked-hash-map`] crate provides a //! [`LinkedHashMap`] type that is deserializable by Serde because the //! crate provides an implementation of [`Deserialize`] for it. //! //! # The Deserializer trait //! //! [`Deserializer`] implementations are provided by third-party crates, for //! example [`serde_json`], [`serde_yaml`] and [`postcard`]. //! //! A partial list of well-maintained formats is given on the [Serde //! website][data formats]. //! //! # Implementations of Deserialize provided by Serde //! //! This is a slightly different set of types than what is supported for //! serialization. Some types can be serialized by Serde but not deserialized. //! One example is `OsStr`. //! //! - **Primitive types**: //! - bool //! - i8, i16, i32, i64, i128, isize //! - u8, u16, u32, u64, u128, usize //! - f32, f64 //! - char //! - **Compound types**: //! - \[T; 0\] through \[T; 32\] //! - tuples up to size 16 //! - **Common standard library types**: //! - String //! - Option\ //! - Result\ //! - PhantomData\ //! - **Wrapper types**: //! - Box\ //! - Box\<\[T\]\> //! - Box\ //! - Cow\<'a, T\> //! - Cell\ //! - RefCell\ //! - Mutex\ //! - RwLock\ //! - Rc\ *(if* features = \["rc"\] *is enabled)* //! - Arc\ *(if* features = \["rc"\] *is enabled)* //! - **Collection types**: //! - BTreeMap\ //! - BTreeSet\ //! - BinaryHeap\ //! - HashMap\ //! - HashSet\ //! - LinkedList\ //! - VecDeque\ //! - Vec\ //! - **Zero-copy types**: //! - &str //! - &\[u8\] //! - **FFI types**: //! - CString //! - Box\ //! - OsString //! - **Miscellaneous standard library types**: //! - Duration //! - SystemTime //! - Path //! - PathBuf //! - Range\ //! - RangeInclusive\ //! - Bound\ //! - num::NonZero* //! - `!` *(unstable)* //! - **Net types**: //! - IpAddr //! - Ipv4Addr //! - Ipv6Addr //! - SocketAddr //! - SocketAddrV4 //! - SocketAddrV6 //! //! [Implementing `Deserialize`]: https://serde.rs/impl-deserialize.html //! [`Deserialize`]: ../trait.Deserialize.html //! [`Deserializer`]: ../trait.Deserializer.html //! [`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html //! [`postcard`]: https://github.com/jamesmunns/postcard //! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map //! [`serde_derive`]: https://crates.io/crates/serde_derive //! [`serde_json`]: https://github.com/serde-rs/json //! [`serde_yaml`]: https://github.com/dtolnay/serde-yaml //! [derive section of the manual]: https://serde.rs/derive.html //! [data formats]: https://serde.rs/#data-formats use crate::lib::*; //////////////////////////////////////////////////////////////////////////////// pub mod value; mod ignored_any; mod impls; pub(crate) mod size_hint; pub use self::ignored_any::IgnoredAny; #[cfg(all(not(feature = "std"), no_core_error))] #[doc(no_inline)] pub use crate::std_error::Error as StdError; #[cfg(not(any(feature = "std", no_core_error)))] #[doc(no_inline)] pub use core::error::Error as StdError; #[cfg(feature = "std")] #[doc(no_inline)] pub use std::error::Error as StdError; //////////////////////////////////////////////////////////////////////////////// macro_rules! declare_error_trait { (Error: Sized $(+ $($supertrait:ident)::+)*) => { /// The `Error` trait allows `Deserialize` implementations to create descriptive /// error messages belonging to the `Deserializer` against which they are /// currently running. /// /// Every `Deserializer` declares an `Error` type that encompasses both /// general-purpose deserialization errors as well as errors specific to the /// particular deserialization format. For example the `Error` type of /// `serde_json` can represent errors like an invalid JSON escape sequence or an /// unterminated string literal, in addition to the error cases that are part of /// this trait. /// /// Most deserializers should only need to provide the `Error::custom` method /// and inherit the default behavior for the other methods. /// /// # Example implementation /// /// The [example data format] presented on the website shows an error /// type appropriate for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait Error: Sized $(+ $($supertrait)::+)* { /// Raised when there is general error when deserializing a type. /// /// The message should not be capitalized and should not end with a period. /// /// ```edition2021 /// # use std::str::FromStr; /// # /// # struct IpAddr; /// # /// # impl FromStr for IpAddr { /// # type Err = String; /// # /// # fn from_str(_: &str) -> Result { /// # unimplemented!() /// # } /// # } /// # /// use serde::de::{self, Deserialize, Deserializer}; /// /// impl<'de> Deserialize<'de> for IpAddr { /// fn deserialize(deserializer: D) -> Result /// where /// D: Deserializer<'de>, /// { /// let s = String::deserialize(deserializer)?; /// s.parse().map_err(de::Error::custom) /// } /// } /// ``` fn custom(msg: T) -> Self where T: Display; /// Raised when a `Deserialize` receives a type different from what it was /// expecting. /// /// The `unexp` argument provides information about what type was received. /// This is the type that was present in the input file or other source data /// of the Deserializer. /// /// The `exp` argument provides information about what type was being /// expected. This is the type that is written in the program. /// /// For example if we try to deserialize a String out of a JSON file /// containing an integer, the unexpected type is the integer and the /// expected type is the string. #[cold] fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self { Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp)) } /// Raised when a `Deserialize` receives a value of the right type but that /// is wrong for some other reason. /// /// The `unexp` argument provides information about what value was received. /// This is the value that was present in the input file or other source /// data of the Deserializer. /// /// The `exp` argument provides information about what value was being /// expected. This is the type that is written in the program. /// /// For example if we try to deserialize a String out of some binary data /// that is not valid UTF-8, the unexpected value is the bytes and the /// expected value is a string. #[cold] fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self { Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp)) } /// Raised when deserializing a sequence or map and the input data contains /// too many or too few elements. /// /// The `len` argument is the number of elements encountered. The sequence /// or map may have expected more arguments or fewer arguments. /// /// The `exp` argument provides information about what data was being /// expected. For example `exp` might say that a tuple of size 6 was /// expected. #[cold] fn invalid_length(len: usize, exp: &Expected) -> Self { Error::custom(format_args!("invalid length {}, expected {}", len, exp)) } /// Raised when a `Deserialize` enum type received a variant with an /// unrecognized name. #[cold] fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self { if expected.is_empty() { Error::custom(format_args!( "unknown variant `{}`, there are no variants", variant )) } else { Error::custom(format_args!( "unknown variant `{}`, expected {}", variant, OneOf { names: expected } )) } } /// Raised when a `Deserialize` struct type received a field with an /// unrecognized name. #[cold] fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self { if expected.is_empty() { Error::custom(format_args!( "unknown field `{}`, there are no fields", field )) } else { Error::custom(format_args!( "unknown field `{}`, expected {}", field, OneOf { names: expected } )) } } /// Raised when a `Deserialize` struct type expected to receive a required /// field with a particular name but that field was not present in the /// input. #[cold] fn missing_field(field: &'static str) -> Self { Error::custom(format_args!("missing field `{}`", field)) } /// Raised when a `Deserialize` struct type received more than one of the /// same field. #[cold] fn duplicate_field(field: &'static str) -> Self { Error::custom(format_args!("duplicate field `{}`", field)) } } } } #[cfg(feature = "std")] declare_error_trait!(Error: Sized + StdError); #[cfg(not(feature = "std"))] declare_error_trait!(Error: Sized + Debug + Display); /// `Unexpected` represents an unexpected invocation of any one of the `Visitor` /// trait methods. /// /// This is used as an argument to the `invalid_type`, `invalid_value`, and /// `invalid_length` methods of the `Error` trait to build error messages. /// /// ```edition2021 /// # use std::fmt; /// # /// # use serde::de::{self, Unexpected, Visitor}; /// # /// # struct Example; /// # /// # impl<'de> Visitor<'de> for Example { /// # type Value = (); /// # /// # fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// # write!(formatter, "definitely not a boolean") /// # } /// # /// fn visit_bool(self, v: bool) -> Result /// where /// E: de::Error, /// { /// Err(de::Error::invalid_type(Unexpected::Bool(v), &self)) /// } /// # } /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub enum Unexpected<'a> { /// The input contained a boolean value that was not expected. Bool(bool), /// The input contained an unsigned integer `u8`, `u16`, `u32` or `u64` that /// was not expected. Unsigned(u64), /// The input contained a signed integer `i8`, `i16`, `i32` or `i64` that /// was not expected. Signed(i64), /// The input contained a floating point `f32` or `f64` that was not /// expected. Float(f64), /// The input contained a `char` that was not expected. Char(char), /// The input contained a `&str` or `String` that was not expected. Str(&'a str), /// The input contained a `&[u8]` or `Vec` that was not expected. Bytes(&'a [u8]), /// The input contained a unit `()` that was not expected. Unit, /// The input contained an `Option` that was not expected. Option, /// The input contained a newtype struct that was not expected. NewtypeStruct, /// The input contained a sequence that was not expected. Seq, /// The input contained a map that was not expected. Map, /// The input contained an enum that was not expected. Enum, /// The input contained a unit variant that was not expected. UnitVariant, /// The input contained a newtype variant that was not expected. NewtypeVariant, /// The input contained a tuple variant that was not expected. TupleVariant, /// The input contained a struct variant that was not expected. StructVariant, /// A message stating what uncategorized thing the input contained that was /// not expected. /// /// The message should be a noun or noun phrase, not capitalized and without /// a period. An example message is "unoriginal superhero". Other(&'a str), } impl<'a> fmt::Display for Unexpected<'a> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { use self::Unexpected::*; match *self { Bool(b) => write!(formatter, "boolean `{}`", b), Unsigned(i) => write!(formatter, "integer `{}`", i), Signed(i) => write!(formatter, "integer `{}`", i), Float(f) => write!(formatter, "floating point `{}`", WithDecimalPoint(f)), Char(c) => write!(formatter, "character `{}`", c), Str(s) => write!(formatter, "string {:?}", s), Bytes(_) => formatter.write_str("byte array"), Unit => formatter.write_str("unit value"), Option => formatter.write_str("Option value"), NewtypeStruct => formatter.write_str("newtype struct"), Seq => formatter.write_str("sequence"), Map => formatter.write_str("map"), Enum => formatter.write_str("enum"), UnitVariant => formatter.write_str("unit variant"), NewtypeVariant => formatter.write_str("newtype variant"), TupleVariant => formatter.write_str("tuple variant"), StructVariant => formatter.write_str("struct variant"), Other(other) => formatter.write_str(other), } } } /// `Expected` represents an explanation of what data a `Visitor` was expecting /// to receive. /// /// This is used as an argument to the `invalid_type`, `invalid_value`, and /// `invalid_length` methods of the `Error` trait to build error messages. The /// message should be a noun or noun phrase that completes the sentence "This /// Visitor expects to receive ...", for example the message could be "an /// integer between 0 and 64". The message should not be capitalized and should /// not end with a period. /// /// Within the context of a `Visitor` implementation, the `Visitor` itself /// (`&self`) is an implementation of this trait. /// /// ```edition2021 /// # use serde::de::{self, Unexpected, Visitor}; /// # use std::fmt; /// # /// # struct Example; /// # /// # impl<'de> Visitor<'de> for Example { /// # type Value = (); /// # /// # fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// # write!(formatter, "definitely not a boolean") /// # } /// # /// fn visit_bool(self, v: bool) -> Result /// where /// E: de::Error, /// { /// Err(de::Error::invalid_type(Unexpected::Bool(v), &self)) /// } /// # } /// ``` /// /// Outside of a `Visitor`, `&"..."` can be used. /// /// ```edition2021 /// # use serde::de::{self, Unexpected}; /// # /// # fn example() -> Result<(), E> /// # where /// # E: de::Error, /// # { /// # let v = true; /// return Err(de::Error::invalid_type( /// Unexpected::Bool(v), /// &"a negative integer", /// )); /// # } /// ``` pub trait Expected { /// Format an explanation of what data was being expected. Same signature as /// the `Display` and `Debug` traits. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result; } impl<'de, T> Expected for T where T: Visitor<'de>, { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.expecting(formatter) } } impl Expected for &str { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(self) } } impl Display for Expected + '_ { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { Expected::fmt(self, formatter) } } //////////////////////////////////////////////////////////////////////////////// /// A **data structure** that can be deserialized from any data format supported /// by Serde. /// /// Serde provides `Deserialize` implementations for many Rust primitive and /// standard library types. The complete list is [here][crate::de]. All of these /// can be deserialized using Serde out of the box. /// /// Additionally, Serde provides a procedural macro called `serde_derive` to /// automatically generate `Deserialize` implementations for structs and enums /// in your program. See the [derive section of the manual][derive] for how to /// use this. /// /// In rare cases it may be necessary to implement `Deserialize` manually for /// some type in your program. See the [Implementing /// `Deserialize`][impl-deserialize] section of the manual for more about this. /// /// Third-party crates may provide `Deserialize` implementations for types that /// they expose. For example the `linked-hash-map` crate provides a /// `LinkedHashMap` type that is deserializable by Serde because the crate /// provides an implementation of `Deserialize` for it. /// /// [derive]: https://serde.rs/derive.html /// [impl-deserialize]: https://serde.rs/impl-deserialize.html /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by `Self` when deserialized. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html #[cfg_attr( not(no_diagnostic_namespace), diagnostic::on_unimplemented( note = "for local types consider adding `#[derive(serde::Deserialize)]` to your `{Self}` type", note = "for types from other crates check whether the crate offers a `serde` feature flag", ) )] pub trait Deserialize<'de>: Sized { /// Deserialize this value from the given Serde deserializer. /// /// See the [Implementing `Deserialize`][impl-deserialize] section of the /// manual for more information about how to implement this method. /// /// [impl-deserialize]: https://serde.rs/impl-deserialize.html fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>; /// Deserializes a value into `self` from the given Deserializer. /// /// The purpose of this method is to allow the deserializer to reuse /// resources and avoid copies. As such, if this method returns an error, /// `self` will be in an indeterminate state where some parts of the struct /// have been overwritten. Although whatever state that is will be /// memory-safe. /// /// This is generally useful when repeatedly deserializing values that /// are processed one at a time, where the value of `self` doesn't matter /// when the next deserialization occurs. /// /// If you manually implement this, your recursive deserializations should /// use `deserialize_in_place`. /// /// This method is stable and an official public API, but hidden from the /// documentation because it is almost never what newbies are looking for. /// Showing it in rustdoc would cause it to be featured more prominently /// than it deserves. #[doc(hidden)] fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> where D: Deserializer<'de>, { // Default implementation just delegates to `deserialize` impl. *place = tri!(Deserialize::deserialize(deserializer)); Ok(()) } } /// A data structure that can be deserialized without borrowing any data from /// the deserializer. /// /// This is primarily useful for trait bounds on functions. For example a /// `from_str` function may be able to deserialize a data structure that borrows /// from the input string, but a `from_reader` function may only deserialize /// owned data. /// /// ```edition2021 /// # use serde::de::{Deserialize, DeserializeOwned}; /// # use std::io::{Read, Result}; /// # /// # trait Ignore { /// fn from_str<'a, T>(s: &'a str) -> Result /// where /// T: Deserialize<'a>; /// /// fn from_reader(rdr: R) -> Result /// where /// R: Read, /// T: DeserializeOwned; /// # } /// ``` /// /// # Lifetime /// /// The relationship between `Deserialize` and `DeserializeOwned` in trait /// bounds is explained in more detail on the page [Understanding deserializer /// lifetimes]. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html pub trait DeserializeOwned: for<'de> Deserialize<'de> {} impl DeserializeOwned for T where T: for<'de> Deserialize<'de> {} /// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you /// ever find yourself looking for a way to pass data into a `Deserialize` impl, /// this trait is the way to do it. /// /// As one example of stateful deserialization consider deserializing a JSON /// array into an existing buffer. Using the `Deserialize` trait we could /// deserialize a JSON array into a `Vec` but it would be a freshly allocated /// `Vec`; there is no way for `Deserialize` to reuse a previously allocated /// buffer. Using `DeserializeSeed` instead makes this possible as in the /// example code below. /// /// The canonical API for stateless deserialization looks like this: /// /// ```edition2021 /// # use serde::Deserialize; /// # /// # enum Error {} /// # /// fn func<'de, T: Deserialize<'de>>() -> Result /// # { /// # unimplemented!() /// # } /// ``` /// /// Adjusting an API like this to support stateful deserialization is a matter /// of accepting a seed as input: /// /// ```edition2021 /// # use serde::de::DeserializeSeed; /// # /// # enum Error {} /// # /// fn func_seed<'de, T: DeserializeSeed<'de>>(seed: T) -> Result /// # { /// # let _ = seed; /// # unimplemented!() /// # } /// ``` /// /// In practice the majority of deserialization is stateless. An API expecting a /// seed can be appeased by passing `std::marker::PhantomData` as a seed in the /// case of stateless deserialization. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by `Self::Value` when deserialized. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example /// /// Suppose we have JSON that looks like `[[1, 2], [3, 4, 5], [6]]` and we need /// to deserialize it into a flat representation like `vec![1, 2, 3, 4, 5, 6]`. /// Allocating a brand new `Vec` for each subarray would be slow. Instead we /// would like to allocate a single `Vec` and then deserialize each subarray /// into it. This requires stateful deserialization using the `DeserializeSeed` /// trait. /// /// ```edition2021 /// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor}; /// use std::fmt; /// use std::marker::PhantomData; /// /// // 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. The `deserialize` method of /// // `ExtendVec` will be traversing the inner arrays of the JSON input and /// // appending each integer into the existing Vec. /// struct ExtendVec<'a, T: 'a>(&'a mut Vec); /// /// impl<'de, 'a, T> DeserializeSeed<'de> for ExtendVec<'a, T> /// where /// T: Deserialize<'de>, /// { /// // The return type of the `deserialize` method. This implementation /// // appends onto an existing vector but does not create any new data /// // structure, so the return type is (). /// type Value = (); /// /// fn deserialize(self, deserializer: D) -> Result /// where /// D: Deserializer<'de>, /// { /// // Visitor implementation that will walk an inner array of the JSON /// // input. /// struct ExtendVecVisitor<'a, T: 'a>(&'a mut Vec); /// /// impl<'de, 'a, T> Visitor<'de> for ExtendVecVisitor<'a, T> /// where /// T: Deserialize<'de>, /// { /// type Value = (); /// /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!(formatter, "an array of integers") /// } /// /// fn visit_seq(self, mut seq: A) -> Result<(), A::Error> /// where /// A: SeqAccess<'de>, /// { /// // Decrease the number of reallocations if there are many elements /// if let Some(size_hint) = seq.size_hint() { /// self.0.reserve(size_hint); /// } /// /// // Visit each element in the inner array and push it onto /// // the existing vector. /// while let Some(elem) = seq.next_element()? { /// self.0.push(elem); /// } /// Ok(()) /// } /// } /// /// deserializer.deserialize_seq(ExtendVecVisitor(self.0)) /// } /// } /// /// // Visitor implementation that will walk the outer array of the JSON input. /// struct FlattenedVecVisitor(PhantomData); /// /// impl<'de, T> Visitor<'de> for FlattenedVecVisitor /// where /// T: Deserialize<'de>, /// { /// // This Visitor constructs a single Vec to hold the flattened /// // contents of the inner arrays. /// type Value = Vec; /// /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!(formatter, "an array of arrays") /// } /// /// fn visit_seq(self, mut seq: A) -> Result, A::Error> /// where /// A: SeqAccess<'de>, /// { /// // Create a single Vec to hold the flattened contents. /// let mut vec = Vec::new(); /// /// // Each iteration through this loop is one inner array. /// while let Some(()) = seq.next_element_seed(ExtendVec(&mut vec))? { /// // Nothing to do; inner array has been appended into `vec`. /// } /// /// // Return the finished vec. /// Ok(vec) /// } /// } /// /// # fn example<'de, D>(deserializer: D) -> Result<(), D::Error> /// # where /// # D: Deserializer<'de>, /// # { /// let visitor = FlattenedVecVisitor(PhantomData); /// let flattened: Vec = deserializer.deserialize_seq(visitor)?; /// # Ok(()) /// # } /// ``` pub trait DeserializeSeed<'de>: Sized { /// The type produced by using this seed. type Value; /// Equivalent to the more common `Deserialize::deserialize` method, except /// with some initial piece of data (the seed) passed in. fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>; } impl<'de, T> DeserializeSeed<'de> for PhantomData where T: Deserialize<'de>, { type Value = T; #[inline] fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { T::deserialize(deserializer) } } //////////////////////////////////////////////////////////////////////////////// /// A **data format** that can deserialize any data structure supported by /// Serde. /// /// The role of this trait is to define the deserialization half of the [Serde /// data model], which is a way to categorize every Rust data type into one of /// 29 possible types. Each method of the `Deserializer` trait corresponds to one /// of the types of the data model. /// /// Implementations of `Deserialize` map themselves into this data model by /// passing to the `Deserializer` a `Visitor` implementation that can receive /// these various types. /// /// The types that make up the Serde data model are: /// /// - **14 primitive types** /// - bool /// - i8, i16, i32, i64, i128 /// - u8, u16, u32, u64, u128 /// - f32, f64 /// - char /// - **string** /// - UTF-8 bytes with a length and no null terminator. /// - When serializing, all strings are handled equally. When deserializing, /// there are three flavors of strings: transient, owned, and borrowed. /// - **byte array** - \[u8\] /// - Similar to strings, during deserialization byte arrays can be /// transient, owned, or borrowed. /// - **option** /// - Either none or some value. /// - **unit** /// - The type of `()` in Rust. It represents an anonymous value containing /// no data. /// - **unit_struct** /// - For example `struct Unit` or `PhantomData`. It represents a named /// value containing no data. /// - **unit_variant** /// - For example the `E::A` and `E::B` in `enum E { A, B }`. /// - **newtype_struct** /// - For example `struct Millimeters(u8)`. /// - **newtype_variant** /// - For example the `E::N` in `enum E { N(u8) }`. /// - **seq** /// - A variably sized heterogeneous sequence of values, for example `Vec` /// or `HashSet`. When serializing, the length may or may not be known /// before iterating through all the data. When deserializing, the length /// is determined by looking at the serialized data. /// - **tuple** /// - A statically sized heterogeneous sequence of values for which the /// length will be known at deserialization time without looking at the /// serialized data, for example `(u8,)` or `(String, u64, Vec)` or /// `[u64; 10]`. /// - **tuple_struct** /// - A named tuple, for example `struct Rgb(u8, u8, u8)`. /// - **tuple_variant** /// - For example the `E::T` in `enum E { T(u8, u8) }`. /// - **map** /// - A heterogeneous key-value pairing, for example `BTreeMap`. /// - **struct** /// - A heterogeneous key-value pairing in which the keys are strings and /// will be known at deserialization time without looking at the serialized /// data, for example `struct S { r: u8, g: u8, b: u8 }`. /// - **struct_variant** /// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`. /// /// The `Deserializer` trait supports two entry point styles which enables /// different kinds of deserialization. /// /// 1. The `deserialize_any` method. Self-describing data formats like JSON are /// able to look at the serialized data and tell what it represents. For /// example the JSON deserializer may see an opening curly brace (`{`) and /// know that it is seeing a map. If the data format supports /// `Deserializer::deserialize_any`, it will drive the Visitor using whatever /// type it sees in the input. JSON uses this approach when deserializing /// `serde_json::Value` which is an enum that can represent any JSON /// document. Without knowing what is in a JSON document, we can deserialize /// it to `serde_json::Value` by going through /// `Deserializer::deserialize_any`. /// /// 2. The various `deserialize_*` methods. Non-self-describing formats like /// Postcard need to be told what is in the input in order to deserialize it. /// The `deserialize_*` methods are hints to the deserializer for how to /// interpret the next piece of input. Non-self-describing formats are not /// able to deserialize something like `serde_json::Value` which relies on /// `Deserializer::deserialize_any`. /// /// When implementing `Deserialize`, you should avoid relying on /// `Deserializer::deserialize_any` unless you need to be told by the /// Deserializer what type is in the input. Know that relying on /// `Deserializer::deserialize_any` means your data type will be able to /// deserialize from self-describing formats only, ruling out Postcard and many /// others. /// /// [Serde data model]: https://serde.rs/data-model.html /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed from the input when deserializing. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example implementation /// /// The [example data format] presented on the website contains example code for /// a basic JSON `Deserializer`. /// /// [example data format]: https://serde.rs/data-format.html pub trait Deserializer<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; /// Require the `Deserializer` to figure out how to drive the visitor based /// on what data type is in the input. /// /// When implementing `Deserialize`, you should avoid relying on /// `Deserializer::deserialize_any` unless you need to be told by the /// Deserializer what type is in the input. Know that relying on /// `Deserializer::deserialize_any` means your data type will be able to /// deserialize from self-describing formats only, ruling out Postcard and /// many others. fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `bool` value. fn deserialize_bool(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i8` value. fn deserialize_i8(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i16` value. fn deserialize_i16(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i32` value. fn deserialize_i32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i64` value. fn deserialize_i64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i128` value. /// /// The default behavior unconditionally returns an error. fn deserialize_i128(self, visitor: V) -> Result where V: Visitor<'de>, { let _ = visitor; Err(Error::custom("i128 is not supported")) } /// Hint that the `Deserialize` type is expecting a `u8` value. fn deserialize_u8(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u16` value. fn deserialize_u16(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u32` value. fn deserialize_u32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u64` value. fn deserialize_u64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `u128` value. /// /// The default behavior unconditionally returns an error. fn deserialize_u128(self, visitor: V) -> Result where V: Visitor<'de>, { let _ = visitor; Err(Error::custom("u128 is not supported")) } /// Hint that the `Deserialize` type is expecting a `f32` value. fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `f64` value. fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `char` value. fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a string value and does /// not benefit from taking ownership of buffered data owned by the /// `Deserializer`. /// /// If the `Visitor` would benefit from taking ownership of `String` data, /// indicate this to the `Deserializer` by using `deserialize_string` /// instead. fn deserialize_str(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a string value and would /// benefit from taking ownership of buffered data owned by the /// `Deserializer`. /// /// If the `Visitor` would not benefit from taking ownership of `String` /// data, indicate that to the `Deserializer` by using `deserialize_str` /// instead. fn deserialize_string(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a byte array and does not /// benefit from taking ownership of buffered data owned by the /// `Deserializer`. /// /// If the `Visitor` would benefit from taking ownership of `Vec` data, /// indicate this to the `Deserializer` by using `deserialize_byte_buf` /// instead. fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a byte array and would /// benefit from taking ownership of buffered data owned by the /// `Deserializer`. /// /// If the `Visitor` would not benefit from taking ownership of `Vec` /// data, indicate that to the `Deserializer` by using `deserialize_bytes` /// instead. fn deserialize_byte_buf(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an optional value. /// /// This allows deserializers that encode an optional value as a nullable /// value to convert the null value into `None` and a regular value into /// `Some(value)`. fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a unit value. fn deserialize_unit(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a unit struct with a /// particular name. fn deserialize_unit_struct( self, name: &'static str, visitor: V, ) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a newtype struct with a /// particular name. fn deserialize_newtype_struct( self, name: &'static str, visitor: V, ) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a sequence of values. fn deserialize_seq(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a sequence of values and /// knows how many values there are without looking at the serialized data. fn deserialize_tuple(self, len: usize, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a tuple struct with a /// particular name and number of fields. fn deserialize_tuple_struct( self, name: &'static str, len: usize, visitor: V, ) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a map of key-value pairs. fn deserialize_map(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a struct with a particular /// name and fields. fn deserialize_struct( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an enum value with a /// particular name and possible variants. fn deserialize_enum( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting the name of a struct /// field or the discriminant of an enum variant. fn deserialize_identifier(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type needs to deserialize a value whose type /// doesn't matter because it is ignored. /// /// Deserializers for non-self-describing formats may not support this mode. fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>; /// Determine whether `Deserialize` implementations should expect to /// deserialize their human-readable form. /// /// Some types have a human-readable form that may be somewhat expensive to /// construct, as well as a binary form that is compact and efficient. /// Generally text-based formats like JSON and YAML will prefer to use the /// human-readable one and binary formats like Postcard will prefer the /// compact one. /// /// ```edition2021 /// # use std::ops::Add; /// # use std::str::FromStr; /// # /// # struct Timestamp; /// # /// # impl Timestamp { /// # const EPOCH: Timestamp = Timestamp; /// # } /// # /// # impl FromStr for Timestamp { /// # type Err = String; /// # fn from_str(_: &str) -> Result { /// # unimplemented!() /// # } /// # } /// # /// # struct Duration; /// # /// # impl Duration { /// # fn seconds(_: u64) -> Self { unimplemented!() } /// # } /// # /// # impl Add for Timestamp { /// # type Output = Timestamp; /// # fn add(self, _: Duration) -> Self::Output { /// # unimplemented!() /// # } /// # } /// # /// use serde::de::{self, Deserialize, Deserializer}; /// /// impl<'de> Deserialize<'de> for Timestamp { /// fn deserialize(deserializer: D) -> Result /// where /// D: Deserializer<'de>, /// { /// if deserializer.is_human_readable() { /// // Deserialize from a human-readable string like "2015-05-15T17:01:00Z". /// let s = String::deserialize(deserializer)?; /// Timestamp::from_str(&s).map_err(de::Error::custom) /// } else { /// // Deserialize from a compact binary representation, seconds since /// // the Unix epoch. /// let n = u64::deserialize(deserializer)?; /// Ok(Timestamp::EPOCH + Duration::seconds(n)) /// } /// } /// } /// ``` /// /// The default implementation of this method returns `true`. Data formats /// may override this to `false` to request a compact form for types that /// support one. Note that modifying this method to change a format from /// human-readable to compact or vice versa should be regarded as a breaking /// change, as a value serialized in human-readable mode is not required to /// deserialize from the same data in compact mode. #[inline] fn is_human_readable(&self) -> bool { true } // Not public API. #[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))] #[doc(hidden)] fn __deserialize_content( self, _: crate::actually_private::T, visitor: V, ) -> Result, Self::Error> where V: Visitor<'de, Value = crate::__private::de::Content<'de>>, { self.deserialize_any(visitor) } } //////////////////////////////////////////////////////////////////////////////// /// This trait represents a visitor that walks through a deserializer. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the requirement for lifetime of data /// that may be borrowed by `Self::Value`. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example /// /// ```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. /// struct LongString { /// min: usize, /// } /// /// impl<'de> Visitor<'de> for LongString { /// type Value = String; /// /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!(formatter, "a string containing at least {} bytes", self.min) /// } /// /// fn visit_str(self, s: &str) -> Result /// where /// E: de::Error, /// { /// if s.len() >= self.min { /// Ok(s.to_owned()) /// } else { /// Err(de::Error::invalid_value(Unexpected::Str(s), &self)) /// } /// } /// } /// ``` pub trait Visitor<'de>: Sized { /// The value produced by this visitor. type Value; /// Format a message stating what data this Visitor expects to receive. /// /// This is used in error messages. The message should complete the sentence /// "This Visitor expects to receive ...", for example the message could be /// "an integer between 0 and 64". The message should not be capitalized and /// should not end with a period. /// /// ```edition2021 /// # use std::fmt; /// # /// # struct S { /// # max: usize, /// # } /// # /// # impl<'de> serde::de::Visitor<'de> for S { /// # type Value = (); /// # /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!(formatter, "an integer between 0 and {}", self.max) /// } /// # } /// ``` fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result; /// The input contains a boolean. /// /// The default implementation fails with a type error. fn visit_bool(self, v: bool) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Bool(v), &self)) } /// The input contains an `i8`. /// /// The default implementation forwards to [`visit_i64`]. /// /// [`visit_i64`]: #method.visit_i64 fn visit_i8(self, v: i8) -> Result where E: Error, { self.visit_i64(v as i64) } /// The input contains an `i16`. /// /// The default implementation forwards to [`visit_i64`]. /// /// [`visit_i64`]: #method.visit_i64 fn visit_i16(self, v: i16) -> Result where E: Error, { self.visit_i64(v as i64) } /// The input contains an `i32`. /// /// The default implementation forwards to [`visit_i64`]. /// /// [`visit_i64`]: #method.visit_i64 fn visit_i32(self, v: i32) -> Result where E: Error, { self.visit_i64(v as i64) } /// The input contains an `i64`. /// /// The default implementation fails with a type error. fn visit_i64(self, v: i64) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Signed(v), &self)) } /// The input contains a `i128`. /// /// The default implementation fails with a type error. fn visit_i128(self, v: i128) -> Result where E: Error, { let mut buf = [0u8; 58]; let mut writer = crate::format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap(); Err(Error::invalid_type( Unexpected::Other(writer.as_str()), &self, )) } /// The input contains a `u8`. /// /// The default implementation forwards to [`visit_u64`]. /// /// [`visit_u64`]: #method.visit_u64 fn visit_u8(self, v: u8) -> Result where E: Error, { self.visit_u64(v as u64) } /// The input contains a `u16`. /// /// The default implementation forwards to [`visit_u64`]. /// /// [`visit_u64`]: #method.visit_u64 fn visit_u16(self, v: u16) -> Result where E: Error, { self.visit_u64(v as u64) } /// The input contains a `u32`. /// /// The default implementation forwards to [`visit_u64`]. /// /// [`visit_u64`]: #method.visit_u64 fn visit_u32(self, v: u32) -> Result where E: Error, { self.visit_u64(v as u64) } /// The input contains a `u64`. /// /// The default implementation fails with a type error. fn visit_u64(self, v: u64) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Unsigned(v), &self)) } /// The input contains a `u128`. /// /// The default implementation fails with a type error. fn visit_u128(self, v: u128) -> Result where E: Error, { let mut buf = [0u8; 57]; let mut writer = crate::format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap(); Err(Error::invalid_type( Unexpected::Other(writer.as_str()), &self, )) } /// The input contains an `f32`. /// /// The default implementation forwards to [`visit_f64`]. /// /// [`visit_f64`]: #method.visit_f64 fn visit_f32(self, v: f32) -> Result where E: Error, { self.visit_f64(v as f64) } /// The input contains an `f64`. /// /// The default implementation fails with a type error. fn visit_f64(self, v: f64) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Float(v), &self)) } /// The input contains a `char`. /// /// The default implementation forwards to [`visit_str`] as a one-character /// string. /// /// [`visit_str`]: #method.visit_str #[inline] fn visit_char(self, v: char) -> Result where E: Error, { self.visit_str(v.encode_utf8(&mut [0u8; 4])) } /// The input contains a string. The lifetime of the string is ephemeral and /// it may be destroyed after this method returns. /// /// This method allows the `Deserializer` to avoid a copy by retaining /// ownership of any buffered data. `Deserialize` implementations that do /// not benefit from taking ownership of `String` data should indicate that /// to the deserializer by using `Deserializer::deserialize_str` rather than /// `Deserializer::deserialize_string`. /// /// It is never correct to implement `visit_string` without implementing /// `visit_str`. Implement neither, both, or just `visit_str`. fn visit_str(self, v: &str) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Str(v), &self)) } /// The input contains a string that lives at least as long as the /// `Deserializer`. /// /// This enables zero-copy deserialization of strings in some formats. For /// example JSON input containing the JSON string `"borrowed"` can be /// deserialized with zero copying into a `&'a str` as long as the input /// data outlives `'a`. /// /// The default implementation forwards to `visit_str`. #[inline] fn visit_borrowed_str(self, v: &'de str) -> Result where E: Error, { self.visit_str(v) } /// The input contains a string and ownership of the string is being given /// to the `Visitor`. /// /// This method allows the `Visitor` to avoid a copy by taking ownership of /// a string created by the `Deserializer`. `Deserialize` implementations /// that benefit from taking ownership of `String` data should indicate that /// to the deserializer by using `Deserializer::deserialize_string` rather /// than `Deserializer::deserialize_str`, although not every deserializer /// will honor such a request. /// /// It is never correct to implement `visit_string` without implementing /// `visit_str`. Implement neither, both, or just `visit_str`. /// /// The default implementation forwards to `visit_str` and then drops the /// `String`. #[inline] #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] fn visit_string(self, v: String) -> Result where E: Error, { self.visit_str(&v) } /// The input contains a byte array. The lifetime of the byte array is /// ephemeral and it may be destroyed after this method returns. /// /// This method allows the `Deserializer` to avoid a copy by retaining /// ownership of any buffered data. `Deserialize` implementations that do /// not benefit from taking ownership of `Vec` data should indicate that /// to the deserializer by using `Deserializer::deserialize_bytes` rather /// than `Deserializer::deserialize_byte_buf`. /// /// It is never correct to implement `visit_byte_buf` without implementing /// `visit_bytes`. Implement neither, both, or just `visit_bytes`. fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Bytes(v), &self)) } /// The input contains a byte array that lives at least as long as the /// `Deserializer`. /// /// This enables zero-copy deserialization of bytes in some formats. For /// example Postcard data containing bytes can be deserialized with zero /// copying into a `&'a [u8]` as long as the input data outlives `'a`. /// /// The default implementation forwards to `visit_bytes`. #[inline] fn visit_borrowed_bytes(self, v: &'de [u8]) -> Result where E: Error, { self.visit_bytes(v) } /// The input contains a byte array and ownership of the byte array is being /// given to the `Visitor`. /// /// This method allows the `Visitor` to avoid a copy by taking ownership of /// a byte buffer created by the `Deserializer`. `Deserialize` /// implementations that benefit from taking ownership of `Vec` data /// should indicate that to the deserializer by using /// `Deserializer::deserialize_byte_buf` rather than /// `Deserializer::deserialize_bytes`, although not every deserializer will /// honor such a request. /// /// It is never correct to implement `visit_byte_buf` without implementing /// `visit_bytes`. Implement neither, both, or just `visit_bytes`. /// /// The default implementation forwards to `visit_bytes` and then drops the /// `Vec`. #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { self.visit_bytes(&v) } /// The input contains an optional that is absent. /// /// The default implementation fails with a type error. fn visit_none(self) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Option, &self)) } /// The input contains an optional that is present. /// /// The default implementation fails with a type error. fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de>, { let _ = deserializer; Err(Error::invalid_type(Unexpected::Option, &self)) } /// The input contains a unit `()`. /// /// The default implementation fails with a type error. fn visit_unit(self) -> Result where E: Error, { Err(Error::invalid_type(Unexpected::Unit, &self)) } /// The input contains a newtype struct. /// /// The content of the newtype struct may be read from the given /// `Deserializer`. /// /// The default implementation fails with a type error. fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de>, { let _ = deserializer; Err(Error::invalid_type(Unexpected::NewtypeStruct, &self)) } /// The input contains a sequence of elements. /// /// The default implementation fails with a type error. fn visit_seq(self, seq: A) -> Result where A: SeqAccess<'de>, { let _ = seq; Err(Error::invalid_type(Unexpected::Seq, &self)) } /// The input contains a key-value map. /// /// The default implementation fails with a type error. fn visit_map(self, map: A) -> Result where A: MapAccess<'de>, { let _ = map; Err(Error::invalid_type(Unexpected::Map, &self)) } /// The input contains an enum. /// /// The default implementation fails with a type error. fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { let _ = data; Err(Error::invalid_type(Unexpected::Enum, &self)) } // Used when deserializing a flattened Option field. Not public API. #[doc(hidden)] fn __private_visit_untagged_option(self, _: D) -> Result where D: Deserializer<'de>, { Err(()) } } //////////////////////////////////////////////////////////////////////////////// /// Provides a `Visitor` access to each element of a sequence in the input. /// /// This is a trait that a `Deserializer` passes to a `Visitor` implementation, /// which deserializes each item in a sequence. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by deserialized sequence elements. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SeqAccess` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SeqAccess<'de> { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; /// This returns `Ok(Some(value))` for the next value in the sequence, or /// `Ok(None)` if there are no more remaining items. /// /// `Deserialize` implementations should typically use /// `SeqAccess::next_element` instead. fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: DeserializeSeed<'de>; /// This returns `Ok(Some(value))` for the next value in the sequence, or /// `Ok(None)` if there are no more remaining items. /// /// This method exists as a convenience for `Deserialize` implementations. /// `SeqAccess` implementations should not override the default behavior. #[inline] fn next_element(&mut self) -> Result, Self::Error> where T: Deserialize<'de>, { self.next_element_seed(PhantomData) } /// Returns the number of elements remaining in the sequence, if known. #[inline] fn size_hint(&self) -> Option { None } } impl<'de, A> SeqAccess<'de> for &mut A where A: ?Sized + SeqAccess<'de>, { type Error = A::Error; #[inline] fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: DeserializeSeed<'de>, { (**self).next_element_seed(seed) } #[inline] fn next_element(&mut self) -> Result, Self::Error> where T: Deserialize<'de>, { (**self).next_element() } #[inline] fn size_hint(&self) -> Option { (**self).size_hint() } } //////////////////////////////////////////////////////////////////////////////// /// Provides a `Visitor` access to each entry of a map in the input. /// /// This is a trait that a `Deserializer` passes to a `Visitor` implementation. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by deserialized map entries. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `MapAccess` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait MapAccess<'de> { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; /// This returns `Ok(Some(key))` for the next key in the map, or `Ok(None)` /// if there are no more remaining entries. /// /// `Deserialize` implementations should typically use /// `MapAccess::next_key` or `MapAccess::next_entry` instead. fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> where K: DeserializeSeed<'de>; /// This returns a `Ok(value)` for the next value in the map. /// /// `Deserialize` implementations should typically use /// `MapAccess::next_value` instead. /// /// # Panics /// /// Calling `next_value_seed` before `next_key_seed` is incorrect and is /// allowed to panic or return bogus results. fn next_value_seed(&mut self, seed: V) -> Result where V: DeserializeSeed<'de>; /// This returns `Ok(Some((key, value)))` for the next (key-value) pair in /// the map, or `Ok(None)` if there are no more remaining items. /// /// `MapAccess` implementations should override the default behavior if a /// more efficient implementation is possible. /// /// `Deserialize` implementations should typically use /// `MapAccess::next_entry` instead. #[inline] fn next_entry_seed( &mut self, kseed: K, vseed: V, ) -> Result, Self::Error> where K: DeserializeSeed<'de>, V: DeserializeSeed<'de>, { match tri!(self.next_key_seed(kseed)) { Some(key) => { let value = tri!(self.next_value_seed(vseed)); Ok(Some((key, value))) } None => Ok(None), } } /// This returns `Ok(Some(key))` for the next key in the map, or `Ok(None)` /// if there are no more remaining entries. /// /// This method exists as a convenience for `Deserialize` implementations. /// `MapAccess` implementations should not override the default behavior. #[inline] fn next_key(&mut self) -> Result, Self::Error> where K: Deserialize<'de>, { self.next_key_seed(PhantomData) } /// This returns a `Ok(value)` for the next value in the map. /// /// This method exists as a convenience for `Deserialize` implementations. /// `MapAccess` implementations should not override the default behavior. /// /// # Panics /// /// Calling `next_value` before `next_key` is incorrect and is allowed to /// panic or return bogus results. #[inline] fn next_value(&mut self) -> Result where V: Deserialize<'de>, { self.next_value_seed(PhantomData) } /// This returns `Ok(Some((key, value)))` for the next (key-value) pair in /// the map, or `Ok(None)` if there are no more remaining items. /// /// This method exists as a convenience for `Deserialize` implementations. /// `MapAccess` implementations should not override the default behavior. #[inline] fn next_entry(&mut self) -> Result, Self::Error> where K: Deserialize<'de>, V: Deserialize<'de>, { self.next_entry_seed(PhantomData, PhantomData) } /// Returns the number of entries remaining in the map, if known. #[inline] fn size_hint(&self) -> Option { None } } impl<'de, A> MapAccess<'de> for &mut A where A: ?Sized + MapAccess<'de>, { type Error = A::Error; #[inline] fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> where K: DeserializeSeed<'de>, { (**self).next_key_seed(seed) } #[inline] fn next_value_seed(&mut self, seed: V) -> Result where V: DeserializeSeed<'de>, { (**self).next_value_seed(seed) } #[inline] fn next_entry_seed( &mut self, kseed: K, vseed: V, ) -> Result, Self::Error> where K: DeserializeSeed<'de>, V: DeserializeSeed<'de>, { (**self).next_entry_seed(kseed, vseed) } #[inline] fn next_entry(&mut self) -> Result, Self::Error> where K: Deserialize<'de>, V: Deserialize<'de>, { (**self).next_entry() } #[inline] fn next_key(&mut self) -> Result, Self::Error> where K: Deserialize<'de>, { (**self).next_key() } #[inline] fn next_value(&mut self) -> Result where V: Deserialize<'de>, { (**self).next_value() } #[inline] fn size_hint(&self) -> Option { (**self).size_hint() } } //////////////////////////////////////////////////////////////////////////////// /// Provides a `Visitor` access to the data of an enum in the input. /// /// `EnumAccess` is created by the `Deserializer` and passed to the /// `Visitor` in order to identify which variant of an enum to deserialize. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by the deserialized enum variant. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `EnumAccess` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait EnumAccess<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; /// The `Visitor` that will be used to deserialize the content of the enum /// variant. type Variant: VariantAccess<'de, Error = Self::Error>; /// `variant` is called to identify which variant to deserialize. /// /// `Deserialize` implementations should typically use `EnumAccess::variant` /// instead. fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: DeserializeSeed<'de>; /// `variant` is called to identify which variant to deserialize. /// /// This method exists as a convenience for `Deserialize` implementations. /// `EnumAccess` implementations should not override the default behavior. #[inline] fn variant(self) -> Result<(V, Self::Variant), Self::Error> where V: Deserialize<'de>, { self.variant_seed(PhantomData) } } /// `VariantAccess` is a visitor that is created by the `Deserializer` and /// passed to the `Deserialize` to deserialize the content of a particular enum /// variant. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed by the deserialized enum variant. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `VariantAccess` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait VariantAccess<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. Must match the error type of our `EnumAccess`. type Error: Error; /// Called when deserializing a variant with no values. /// /// If the data contains a different type of variant, the following /// `invalid_type` error should be constructed: /// /// ```edition2021 /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; /// # /// # struct X; /// # /// # impl<'de> VariantAccess<'de> for X { /// # type Error = value::Error; /// # /// fn unit_variant(self) -> Result<(), Self::Error> { /// // What the data actually contained; suppose it is a tuple variant. /// let unexp = Unexpected::TupleVariant; /// Err(de::Error::invalid_type(unexp, &"unit variant")) /// } /// # /// # fn newtype_variant_seed(self, _: T) -> Result /// # where /// # T: DeserializeSeed<'de>, /// # { unimplemented!() } /// # /// # fn tuple_variant(self, _: usize, _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # /// # fn struct_variant(self, _: &[&str], _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # } /// ``` fn unit_variant(self) -> Result<(), Self::Error>; /// Called when deserializing a variant with a single value. /// /// `Deserialize` implementations should typically use /// `VariantAccess::newtype_variant` instead. /// /// If the data contains a different type of variant, the following /// `invalid_type` error should be constructed: /// /// ```edition2021 /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; /// # /// # struct X; /// # /// # impl<'de> VariantAccess<'de> for X { /// # type Error = value::Error; /// # /// # fn unit_variant(self) -> Result<(), Self::Error> { /// # unimplemented!() /// # } /// # /// fn newtype_variant_seed(self, _seed: T) -> Result /// where /// T: DeserializeSeed<'de>, /// { /// // What the data actually contained; suppose it is a unit variant. /// let unexp = Unexpected::UnitVariant; /// Err(de::Error::invalid_type(unexp, &"newtype variant")) /// } /// # /// # fn tuple_variant(self, _: usize, _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # /// # fn struct_variant(self, _: &[&str], _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # } /// ``` fn newtype_variant_seed(self, seed: T) -> Result where T: DeserializeSeed<'de>; /// Called when deserializing a variant with a single value. /// /// This method exists as a convenience for `Deserialize` implementations. /// `VariantAccess` implementations should not override the default /// behavior. #[inline] fn newtype_variant(self) -> Result where T: Deserialize<'de>, { self.newtype_variant_seed(PhantomData) } /// Called when deserializing a tuple-like variant. /// /// The `len` is the number of fields expected in the tuple variant. /// /// If the data contains a different type of variant, the following /// `invalid_type` error should be constructed: /// /// ```edition2021 /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; /// # /// # struct X; /// # /// # impl<'de> VariantAccess<'de> for X { /// # type Error = value::Error; /// # /// # fn unit_variant(self) -> Result<(), Self::Error> { /// # unimplemented!() /// # } /// # /// # fn newtype_variant_seed(self, _: T) -> Result /// # where /// # T: DeserializeSeed<'de>, /// # { unimplemented!() } /// # /// fn tuple_variant(self, _len: usize, _visitor: V) -> Result /// where /// V: Visitor<'de>, /// { /// // What the data actually contained; suppose it is a unit variant. /// let unexp = Unexpected::UnitVariant; /// Err(de::Error::invalid_type(unexp, &"tuple variant")) /// } /// # /// # fn struct_variant(self, _: &[&str], _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # } /// ``` fn tuple_variant(self, len: usize, visitor: V) -> Result where V: Visitor<'de>; /// Called when deserializing a struct-like variant. /// /// The `fields` are the names of the fields of the struct variant. /// /// If the data contains a different type of variant, the following /// `invalid_type` error should be constructed: /// /// ```edition2021 /// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; /// # /// # struct X; /// # /// # impl<'de> VariantAccess<'de> for X { /// # type Error = value::Error; /// # /// # fn unit_variant(self) -> Result<(), Self::Error> { /// # unimplemented!() /// # } /// # /// # fn newtype_variant_seed(self, _: T) -> Result /// # where /// # T: DeserializeSeed<'de>, /// # { unimplemented!() } /// # /// # fn tuple_variant(self, _: usize, _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { unimplemented!() } /// # /// fn struct_variant( /// self, /// _fields: &'static [&'static str], /// _visitor: V, /// ) -> Result /// where /// V: Visitor<'de>, /// { /// // What the data actually contained; suppose it is a unit variant. /// let unexp = Unexpected::UnitVariant; /// Err(de::Error::invalid_type(unexp, &"struct variant")) /// } /// # } /// ``` fn struct_variant( self, fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>; } //////////////////////////////////////////////////////////////////////////////// /// Converts an existing value into a `Deserializer` from which other values can /// be deserialized. /// /// # Lifetime /// /// The `'de` lifetime of this trait is the lifetime of data that may be /// borrowed from the resulting `Deserializer`. See the page [Understanding /// deserializer lifetimes] for a more detailed explanation of these lifetimes. /// /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html /// /// # Example /// /// ```edition2021 /// use serde::de::{value, Deserialize, IntoDeserializer}; /// use serde_derive::Deserialize; /// use std::str::FromStr; /// /// #[derive(Deserialize)] /// enum Setting { /// On, /// Off, /// } /// /// impl FromStr for Setting { /// type Err = value::Error; /// /// fn from_str(s: &str) -> Result { /// Self::deserialize(s.into_deserializer()) /// } /// } /// ``` pub trait IntoDeserializer<'de, E: Error = value::Error> { /// The type of the deserializer being converted into. type Deserializer: Deserializer<'de, Error = E>; /// Convert this value into a deserializer. fn into_deserializer(self) -> Self::Deserializer; } //////////////////////////////////////////////////////////////////////////////// /// Used in error messages. /// /// - expected `a` /// - expected `a` or `b` /// - expected one of `a`, `b`, `c` /// /// The slice of names must not be empty. struct OneOf { names: &'static [&'static str], } impl Display for OneOf { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self.names.len() { 0 => panic!(), // special case elsewhere 1 => write!(formatter, "`{}`", self.names[0]), 2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]), _ => { tri!(formatter.write_str("one of ")); for (i, alt) in self.names.iter().enumerate() { if i > 0 { tri!(formatter.write_str(", ")); } tri!(write!(formatter, "`{}`", alt)); } Ok(()) } } } } struct WithDecimalPoint(f64); impl Display for WithDecimalPoint { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { struct LookForDecimalPoint<'f, 'a> { formatter: &'f mut fmt::Formatter<'a>, has_decimal_point: bool, } impl<'f, 'a> fmt::Write for LookForDecimalPoint<'f, 'a> { fn write_str(&mut self, fragment: &str) -> fmt::Result { self.has_decimal_point |= fragment.contains('.'); self.formatter.write_str(fragment) } fn write_char(&mut self, ch: char) -> fmt::Result { self.has_decimal_point |= ch == '.'; self.formatter.write_char(ch) } } if self.0.is_finite() { let mut writer = LookForDecimalPoint { formatter, has_decimal_point: false, }; tri!(write!(writer, "{}", self.0)); if !writer.has_decimal_point { tri!(formatter.write_str(".0")); } } else { tri!(write!(formatter, "{}", self.0)); } Ok(()) } } serde-1.0.217/src/de/seed.rs000064400000000000000000000010631046102023000135430ustar 00000000000000use crate::de::{Deserialize, DeserializeSeed, Deserializer}; /// A DeserializeSeed helper for implementing deserialize_in_place Visitors. /// /// Wraps a mutable reference and calls deserialize_in_place on it. pub struct InPlaceSeed<'a, T: 'a>(pub &'a mut T); impl<'a, 'de, T> DeserializeSeed<'de> for InPlaceSeed<'a, T> where T: Deserialize<'de>, { type Value = (); fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { T::deserialize_in_place(deserializer, self.0) } } serde-1.0.217/src/de/size_hint.rs000064400000000000000000000012041046102023000146140ustar 00000000000000use crate::lib::*; pub fn from_bounds(iter: &I) -> Option where I: Iterator, { helper(iter.size_hint()) } #[cfg(any(feature = "std", feature = "alloc"))] pub fn cautious(hint: Option) -> usize { const MAX_PREALLOC_BYTES: usize = 1024 * 1024; if mem::size_of::() == 0 { 0 } else { cmp::min( hint.unwrap_or(0), MAX_PREALLOC_BYTES / mem::size_of::(), ) } } fn helper(bounds: (usize, Option)) -> Option { match bounds { (lower, Some(upper)) if lower == upper => Some(upper), _ => None, } } serde-1.0.217/src/de/value.rs000064400000000000000000001372741046102023000137550ustar 00000000000000//! Building blocks for deserializing basic values using the `IntoDeserializer` //! trait. //! //! ```edition2021 //! use serde::de::{value, Deserialize, IntoDeserializer}; //! use serde_derive::Deserialize; //! use std::str::FromStr; //! //! #[derive(Deserialize)] //! enum Setting { //! On, //! Off, //! } //! //! impl FromStr for Setting { //! type Err = value::Error; //! //! fn from_str(s: &str) -> Result { //! Self::deserialize(s.into_deserializer()) //! } //! } //! ``` use crate::lib::*; use self::private::{First, Second}; use crate::de::{self, size_hint, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor}; use crate::ser; //////////////////////////////////////////////////////////////////////////////// // For structs that contain a PhantomData. We do not want the trait // bound `E: Clone` inferred by derive(Clone). macro_rules! impl_copy_clone { ($ty:ident $(<$lifetime:tt>)*) => { impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {} impl<$($lifetime,)* E> Clone for $ty<$($lifetime,)* E> { fn clone(&self) -> Self { *self } } }; } //////////////////////////////////////////////////////////////////////////////// /// A minimal representation of all possible errors that can occur using the /// `IntoDeserializer` trait. #[derive(Clone, PartialEq)] pub struct Error { err: ErrorImpl, } #[cfg(any(feature = "std", feature = "alloc"))] type ErrorImpl = Box; #[cfg(not(any(feature = "std", feature = "alloc")))] type ErrorImpl = (); impl de::Error for Error { #[cfg(any(feature = "std", feature = "alloc"))] #[cold] fn custom(msg: T) -> Self where T: Display, { Error { err: msg.to_string().into_boxed_str(), } } #[cfg(not(any(feature = "std", feature = "alloc")))] #[cold] fn custom(msg: T) -> Self where T: Display, { let _ = msg; Error { err: () } } } impl ser::Error for Error { #[cold] fn custom(msg: T) -> Self where T: Display, { de::Error::custom(msg) } } impl Display for Error { #[cfg(any(feature = "std", feature = "alloc"))] fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(&self.err) } #[cfg(not(any(feature = "std", feature = "alloc")))] fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("Serde deserialization error") } } impl Debug for Error { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { let mut debug = formatter.debug_tuple("Error"); #[cfg(any(feature = "std", feature = "alloc"))] debug.field(&self.err); debug.finish() } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl error::Error for Error { fn description(&self) -> &str { &self.err } } //////////////////////////////////////////////////////////////////////////////// impl<'de, E> IntoDeserializer<'de, E> for () where E: de::Error, { type Deserializer = UnitDeserializer; fn into_deserializer(self) -> UnitDeserializer { UnitDeserializer::new() } } /// A deserializer holding a `()`. pub struct UnitDeserializer { marker: PhantomData, } impl_copy_clone!(UnitDeserializer); impl UnitDeserializer { #[allow(missing_docs)] pub fn new() -> Self { UnitDeserializer { marker: PhantomData, } } } impl<'de, E> de::Deserializer<'de> for UnitDeserializer where E: de::Error, { type Error = E; forward_to_deserialize_any! { bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map struct enum identifier ignored_any } fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_unit() } fn deserialize_option(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_none() } } impl<'de, E> IntoDeserializer<'de, E> for UnitDeserializer where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl Debug for UnitDeserializer { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.debug_struct("UnitDeserializer").finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer that cannot be instantiated. #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] pub struct NeverDeserializer { never: !, marker: PhantomData, } #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] impl<'de, E> IntoDeserializer<'de, E> for ! where E: de::Error, { type Deserializer = NeverDeserializer; fn into_deserializer(self) -> Self::Deserializer { self } } #[cfg(feature = "unstable")] impl<'de, E> de::Deserializer<'de> for NeverDeserializer where E: de::Error, { type Error = E; fn deserialize_any(self, _visitor: V) -> Result where V: de::Visitor<'de>, { self.never } 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 } } #[cfg(feature = "unstable")] impl<'de, E> IntoDeserializer<'de, E> for NeverDeserializer where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } //////////////////////////////////////////////////////////////////////////////// macro_rules! primitive_deserializer { ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => { #[doc = "A deserializer holding"] #[doc = $doc] pub struct $name { value: $ty, marker: PhantomData } impl_copy_clone!($name); impl<'de, E> IntoDeserializer<'de, E> for $ty where E: de::Error, { type Deserializer = $name; fn into_deserializer(self) -> $name { $name::new(self) } } impl $name { #[allow(missing_docs)] pub fn new(value: $ty) -> Self { $name { value, marker: PhantomData, } } } impl<'de, E> de::Deserializer<'de> for $name where E: de::Error, { type Error = E; 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 } fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.$method(self.value $($cast)*) } } impl<'de, E> IntoDeserializer<'de, E> for $name where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl Debug for $name { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct(stringify!($name)) .field("value", &self.value) .finish() } } } } primitive_deserializer!(bool, "a `bool`.", BoolDeserializer, visit_bool); primitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8); primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16); primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32); primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64); primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128); primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64); primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8); primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16); primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64); primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128); primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64); primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32); primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64); primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char); /// A deserializer holding a `u32`. pub struct U32Deserializer { value: u32, marker: PhantomData, } impl_copy_clone!(U32Deserializer); impl<'de, E> IntoDeserializer<'de, E> for u32 where E: de::Error, { type Deserializer = U32Deserializer; fn into_deserializer(self) -> U32Deserializer { U32Deserializer::new(self) } } impl U32Deserializer { #[allow(missing_docs)] pub fn new(value: u32) -> Self { U32Deserializer { value, marker: PhantomData, } } } impl<'de, E> de::Deserializer<'de> for U32Deserializer where E: de::Error, { type Error = E; 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 identifier ignored_any } fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_u32(self.value) } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(self) } } impl<'de, E> IntoDeserializer<'de, E> for U32Deserializer where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, E> de::EnumAccess<'de> for U32Deserializer where E: de::Error, { type Error = E; type Variant = private::UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { seed.deserialize(self).map(private::unit_only) } } impl Debug for U32Deserializer { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("U32Deserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `&str`. pub struct StrDeserializer<'a, E> { value: &'a str, marker: PhantomData, } impl_copy_clone!(StrDeserializer<'de>); impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str where E: de::Error, { type Deserializer = StrDeserializer<'a, E>; fn into_deserializer(self) -> StrDeserializer<'a, E> { StrDeserializer::new(self) } } impl<'a, E> StrDeserializer<'a, E> { #[allow(missing_docs)] pub fn new(value: &'a str) -> Self { StrDeserializer { value, marker: PhantomData, } } } impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_str(self.value) } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(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 identifier ignored_any } } impl<'de, 'a, E> IntoDeserializer<'de, E> for StrDeserializer<'a, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E> where E: de::Error, { type Error = E; type Variant = private::UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { seed.deserialize(self).map(private::unit_only) } } impl<'a, E> Debug for StrDeserializer<'a, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("StrDeserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `&str` with a lifetime tied to another /// deserializer. pub struct BorrowedStrDeserializer<'de, E> { value: &'de str, marker: PhantomData, } impl_copy_clone!(BorrowedStrDeserializer<'de>); 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, marker: PhantomData, } } } impl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_borrowed_str(self.value) } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(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 identifier ignored_any } } impl<'de, E> IntoDeserializer<'de, E> for BorrowedStrDeserializer<'de, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E> where E: de::Error, { type Error = E; type Variant = private::UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { seed.deserialize(self).map(private::unit_only) } } impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("BorrowedStrDeserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `String`. #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] pub struct StringDeserializer { value: String, marker: PhantomData, } #[cfg(any(feature = "std", feature = "alloc"))] impl Clone for StringDeserializer { fn clone(&self) -> Self { StringDeserializer { value: self.value.clone(), marker: PhantomData, } } } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, E> IntoDeserializer<'de, E> for String where E: de::Error, { type Deserializer = StringDeserializer; fn into_deserializer(self) -> StringDeserializer { StringDeserializer::new(self) } } #[cfg(any(feature = "std", feature = "alloc"))] impl StringDeserializer { #[allow(missing_docs)] pub fn new(value: String) -> Self { StringDeserializer { value, marker: PhantomData, } } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, E> de::Deserializer<'de> for StringDeserializer where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_string(self.value) } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(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 identifier ignored_any } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, E> IntoDeserializer<'de, E> for StringDeserializer where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, E> de::EnumAccess<'de> for StringDeserializer where E: de::Error, { type Error = E; type Variant = private::UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { seed.deserialize(self).map(private::unit_only) } } #[cfg(any(feature = "std", feature = "alloc"))] impl Debug for StringDeserializer { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("StringDeserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `Cow`. #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] pub struct CowStrDeserializer<'a, E> { value: Cow<'a, str>, marker: PhantomData, } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, E> Clone for CowStrDeserializer<'a, E> { fn clone(&self) -> Self { CowStrDeserializer { value: self.value.clone(), marker: PhantomData, } } } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str> where E: de::Error, { type Deserializer = CowStrDeserializer<'a, E>; fn into_deserializer(self) -> CowStrDeserializer<'a, E> { CowStrDeserializer::new(self) } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, E> CowStrDeserializer<'a, E> { #[allow(missing_docs)] pub fn new(value: Cow<'a, str>) -> Self { CowStrDeserializer { value, marker: PhantomData, } } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { match self.value { Cow::Borrowed(string) => visitor.visit_str(string), Cow::Owned(string) => visitor.visit_string(string), } } fn deserialize_enum( self, name: &str, variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { let _ = name; let _ = variants; visitor.visit_enum(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 identifier ignored_any } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, 'a, E> IntoDeserializer<'de, E> for CowStrDeserializer<'a, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E> where E: de::Error, { type Error = E; type Variant = private::UnitOnly; fn variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { seed.deserialize(self).map(private::unit_only) } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, E> Debug for CowStrDeserializer<'a, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("CowStrDeserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`]. pub struct BytesDeserializer<'a, E> { value: &'a [u8], marker: PhantomData, } impl<'a, E> BytesDeserializer<'a, E> { /// Create a new deserializer from the given bytes. pub fn new(value: &'a [u8]) -> Self { BytesDeserializer { value, marker: PhantomData, } } } impl_copy_clone!(BytesDeserializer<'a>); impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8] where E: de::Error, { type Deserializer = BytesDeserializer<'a, E>; fn into_deserializer(self) -> BytesDeserializer<'a, E> { BytesDeserializer::new(self) } } impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: 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 } } impl<'de, 'a, E> IntoDeserializer<'de, E> for BytesDeserializer<'a, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'a, E> Debug for BytesDeserializer<'a, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("BytesDeserializer") .field("value", &self.value) .finish() } } /// A deserializer holding a `&[u8]` with a lifetime tied to another /// deserializer. Always calls [`Visitor::visit_borrowed_bytes`]. pub struct BorrowedBytesDeserializer<'de, E> { value: &'de [u8], marker: PhantomData, } 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, marker: PhantomData, } } } impl_copy_clone!(BorrowedBytesDeserializer<'de>); impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_borrowed_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 } } impl<'de, E> IntoDeserializer<'de, E> for BorrowedBytesDeserializer<'de, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("BorrowedBytesDeserializer") .field("value", &self.value) .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer that iterates over a sequence. #[derive(Clone)] pub struct SeqDeserializer { iter: iter::Fuse, count: usize, marker: PhantomData, } impl SeqDeserializer where I: Iterator, { /// Construct a new `SeqDeserializer`. pub fn new(iter: I) -> Self { SeqDeserializer { iter: iter.fuse(), count: 0, marker: PhantomData, } } } impl SeqDeserializer where I: Iterator, E: de::Error, { /// Check for remaining elements after passing a `SeqDeserializer` to /// `Visitor::visit_seq`. pub fn end(self) -> Result<(), E> { let remaining = self.iter.count(); if remaining == 0 { Ok(()) } else { // First argument is the number of elements in the data, second // argument is the number of elements expected by the Deserialize. Err(de::Error::invalid_length( self.count + remaining, &ExpectedInSeq(self.count), )) } } } impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer where I: Iterator, T: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn deserialize_any(mut self, visitor: V) -> Result where V: de::Visitor<'de>, { let v = tri!(visitor.visit_seq(&mut self)); tri!(self.end()); Ok(v) } 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, I, T, E> IntoDeserializer<'de, E> for SeqDeserializer where I: Iterator, T: IntoDeserializer<'de, E>, E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer where I: Iterator, T: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn next_element_seed(&mut self, seed: V) -> Result, Self::Error> where V: de::DeserializeSeed<'de>, { match self.iter.next() { Some(value) => { self.count += 1; seed.deserialize(value.into_deserializer()).map(Some) } None => Ok(None), } } fn size_hint(&self) -> Option { size_hint::from_bounds(&self.iter) } } struct ExpectedInSeq(usize); impl Expected for ExpectedInSeq { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if self.0 == 1 { formatter.write_str("1 element in sequence") } else { write!(formatter, "{} elements in sequence", self.0) } } } impl Debug for SeqDeserializer where I: Debug, { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("SeqDeserializer") .field("iter", &self.iter) .field("count", &self.count) .finish() } } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, T, E> IntoDeserializer<'de, E> for Vec where T: IntoDeserializer<'de, E>, E: de::Error, { type Deserializer = SeqDeserializer<::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { SeqDeserializer::new(self.into_iter()) } } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet where T: IntoDeserializer<'de, E> + Eq + Ord, E: de::Error, { type Deserializer = SeqDeserializer<::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { SeqDeserializer::new(self.into_iter()) } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet where T: IntoDeserializer<'de, E> + Eq + Hash, S: BuildHasher, E: de::Error, { type Deserializer = SeqDeserializer<::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { SeqDeserializer::new(self.into_iter()) } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `SeqAccess`. #[derive(Clone, Debug)] pub struct SeqAccessDeserializer { seq: A, } impl SeqAccessDeserializer { /// Construct a new `SeqAccessDeserializer`. pub fn new(seq: A) -> Self { SeqAccessDeserializer { seq } } } impl<'de, A> de::Deserializer<'de> for SeqAccessDeserializer where A: de::SeqAccess<'de>, { type Error = A::Error; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_seq(self.seq) } 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, A> IntoDeserializer<'de, A::Error> for SeqAccessDeserializer where A: de::SeqAccess<'de>, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer that iterates over a map. pub struct MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, { iter: iter::Fuse, value: Option>, count: usize, lifetime: PhantomData<&'de ()>, error: PhantomData, } impl<'de, I, E> MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, { /// Construct a new `MapDeserializer`. pub fn new(iter: I) -> Self { MapDeserializer { iter: iter.fuse(), value: None, count: 0, lifetime: PhantomData, error: PhantomData, } } } impl<'de, I, E> MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, E: de::Error, { /// Check for remaining elements after passing a `MapDeserializer` to /// `Visitor::visit_map`. pub fn end(self) -> Result<(), E> { let remaining = self.iter.count(); if remaining == 0 { Ok(()) } else { // First argument is the number of elements in the data, second // argument is the number of elements expected by the Deserialize. Err(de::Error::invalid_length( self.count + remaining, &ExpectedInMap(self.count), )) } } } impl<'de, I, E> MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, { fn next_pair(&mut self) -> Option<(First, Second)> { match self.iter.next() { Some(kv) => { self.count += 1; Some(private::Pair::split(kv)) } None => None, } } } impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, First: IntoDeserializer<'de, E>, Second: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn deserialize_any(mut self, visitor: V) -> Result where V: de::Visitor<'de>, { let value = tri!(visitor.visit_map(&mut self)); tri!(self.end()); Ok(value) } fn deserialize_seq(mut self, visitor: V) -> Result where V: de::Visitor<'de>, { let value = tri!(visitor.visit_seq(&mut self)); tri!(self.end()); Ok(value) } fn deserialize_tuple(self, len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { let _ = len; self.deserialize_seq(visitor) } 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 tuple_struct map struct enum identifier ignored_any } } impl<'de, I, E> IntoDeserializer<'de, E> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, First: IntoDeserializer<'de, E>, Second: IntoDeserializer<'de, E>, E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, First: IntoDeserializer<'de, E>, Second: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn next_key_seed(&mut self, seed: T) -> Result, Self::Error> where T: de::DeserializeSeed<'de>, { match self.next_pair() { Some((key, value)) => { self.value = Some(value); seed.deserialize(key.into_deserializer()).map(Some) } None => Ok(None), } } fn next_value_seed(&mut self, seed: T) -> Result where T: de::DeserializeSeed<'de>, { let value = self.value.take(); // Panic because this indicates a bug in the program rather than an // expected failure. let value = value.expect("MapAccess::next_value called before next_key"); seed.deserialize(value.into_deserializer()) } fn next_entry_seed( &mut self, kseed: TK, vseed: TV, ) -> Result, Self::Error> where TK: de::DeserializeSeed<'de>, TV: de::DeserializeSeed<'de>, { match self.next_pair() { Some((key, value)) => { let key = tri!(kseed.deserialize(key.into_deserializer())); let value = tri!(vseed.deserialize(value.into_deserializer())); Ok(Some((key, value))) } None => Ok(None), } } fn size_hint(&self) -> Option { size_hint::from_bounds(&self.iter) } } impl<'de, I, E> de::SeqAccess<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, First: IntoDeserializer<'de, E>, Second: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: de::DeserializeSeed<'de>, { match self.next_pair() { Some((k, v)) => { let de = PairDeserializer(k, v, PhantomData); seed.deserialize(de).map(Some) } None => Ok(None), } } fn size_hint(&self) -> Option { size_hint::from_bounds(&self.iter) } } // Cannot #[derive(Clone)] because of the bound `Second: Clone`. impl<'de, I, E> Clone for MapDeserializer<'de, I, E> where I: Iterator + Clone, I::Item: private::Pair, Second: Clone, { fn clone(&self) -> Self { MapDeserializer { iter: self.iter.clone(), value: self.value.clone(), count: self.count, lifetime: self.lifetime, error: self.error, } } } impl<'de, I, E> Debug for MapDeserializer<'de, I, E> where I: Iterator + Debug, I::Item: private::Pair, Second: Debug, { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter .debug_struct("MapDeserializer") .field("iter", &self.iter) .field("value", &self.value) .field("count", &self.count) .finish() } } // Used in the `impl SeqAccess for MapDeserializer` to visit the map as a // sequence of pairs. struct PairDeserializer(A, B, PhantomData); impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer where A: IntoDeserializer<'de, E>, B: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; 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 tuple_struct map struct enum identifier ignored_any } fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { self.deserialize_seq(visitor) } fn deserialize_seq(self, visitor: V) -> Result where V: de::Visitor<'de>, { let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); let pair = tri!(visitor.visit_seq(&mut pair_visitor)); if pair_visitor.1.is_none() { Ok(pair) } else { let remaining = pair_visitor.size_hint().unwrap(); // First argument is the number of elements in the data, second // argument is the number of elements expected by the Deserialize. Err(de::Error::invalid_length(2, &ExpectedInSeq(2 - remaining))) } } fn deserialize_tuple(self, len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { if len == 2 { self.deserialize_seq(visitor) } else { // First argument is the number of elements in the data, second // argument is the number of elements expected by the Deserialize. Err(de::Error::invalid_length(2, &ExpectedInSeq(len))) } } } struct PairVisitor(Option, Option, PhantomData); impl<'de, A, B, E> de::SeqAccess<'de> for PairVisitor where A: IntoDeserializer<'de, E>, B: IntoDeserializer<'de, E>, E: de::Error, { type Error = E; fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where T: de::DeserializeSeed<'de>, { if let Some(k) = self.0.take() { seed.deserialize(k.into_deserializer()).map(Some) } else if let Some(v) = self.1.take() { seed.deserialize(v.into_deserializer()).map(Some) } else { Ok(None) } } fn size_hint(&self) -> Option { if self.0.is_some() { Some(2) } else if self.1.is_some() { Some(1) } else { Some(0) } } } struct ExpectedInMap(usize); impl Expected for ExpectedInMap { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { if self.0 == 1 { formatter.write_str("1 element in map") } else { write!(formatter, "{} elements in map", self.0) } } } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap where K: IntoDeserializer<'de, E> + Eq + Ord, V: IntoDeserializer<'de, E>, E: de::Error, { type Deserializer = MapDeserializer<'de, ::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { MapDeserializer::new(self.into_iter()) } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap where K: IntoDeserializer<'de, E> + Eq + Hash, V: IntoDeserializer<'de, E>, S: BuildHasher, E: de::Error, { type Deserializer = MapDeserializer<'de, ::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { MapDeserializer::new(self.into_iter()) } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `MapAccess`. #[derive(Clone, Debug)] pub struct MapAccessDeserializer { map: A, } impl MapAccessDeserializer { /// Construct a new `MapAccessDeserializer`. pub fn new(map: A) -> Self { MapAccessDeserializer { map } } } impl<'de, A> de::Deserializer<'de> for MapAccessDeserializer where A: de::MapAccess<'de>, { type Error = A::Error; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_map(self.map) } fn deserialize_enum( self, _name: &str, _variants: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { visitor.visit_enum(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 identifier ignored_any } } impl<'de, A> IntoDeserializer<'de, A::Error> for MapAccessDeserializer where A: de::MapAccess<'de>, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer where A: de::MapAccess<'de>, { type Error = A::Error; type Variant = private::MapAsEnum; fn variant_seed(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> where T: de::DeserializeSeed<'de>, { 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")), } } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding an `EnumAccess`. #[derive(Clone, Debug)] pub struct EnumAccessDeserializer { access: A, } impl EnumAccessDeserializer { /// Construct a new `EnumAccessDeserializer`. pub fn new(access: A) -> Self { EnumAccessDeserializer { access } } } impl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer where A: de::EnumAccess<'de>, { type Error = A::Error; fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { visitor.visit_enum(self.access) } 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, A> IntoDeserializer<'de, A::Error> for EnumAccessDeserializer where A: de::EnumAccess<'de>, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } //////////////////////////////////////////////////////////////////////////////// mod private { use crate::lib::*; use crate::de::{ self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor, }; pub struct UnitOnly { marker: PhantomData, } pub fn unit_only(t: T) -> (T, UnitOnly) { ( t, UnitOnly { marker: PhantomData, }, ) } impl<'de, E> de::VariantAccess<'de> for UnitOnly where E: de::Error, { type Error = E; fn unit_variant(self) -> Result<(), Self::Error> { Ok(()) } fn newtype_variant_seed(self, _seed: T) -> Result where T: de::DeserializeSeed<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"newtype variant", )) } fn tuple_variant(self, _len: usize, _visitor: V) -> Result where V: de::Visitor<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"tuple variant", )) } fn struct_variant( self, _fields: &'static [&'static str], _visitor: V, ) -> Result where V: de::Visitor<'de>, { Err(de::Error::invalid_type( Unexpected::UnitVariant, &"struct variant", )) } } pub struct MapAsEnum { map: A, } pub fn map_as_enum(map: A) -> MapAsEnum { MapAsEnum { map } } impl<'de, A> VariantAccess<'de> for MapAsEnum where A: MapAccess<'de>, { type Error = A::Error; fn unit_variant(mut self) -> Result<(), Self::Error> { self.map.next_value() } fn newtype_variant_seed(mut self, seed: T) -> Result where T: DeserializeSeed<'de>, { self.map.next_value_seed(seed) } fn tuple_variant(mut self, len: usize, visitor: V) -> Result where V: Visitor<'de>, { self.map.next_value_seed(SeedTupleVariant { len, visitor }) } fn struct_variant( mut self, _fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { self.map.next_value_seed(SeedStructVariant { visitor }) } } struct SeedTupleVariant { len: usize, visitor: V, } impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant where V: Visitor<'de>, { type Value = V::Value; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_tuple(self.len, self.visitor) } } struct SeedStructVariant { visitor: V, } impl<'de, V> DeserializeSeed<'de> for SeedStructVariant where V: Visitor<'de>, { type Value = V::Value; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_map(self.visitor) } } /// Avoid having to restate the generic types on `MapDeserializer`. The /// `Iterator::Item` contains enough information to figure out K and V. pub trait Pair { type First; type Second; fn split(self) -> (Self::First, Self::Second); } impl Pair for (A, B) { type First = A; type Second = B; fn split(self) -> (A, B) { self } } pub type First = ::First; pub type Second = ::Second; } serde-1.0.217/src/format.rs000064400000000000000000000013261046102023000135250ustar 00000000000000use crate::lib::fmt::{self, Write}; use crate::lib::str; pub(super) struct Buf<'a> { bytes: &'a mut [u8], offset: usize, } impl<'a> Buf<'a> { pub fn new(bytes: &'a mut [u8]) -> Self { Buf { bytes, offset: 0 } } pub fn as_str(&self) -> &str { let slice = &self.bytes[..self.offset]; unsafe { str::from_utf8_unchecked(slice) } } } impl<'a> Write for Buf<'a> { fn write_str(&mut self, s: &str) -> fmt::Result { if self.offset + s.len() > self.bytes.len() { Err(fmt::Error) } else { self.bytes[self.offset..self.offset + s.len()].copy_from_slice(s.as_bytes()); self.offset += s.len(); Ok(()) } } } serde-1.0.217/src/integer128.rs000064400000000000000000000003631046102023000141250ustar 00000000000000// No longer used. Old versions of serde used this macro for supporting targets // that did not yet have 128-bit integer support. #[macro_export] #[doc(hidden)] macro_rules! serde_if_integer128 { ($($tt:tt)*) => { $($tt)* }; } serde-1.0.217/src/lib.rs000064400000000000000000000331251046102023000130050ustar 00000000000000//! # Serde //! //! Serde is a framework for ***ser***ializing and ***de***serializing Rust data //! structures efficiently and generically. //! //! The Serde ecosystem consists of data structures that know how to serialize //! and deserialize themselves along with data formats that know how to //! serialize and deserialize other things. Serde provides the layer by which //! these two groups interact with each other, allowing any supported data //! structure to be serialized and deserialized using any supported data format. //! //! See the Serde website for additional documentation and //! usage examples. //! //! ## Design //! //! Where many other languages rely on runtime reflection for serializing data, //! Serde is instead built on Rust's powerful trait system. A data structure //! that knows how to serialize and deserialize itself is one that implements //! Serde's `Serialize` and `Deserialize` traits (or uses Serde's derive //! attribute to automatically generate implementations at compile time). This //! avoids any overhead of reflection or runtime type information. In fact in //! many situations the interaction between data structure and data format can //! be completely optimized away by the Rust compiler, leaving Serde //! serialization to perform the same speed as a handwritten serializer for the //! specific selection of data structure and data format. //! //! ## Data formats //! //! The following is a partial list of data formats that have been implemented //! for Serde by the community. //! //! - [JSON], the ubiquitous JavaScript Object Notation used by many HTTP APIs. //! - [Postcard], a no\_std and embedded-systems friendly compact binary format. //! - [CBOR], a Concise Binary Object Representation designed for small message //! size without the need for version negotiation. //! - [YAML], a self-proclaimed human-friendly configuration language that ain't //! markup language. //! - [MessagePack], an efficient binary format that resembles a compact JSON. //! - [TOML], a minimal configuration format used by [Cargo]. //! - [Pickle], a format common in the Python world. //! - [RON], a Rusty Object Notation. //! - [BSON], the data storage and network transfer format used by MongoDB. //! - [Avro], a binary format used within Apache Hadoop, with support for schema //! definition. //! - [JSON5], a superset of JSON including some productions from ES5. //! - [URL] query strings, in the x-www-form-urlencoded format. //! - [Starlark], the format used for describing build targets by the Bazel and //! Buck build systems. *(serialization only)* //! - [Envy], a way to deserialize environment variables into Rust structs. //! *(deserialization only)* //! - [Envy Store], a way to deserialize [AWS Parameter Store] parameters into //! Rust structs. *(deserialization only)* //! - [S-expressions], the textual representation of code and data used by the //! Lisp language family. //! - [D-Bus]'s binary wire format. //! - [FlexBuffers], the schemaless cousin of Google's FlatBuffers zero-copy //! serialization format. //! - [Bencode], a simple binary format used in the BitTorrent protocol. //! - [Token streams], for processing Rust procedural macro input. //! *(deserialization only)* //! - [DynamoDB Items], the format used by [rusoto_dynamodb] to transfer data to //! and from DynamoDB. //! - [Hjson], a syntax extension to JSON designed around human reading and //! editing. *(deserialization only)* //! - [CSV], Comma-separated values is a tabular text file format. //! //! [JSON]: https://github.com/serde-rs/json //! [Postcard]: https://github.com/jamesmunns/postcard //! [CBOR]: https://github.com/enarx/ciborium //! [YAML]: https://github.com/dtolnay/serde-yaml //! [MessagePack]: https://github.com/3Hren/msgpack-rust //! [TOML]: https://docs.rs/toml //! [Pickle]: https://github.com/birkenfeld/serde-pickle //! [RON]: https://github.com/ron-rs/ron //! [BSON]: https://github.com/mongodb/bson-rust //! [Avro]: https://docs.rs/apache-avro //! [JSON5]: https://github.com/callum-oakley/json5-rs //! [URL]: https://docs.rs/serde_qs //! [Starlark]: https://github.com/dtolnay/serde-starlark //! [Envy]: https://github.com/softprops/envy //! [Envy Store]: https://github.com/softprops/envy-store //! [Cargo]: https://doc.rust-lang.org/cargo/reference/manifest.html //! [AWS Parameter Store]: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html //! [S-expressions]: https://github.com/rotty/lexpr-rs //! [D-Bus]: https://docs.rs/zvariant //! [FlexBuffers]: https://github.com/google/flatbuffers/tree/master/rust/flexbuffers //! [Bencode]: https://github.com/P3KI/bendy //! [Token streams]: https://github.com/oxidecomputer/serde_tokenstream //! [DynamoDB Items]: https://docs.rs/serde_dynamo //! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb //! [Hjson]: https://github.com/Canop/deser-hjson //! [CSV]: https://docs.rs/csv //////////////////////////////////////////////////////////////////////////////// // Serde types in rustdoc of other crates get linked to here. #![doc(html_root_url = "https://docs.rs/serde/1.0.217")] // Support using Serde without the standard library! #![cfg_attr(not(feature = "std"), no_std)] // Show which crate feature enables conditionally compiled APIs in documentation. #![cfg_attr(docsrs, feature(doc_cfg, rustdoc_internals))] #![cfg_attr(docsrs, allow(internal_features))] // Unstable functionality only if the user asks for it. For tracking and // discussion of these features please refer to this issue: // // https://github.com/serde-rs/serde/issues/812 #![cfg_attr(feature = "unstable", feature(never_type))] #![allow(unknown_lints, bare_trait_objects, deprecated)] // Ignored clippy and clippy_pedantic lints #![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_precision_loss, clippy::cast_sign_loss, // things are often more readable this way clippy::cast_lossless, clippy::module_name_repetitions, 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::incompatible_msrv, 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::needless_lifetimes, 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)] //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "alloc")] extern crate alloc; /// A facade around all the types we need from the `std`, `core`, and `alloc` /// crates. This avoids elaborate import wrangling having to happen in every /// module. mod lib { mod core { #[cfg(not(feature = "std"))] pub use core::*; #[cfg(feature = "std")] pub use std::*; } pub use self::core::{f32, f64}; pub use self::core::{i16, i32, i64, i8, isize}; pub use self::core::{iter, num, ptr, str}; pub use self::core::{u16, u32, u64, u8, usize}; #[cfg(any(feature = "std", feature = "alloc"))] pub use self::core::{cmp, mem, slice}; pub use self::core::cell::{Cell, RefCell}; pub use self::core::clone; pub use self::core::cmp::Reverse; pub use self::core::convert; pub use self::core::default; pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite}; pub use self::core::marker::{self, PhantomData}; pub use self::core::num::Wrapping; pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo}; pub use self::core::option; pub use self::core::result; pub use self::core::time::Duration; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::borrow::{Cow, ToOwned}; #[cfg(feature = "std")] pub use std::borrow::{Cow, ToOwned}; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::string::{String, ToString}; #[cfg(feature = "std")] pub use std::string::{String, ToString}; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::vec::Vec; #[cfg(feature = "std")] pub use std::vec::Vec; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::boxed::Box; #[cfg(feature = "std")] pub use std::boxed::Box; #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))] pub use alloc::rc::{Rc, Weak as RcWeak}; #[cfg(all(feature = "rc", feature = "std"))] pub use std::rc::{Rc, Weak as RcWeak}; #[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))] pub use alloc::sync::{Arc, Weak as ArcWeak}; #[cfg(all(feature = "rc", feature = "std"))] pub use std::sync::{Arc, Weak as ArcWeak}; #[cfg(all(feature = "alloc", not(feature = "std")))] pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; #[cfg(feature = "std")] pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; #[cfg(all(not(no_core_cstr), not(feature = "std")))] pub use self::core::ffi::CStr; #[cfg(feature = "std")] pub use std::ffi::CStr; #[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))] pub use alloc::ffi::CString; #[cfg(feature = "std")] pub use std::ffi::CString; #[cfg(all(not(no_core_net), not(feature = "std")))] pub use self::core::net; #[cfg(feature = "std")] pub use std::net; #[cfg(feature = "std")] pub use std::error; #[cfg(feature = "std")] pub use std::collections::{HashMap, HashSet}; #[cfg(feature = "std")] pub use std::ffi::{OsStr, OsString}; #[cfg(feature = "std")] pub use std::hash::{BuildHasher, Hash}; #[cfg(feature = "std")] pub use std::io::Write; #[cfg(feature = "std")] pub use std::path::{Path, PathBuf}; #[cfg(feature = "std")] pub use std::sync::{Mutex, RwLock}; #[cfg(feature = "std")] pub use std::time::{SystemTime, UNIX_EPOCH}; #[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, AtomicUsize, Ordering, }; #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))] pub use std::sync::atomic::{AtomicI64, AtomicU64}; #[cfg(all(feature = "std", not(no_target_has_atomic)))] pub use std::sync::atomic::Ordering; #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))] pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8}; #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))] pub use std::sync::atomic::{AtomicI16, AtomicU16}; #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))] pub use std::sync::atomic::{AtomicI32, AtomicU32}; #[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))] 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(not(no_core_num_saturating))] pub use self::core::num::Saturating; } // 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! tri { ($expr:expr) => { match $expr { Ok(val) => val, Err(err) => return Err(err), } }; } //////////////////////////////////////////////////////////////////////////////// #[macro_use] mod macros; #[macro_use] mod integer128; pub mod de; pub mod ser; mod format; #[doc(inline)] pub use crate::de::{Deserialize, Deserializer}; #[doc(inline)] 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; #[path = "de/seed.rs"] mod seed; #[cfg(all(not(feature = "std"), no_core_error))] mod std_error; // Re-export #[derive(Serialize, Deserialize)]. // // The reason re-exporting is not enabled by default is that disabling it would // 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")] extern crate serde_derive; /// Derive macro available if serde is built with `features = ["derive"]`. #[cfg(feature = "serde_derive")] #[cfg_attr(docsrs, doc(cfg(feature = "derive")))] pub use serde_derive::{Deserialize, Serialize}; #[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))] mod actually_private { pub struct T; } serde-1.0.217/src/macros.rs000064400000000000000000000201521046102023000135170ustar 00000000000000// Super explicit first paragraph because this shows up at the top level and // trips up people who are just looking for basic Serialize / Deserialize // documentation. // /// Helper macro when implementing the `Deserializer` part of a new data format /// for Serde. /// /// Some [`Deserializer`] implementations for self-describing formats do not /// care what hint the [`Visitor`] gives them, they just want to blindly call /// the [`Visitor`] method corresponding to the data they can tell is in the /// input. This requires repetitive implementations of all the [`Deserializer`] /// trait methods. /// /// ```edition2021 /// # use serde::forward_to_deserialize_any; /// # use serde::de::{value, Deserializer, Visitor}; /// # /// # struct MyDeserializer; /// # /// # impl<'de> Deserializer<'de> for MyDeserializer { /// # type Error = value::Error; /// # /// # fn deserialize_any(self, _: V) -> Result /// # where /// # V: Visitor<'de>, /// # { /// # unimplemented!() /// # } /// # /// #[inline] /// fn deserialize_bool(self, visitor: V) -> Result /// where /// V: Visitor<'de>, /// { /// self.deserialize_any(visitor) /// } /// # /// # forward_to_deserialize_any! { /// # 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 /// # } /// # } /// ``` /// /// The `forward_to_deserialize_any!` macro implements these simple forwarding /// methods so that they forward directly to [`Deserializer::deserialize_any`]. /// You can choose which methods to forward. /// /// ```edition2021 /// # use serde::forward_to_deserialize_any; /// # use serde::de::{value, Deserializer, Visitor}; /// # /// # struct MyDeserializer; /// # /// impl<'de> Deserializer<'de> for MyDeserializer { /// # type Error = value::Error; /// # /// fn deserialize_any(self, visitor: V) -> Result /// where /// V: Visitor<'de>, /// { /// /* ... */ /// # let _ = visitor; /// # unimplemented!() /// } /// /// 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 /// } /// } /// ``` /// /// The macro assumes the convention that your `Deserializer` lifetime parameter /// is called `'de` and that the `Visitor` type parameters on each method are /// called `V`. A different type parameter and a different lifetime can be /// specified explicitly if necessary. /// /// ```edition2021 /// # use serde::forward_to_deserialize_any; /// # use serde::de::{value, Deserializer, Visitor}; /// # use std::marker::PhantomData; /// # /// # struct MyDeserializer(PhantomData); /// # /// # impl<'q, V> Deserializer<'q> for MyDeserializer { /// # type Error = value::Error; /// # /// # fn deserialize_any(self, visitor: W) -> Result /// # where /// # W: Visitor<'q>, /// # { /// # unimplemented!() /// # } /// # /// 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 /// } /// # } /// ``` /// /// [`Deserializer`]: trait.Deserializer.html /// [`Visitor`]: de/trait.Visitor.html /// [`Deserializer::deserialize_any`]: trait.Deserializer.html#tymethod.deserialize_any #[macro_export(local_inner_macros)] macro_rules! forward_to_deserialize_any { (<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => { $(forward_to_deserialize_any_helper!{$func<$lifetime, $visitor>})* }; // This case must be after the previous one. ($($func:ident)*) => { $(forward_to_deserialize_any_helper!{$func<'de, V>})* }; } #[doc(hidden)] #[macro_export] macro_rules! forward_to_deserialize_any_method { ($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => { #[inline] fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, >::Error> where $v: $crate::de::Visitor<$l>, { $( let _ = $arg; )* self.deserialize_any(visitor) } }; } #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! forward_to_deserialize_any_helper { (bool<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_bool<$l, $v>()} }; (i8<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_i8<$l, $v>()} }; (i16<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_i16<$l, $v>()} }; (i32<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_i32<$l, $v>()} }; (i64<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()} }; (i128<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()} }; (u8<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()} }; (u16<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_u16<$l, $v>()} }; (u32<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_u32<$l, $v>()} }; (u64<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()} }; (u128<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()} }; (f32<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()} }; (f64<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_f64<$l, $v>()} }; (char<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_char<$l, $v>()} }; (str<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_str<$l, $v>()} }; (string<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_string<$l, $v>()} }; (bytes<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_bytes<$l, $v>()} }; (byte_buf<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_byte_buf<$l, $v>()} }; (option<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_option<$l, $v>()} }; (unit<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_unit<$l, $v>()} }; (unit_struct<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_unit_struct<$l, $v>(name: &'static str)} }; (newtype_struct<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_newtype_struct<$l, $v>(name: &'static str)} }; (seq<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_seq<$l, $v>()} }; (tuple<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)} }; (tuple_struct<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_tuple_struct<$l, $v>(name: &'static str, len: usize)} }; (map<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_map<$l, $v>()} }; (struct<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_struct<$l, $v>(name: &'static str, fields: &'static [&'static str])} }; (enum<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_enum<$l, $v>(name: &'static str, variants: &'static [&'static str])} }; (identifier<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_identifier<$l, $v>()} }; (ignored_any<$l:tt, $v:ident>) => { forward_to_deserialize_any_method!{deserialize_ignored_any<$l, $v>()} }; } serde-1.0.217/src/private/de.rs000064400000000000000000002476031046102023000143110ustar 00000000000000use crate::lib::*; use crate::de::value::{BorrowedBytesDeserializer, BytesDeserializer}; use crate::de::{ Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, IntoDeserializer, VariantAccess, Visitor, }; #[cfg(any(feature = "std", feature = "alloc"))] use crate::de::{MapAccess, Unexpected}; #[cfg(any(feature = "std", feature = "alloc"))] pub use self::content::{ Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer, InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor, TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor, }; pub use crate::seed::InPlaceSeed; /// If the missing field is of type `Option` then treat is as `None`, /// otherwise it is an error. pub fn missing_field<'de, V, E>(field: &'static str) -> Result where V: Deserialize<'de>, E: Error, { struct MissingFieldDeserializer(&'static str, PhantomData); impl<'de, E> Deserializer<'de> for MissingFieldDeserializer where E: Error, { type Error = E; fn deserialize_any(self, _visitor: V) -> Result where V: Visitor<'de>, { Err(Error::missing_field(self.0)) } fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_none() } forward_to_deserialize_any! { bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map struct enum identifier ignored_any } } let deserializer = MissingFieldDeserializer(field, PhantomData); Deserialize::deserialize(deserializer) } #[cfg(any(feature = "std", feature = "alloc"))] pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result where D: Deserializer<'de>, R: From>, { struct CowStrVisitor; impl<'a> Visitor<'a> for CowStrVisitor { type Value = Cow<'a, str>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(Cow::Owned(v.to_owned())) } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(Cow::Borrowed(v)) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(Cow::Owned(v)) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { match str::from_utf8(v) { Ok(s) => Ok(Cow::Owned(s.to_owned())), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { match str::from_utf8(v) { Ok(s) => Ok(Cow::Borrowed(s)), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { match String::from_utf8(v) { Ok(s) => Ok(Cow::Owned(s)), Err(e) => Err(Error::invalid_value( Unexpected::Bytes(&e.into_bytes()), &self, )), } } } deserializer.deserialize_str(CowStrVisitor).map(From::from) } #[cfg(any(feature = "std", feature = "alloc"))] pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result where D: Deserializer<'de>, R: From>, { struct CowBytesVisitor; impl<'a> Visitor<'a> for CowBytesVisitor { type Value = Cow<'a, [u8]>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a byte array") } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(Cow::Owned(v.as_bytes().to_vec())) } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(Cow::Borrowed(v.as_bytes())) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(Cow::Owned(v.into_bytes())) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { Ok(Cow::Owned(v.to_vec())) } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { Ok(Cow::Borrowed(v)) } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { Ok(Cow::Owned(v)) } } deserializer .deserialize_bytes(CowBytesVisitor) .map(From::from) } #[cfg(any(feature = "std", feature = "alloc"))] mod content { // This module is private and nothing here should be used outside of // generated code. // // We will iterate on the implementation for a few releases and only have to // worry about backward compatibility for the `untagged` and `tag` attributes // rather than for this entire mechanism. // // This issue is tracking making some of this stuff public: // https://github.com/serde-rs/serde/issues/741 use crate::lib::*; 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 /// deserializing untagged enums and internally tagged enums. /// /// Not public API. Use serde-value instead. #[derive(Debug, Clone)] pub enum Content<'de> { Bool(bool), U8(u8), U16(u16), U32(u32), U64(u64), I8(i8), I16(i16), I32(i32), I64(i64), F32(f32), F64(f64), Char(char), String(String), Str(&'de str), ByteBuf(Vec), Bytes(&'de [u8]), None, Some(Box>), Unit, Newtype(Box>), Seq(Vec>), Map(Vec<(Content<'de>, Content<'de>)>), } impl<'de> Content<'de> { pub fn as_str(&self) -> Option<&str> { match *self { Content::Str(x) => Some(x), Content::String(ref x) => Some(x), Content::Bytes(x) => str::from_utf8(x).ok(), Content::ByteBuf(ref x) => str::from_utf8(x).ok(), _ => None, } } #[cold] fn unexpected(&self) -> Unexpected { match *self { Content::Bool(b) => Unexpected::Bool(b), Content::U8(n) => Unexpected::Unsigned(n as u64), Content::U16(n) => Unexpected::Unsigned(n as u64), Content::U32(n) => Unexpected::Unsigned(n as u64), Content::U64(n) => Unexpected::Unsigned(n), Content::I8(n) => Unexpected::Signed(n as i64), Content::I16(n) => Unexpected::Signed(n as i64), Content::I32(n) => Unexpected::Signed(n as i64), Content::I64(n) => Unexpected::Signed(n), Content::F32(f) => Unexpected::Float(f as f64), Content::F64(f) => Unexpected::Float(f), Content::Char(c) => Unexpected::Char(c), Content::String(ref s) => Unexpected::Str(s), Content::Str(s) => Unexpected::Str(s), Content::ByteBuf(ref b) => Unexpected::Bytes(b), Content::Bytes(b) => Unexpected::Bytes(b), Content::None | Content::Some(_) => Unexpected::Option, Content::Unit => Unexpected::Unit, Content::Newtype(_) => Unexpected::NewtypeStruct, Content::Seq(_) => Unexpected::Seq, Content::Map(_) => Unexpected::Map, } } } impl<'de> Deserialize<'de> for Content<'de> { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { // Untagged and internally tagged enums are only supported in // self-describing formats. let visitor = ContentVisitor { value: PhantomData }; deserializer.__deserialize_content(actually_private::T, visitor) } } 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) } } impl<'a, 'de, E> de::IntoDeserializer<'de, E> for &'a Content<'de> where E: de::Error, { type Deserializer = ContentRefDeserializer<'a, 'de, E>; fn into_deserializer(self) -> Self::Deserializer { ContentRefDeserializer::new(self) } } /// Used to capture data in [`Content`] from other deserializers. /// Cannot capture externally tagged enums, `i128` and `u128`. struct ContentVisitor<'de> { value: PhantomData>, } impl<'de> ContentVisitor<'de> { fn new() -> Self { ContentVisitor { value: PhantomData } } } impl<'de> Visitor<'de> for ContentVisitor<'de> { type Value = Content<'de>; fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str("any value") } fn visit_bool(self, value: bool) -> Result where F: de::Error, { Ok(Content::Bool(value)) } fn visit_i8(self, value: i8) -> Result where F: de::Error, { Ok(Content::I8(value)) } fn visit_i16(self, value: i16) -> Result where F: de::Error, { Ok(Content::I16(value)) } fn visit_i32(self, value: i32) -> Result where F: de::Error, { Ok(Content::I32(value)) } fn visit_i64(self, value: i64) -> Result where F: de::Error, { Ok(Content::I64(value)) } fn visit_u8(self, value: u8) -> Result where F: de::Error, { Ok(Content::U8(value)) } fn visit_u16(self, value: u16) -> Result where F: de::Error, { Ok(Content::U16(value)) } fn visit_u32(self, value: u32) -> Result where F: de::Error, { Ok(Content::U32(value)) } fn visit_u64(self, value: u64) -> Result where F: de::Error, { Ok(Content::U64(value)) } fn visit_f32(self, value: f32) -> Result where F: de::Error, { Ok(Content::F32(value)) } fn visit_f64(self, value: f64) -> Result where F: de::Error, { Ok(Content::F64(value)) } fn visit_char(self, value: char) -> Result where F: de::Error, { Ok(Content::Char(value)) } fn visit_str(self, value: &str) -> Result where F: de::Error, { Ok(Content::String(value.into())) } fn visit_borrowed_str(self, value: &'de str) -> Result where F: de::Error, { Ok(Content::Str(value)) } fn visit_string(self, value: String) -> Result where F: de::Error, { Ok(Content::String(value)) } fn visit_bytes(self, value: &[u8]) -> Result where F: de::Error, { Ok(Content::ByteBuf(value.into())) } fn visit_borrowed_bytes(self, value: &'de [u8]) -> Result where F: de::Error, { Ok(Content::Bytes(value)) } fn visit_byte_buf(self, value: Vec) -> Result where F: de::Error, { Ok(Content::ByteBuf(value)) } fn visit_unit(self) -> Result where F: de::Error, { Ok(Content::Unit) } fn visit_none(self) -> Result where F: de::Error, { Ok(Content::None) } fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de>, { let v = tri!(Deserialize::deserialize(deserializer)); Ok(Content::Some(Box::new(v))) } fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de>, { let v = tri!(Deserialize::deserialize(deserializer)); Ok(Content::Newtype(Box::new(v))) } fn visit_seq(self, mut visitor: V) -> Result where V: SeqAccess<'de>, { let mut vec = Vec::::with_capacity(size_hint::cautious::(visitor.size_hint())); while let Some(e) = tri!(visitor.next_element()) { vec.push(e); } Ok(Content::Seq(vec)) } fn visit_map(self, mut visitor: V) -> Result where V: MapAccess<'de>, { 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)) } fn visit_enum(self, _visitor: V) -> Result where V: EnumAccess<'de>, { Err(de::Error::custom( "untagged and internally tagged enums do not support enum input", )) } } /// This is the type of the map keys in an internally tagged enum. /// /// Not public API. pub enum TagOrContent<'de> { Tag, Content(Content<'de>), } /// Serves as a seed for deserializing a key of internally tagged enum. /// Cannot capture externally tagged enums, `i128` and `u128`. struct TagOrContentVisitor<'de> { name: &'static str, value: PhantomData>, } impl<'de> TagOrContentVisitor<'de> { fn new(name: &'static str) -> Self { TagOrContentVisitor { name, value: PhantomData, } } } impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> { type Value = TagOrContent<'de>; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { // Internally tagged enums are only supported in self-describing // formats. deserializer.deserialize_any(self) } } impl<'de> Visitor<'de> for TagOrContentVisitor<'de> { type Value = TagOrContent<'de>; fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "a type tag `{}` or any other value", self.name) } fn visit_bool(self, value: bool) -> Result where F: de::Error, { ContentVisitor::new() .visit_bool(value) .map(TagOrContent::Content) } fn visit_i8(self, value: i8) -> Result where F: de::Error, { ContentVisitor::new() .visit_i8(value) .map(TagOrContent::Content) } fn visit_i16(self, value: i16) -> Result where F: de::Error, { ContentVisitor::new() .visit_i16(value) .map(TagOrContent::Content) } fn visit_i32(self, value: i32) -> Result where F: de::Error, { ContentVisitor::new() .visit_i32(value) .map(TagOrContent::Content) } fn visit_i64(self, value: i64) -> Result where F: de::Error, { ContentVisitor::new() .visit_i64(value) .map(TagOrContent::Content) } fn visit_u8(self, value: u8) -> Result where F: de::Error, { ContentVisitor::new() .visit_u8(value) .map(TagOrContent::Content) } fn visit_u16(self, value: u16) -> Result where F: de::Error, { ContentVisitor::new() .visit_u16(value) .map(TagOrContent::Content) } fn visit_u32(self, value: u32) -> Result where F: de::Error, { ContentVisitor::new() .visit_u32(value) .map(TagOrContent::Content) } fn visit_u64(self, value: u64) -> Result where F: de::Error, { ContentVisitor::new() .visit_u64(value) .map(TagOrContent::Content) } fn visit_f32(self, value: f32) -> Result where F: de::Error, { ContentVisitor::new() .visit_f32(value) .map(TagOrContent::Content) } fn visit_f64(self, value: f64) -> Result where F: de::Error, { ContentVisitor::new() .visit_f64(value) .map(TagOrContent::Content) } fn visit_char(self, value: char) -> Result where F: de::Error, { ContentVisitor::new() .visit_char(value) .map(TagOrContent::Content) } fn visit_str(self, value: &str) -> Result where F: de::Error, { if value == self.name { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_str(value) .map(TagOrContent::Content) } } fn visit_borrowed_str(self, value: &'de str) -> Result where F: de::Error, { if value == self.name { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_borrowed_str(value) .map(TagOrContent::Content) } } fn visit_string(self, value: String) -> Result where F: de::Error, { if value == self.name { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_string(value) .map(TagOrContent::Content) } } fn visit_bytes(self, value: &[u8]) -> Result where F: de::Error, { if value == self.name.as_bytes() { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_bytes(value) .map(TagOrContent::Content) } } fn visit_borrowed_bytes(self, value: &'de [u8]) -> Result where F: de::Error, { if value == self.name.as_bytes() { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_borrowed_bytes(value) .map(TagOrContent::Content) } } fn visit_byte_buf(self, value: Vec) -> Result where F: de::Error, { if value == self.name.as_bytes() { Ok(TagOrContent::Tag) } else { ContentVisitor::new() .visit_byte_buf(value) .map(TagOrContent::Content) } } fn visit_unit(self) -> Result where F: de::Error, { ContentVisitor::new() .visit_unit() .map(TagOrContent::Content) } fn visit_none(self) -> Result where F: de::Error, { ContentVisitor::new() .visit_none() .map(TagOrContent::Content) } fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de>, { ContentVisitor::new() .visit_some(deserializer) .map(TagOrContent::Content) } fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de>, { ContentVisitor::new() .visit_newtype_struct(deserializer) .map(TagOrContent::Content) } fn visit_seq(self, visitor: V) -> Result where V: SeqAccess<'de>, { ContentVisitor::new() .visit_seq(visitor) .map(TagOrContent::Content) } fn visit_map(self, visitor: V) -> Result where V: MapAccess<'de>, { ContentVisitor::new() .visit_map(visitor) .map(TagOrContent::Content) } fn visit_enum(self, visitor: V) -> Result where V: EnumAccess<'de>, { ContentVisitor::new() .visit_enum(visitor) .map(TagOrContent::Content) } } /// Used by generated code to deserialize an internally tagged enum. /// /// Captures map or sequence from the original deserializer and searches /// a tag in it (in case of sequence, tag is the first element of sequence). /// /// Not public API. pub struct TaggedContentVisitor { tag_name: &'static str, expecting: &'static str, value: PhantomData, } impl TaggedContentVisitor { /// 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, value: PhantomData, } } } impl<'de, T> Visitor<'de> for TaggedContentVisitor where T: Deserialize<'de>, { type Value = (T, Content<'de>); fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(self.expecting) } fn visit_seq(self, mut seq: S) -> Result where S: SeqAccess<'de>, { 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((tag, tri!(Content::deserialize(rest)))) } fn visit_map(self, mut map: M) -> Result where M: MapAccess<'de>, { let mut tag = None; 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(tri!(map.next_value())); } TagOrContent::Content(k) => { let v = tri!(map.next_value()); vec.push((k, v)); } } } match tag { None => Err(de::Error::missing_field(self.tag_name)), Some(tag) => Ok((tag, Content::Map(vec))), } } } /// Used by generated code to deserialize an adjacently tagged enum. /// /// Not public API. pub enum TagOrContentField { Tag, Content, } /// Not public API. pub struct TagOrContentFieldVisitor { /// Name of the tag field of the adjacently tagged enum pub tag: &'static str, /// Name of the content field of the adjacently tagged enum pub content: &'static str, } impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor { type Value = TagOrContentField; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_identifier(self) } } impl<'de> Visitor<'de> for TagOrContentFieldVisitor { type Value = TagOrContentField; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "{:?} or {:?}", self.tag, self.content) } fn visit_u64(self, field_index: u64) -> Result 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(self, field: &str) -> Result where E: de::Error, { if field == self.tag { Ok(TagOrContentField::Tag) } else if field == self.content { Ok(TagOrContentField::Content) } else { Err(de::Error::invalid_value(Unexpected::Str(field), &self)) } } fn visit_bytes(self, field: &[u8]) -> Result 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 /// ignoring unrelated fields is allowed. /// /// Not public API. pub enum TagContentOtherField { Tag, Content, Other, } /// Not public API. pub struct TagContentOtherFieldVisitor { /// Name of the tag field of the adjacently tagged enum pub tag: &'static str, /// Name of the content field of the adjacently tagged enum pub content: &'static str, } impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor { type Value = TagContentOtherField; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_identifier(self) } } impl<'de> Visitor<'de> for TagContentOtherFieldVisitor { type Value = TagContentOtherField; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( formatter, "{:?}, {:?}, or other ignored fields", self.tag, self.content ) } fn visit_u64(self, field_index: u64) -> Result where E: de::Error, { match field_index { 0 => Ok(TagContentOtherField::Tag), 1 => Ok(TagContentOtherField::Content), _ => Ok(TagContentOtherField::Other), } } fn visit_str(self, field: &str) -> Result where E: de::Error, { self.visit_bytes(field.as_bytes()) } fn visit_bytes(self, field: &[u8]) -> Result where E: de::Error, { if field == self.tag.as_bytes() { Ok(TagContentOtherField::Tag) } else if field == self.content.as_bytes() { Ok(TagContentOtherField::Content) } else { Ok(TagContentOtherField::Other) } } } /// Not public API pub struct ContentDeserializer<'de, E> { content: Content<'de>, err: PhantomData, } impl<'de, E> ContentDeserializer<'de, E> where E: de::Error, { #[cold] fn invalid_type(self, exp: &Expected) -> E { de::Error::invalid_type(self.content.unexpected(), exp) } fn deserialize_integer(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_float(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::F32(v) => visitor.visit_f32(v), Content::F64(v) => visitor.visit_f64(v), Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), _ => Err(self.invalid_type(&visitor)), } } } fn visit_content_seq<'de, V, E>(content: Vec>, visitor: V) -> Result where V: Visitor<'de>, E: de::Error, { let mut seq_visitor = SeqDeserializer::new(content.into_iter()); let value = tri!(visitor.visit_seq(&mut seq_visitor)); tri!(seq_visitor.end()); Ok(value) } fn visit_content_map<'de, V, E>( content: Vec<(Content<'de>, Content<'de>)>, visitor: V, ) -> Result where V: Visitor<'de>, E: de::Error, { let mut map_visitor = MapDeserializer::new(content.into_iter()); let value = tri!(visitor.visit_map(&mut map_visitor)); tri!(map_visitor.end()); Ok(value) } /// Used when deserializing an internally tagged enum because the content /// will be used exactly once. impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Bool(v) => visitor.visit_bool(v), Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), Content::F32(v) => visitor.visit_f32(v), Content::F64(v) => visitor.visit_f64(v), Content::Char(v) => visitor.visit_char(v), Content::String(v) => visitor.visit_string(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(v) => visitor.visit_byte_buf(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::Unit => visitor.visit_unit(), Content::None => visitor.visit_none(), Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), Content::Seq(v) => visit_content_seq(v, visitor), Content::Map(v) => visit_content_map(v, visitor), } } fn deserialize_bool(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Bool(v) => visitor.visit_bool(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_i8(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i16(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u8(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u16(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_float(visitor) } fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_float(visitor) } fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Char(v) => visitor.visit_char(v), Content::String(v) => visitor.visit_string(v), Content::Str(v) => visitor.visit_borrowed_str(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_str(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_string(visitor) } fn deserialize_string(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::String(v) => visitor.visit_string(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(v) => visitor.visit_byte_buf(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_byte_buf(visitor) } fn deserialize_byte_buf(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::String(v) => visitor.visit_string(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(v) => visitor.visit_byte_buf(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::Seq(v) => visit_content_seq(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::None => visitor.visit_none(), Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), Content::Unit => visitor.visit_unit(), _ => visitor.visit_some(self), } } fn deserialize_unit(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Unit => visitor.visit_unit(), // Allow deserializing newtype variant containing unit. // // #[derive(Deserialize)] // #[serde(tag = "result")] // enum Response { // Success(T), // } // // We want {"result":"Success"} to deserialize into Response<()>. Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_unit_struct( self, _name: &'static str, visitor: V, ) -> Result where V: Visitor<'de>, { match self.content { // As a special case, allow deserializing untagged newtype // variant containing unit struct. // // #[derive(Deserialize)] // struct Info; // // #[derive(Deserialize)] // #[serde(tag = "topic")] // enum Message { // Info(Info), // } // // We want {"topic":"Info"} to deserialize even though // ordinarily unit structs do not deserialize from empty map/seq. Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), _ => self.deserialize_any(visitor), } } fn deserialize_newtype_struct( self, _name: &str, visitor: V, ) -> Result where V: Visitor<'de>, { match self.content { Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), _ => visitor.visit_newtype_struct(self), } } fn deserialize_seq(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Seq(v) => visit_content_seq(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_tuple(self, _len: usize, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_seq(visitor) } fn deserialize_tuple_struct( self, _name: &'static str, _len: usize, visitor: V, ) -> Result where V: Visitor<'de>, { self.deserialize_seq(visitor) } fn deserialize_map(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::Map(v) => visit_content_map(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_struct( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { match self.content { Content::Seq(v) => visit_content_seq(v, visitor), Content::Map(v) => visit_content_map(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_enum( self, _name: &str, _variants: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { let (variant, value) = match self.content { Content::Map(value) => { let mut iter = value.into_iter(); let (variant, value) = match iter.next() { Some(v) => v, None => { return Err(de::Error::invalid_value( de::Unexpected::Map, &"map with a single key", )); } }; // enums are encoded in json as maps with a single key:value pair if iter.next().is_some() { return Err(de::Error::invalid_value( de::Unexpected::Map, &"map with a single key", )); } (variant, Some(value)) } s @ Content::String(_) | s @ Content::Str(_) => (s, None), other => { return Err(de::Error::invalid_type( other.unexpected(), &"string or map", )); } }; visitor.visit_enum(EnumDeserializer::new(variant, value)) } fn deserialize_identifier(self, visitor: V) -> Result where V: Visitor<'de>, { match self.content { Content::String(v) => visitor.visit_string(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(v) => visitor.visit_byte_buf(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::U8(v) => visitor.visit_u8(v), Content::U64(v) => visitor.visit_u64(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, { drop(self); visitor.visit_unit() } fn __deserialize_content( self, _: actually_private::T, visitor: V, ) -> Result, Self::Error> where V: Visitor<'de, Value = Content<'de>>, { let _ = visitor; Ok(self.content) } } impl<'de, E> ContentDeserializer<'de, E> { /// private API, don't use pub fn new(content: Content<'de>) -> Self { ContentDeserializer { content, err: PhantomData, } } } pub struct EnumDeserializer<'de, E> where E: de::Error, { variant: Content<'de>, value: Option>, err: PhantomData, } impl<'de, E> EnumDeserializer<'de, E> where E: de::Error, { pub fn new(variant: Content<'de>, value: Option>) -> EnumDeserializer<'de, E> { EnumDeserializer { variant, value, err: PhantomData, } } } impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E> where E: de::Error, { type Error = E; type Variant = VariantDeserializer<'de, Self::Error>; fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), E> where V: de::DeserializeSeed<'de>, { let visitor = VariantDeserializer { value: self.value, err: PhantomData, }; seed.deserialize(ContentDeserializer::new(self.variant)) .map(|v| (v, visitor)) } } pub struct VariantDeserializer<'de, E> where E: de::Error, { value: Option>, err: PhantomData, } impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E> where E: de::Error, { type Error = E; fn unit_variant(self) -> Result<(), E> { match self.value { Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)), None => Ok(()), } } fn newtype_variant_seed(self, seed: T) -> Result where T: de::DeserializeSeed<'de>, { match self.value { Some(value) => seed.deserialize(ContentDeserializer::new(value)), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"newtype variant", )), } } fn tuple_variant(self, _len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { match self.value { Some(Content::Seq(v)) => { de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) } Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"tuple variant", )), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"tuple variant", )), } } fn struct_variant( self, _fields: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { match self.value { Some(Content::Map(v)) => { de::Deserializer::deserialize_any(MapDeserializer::new(v.into_iter()), visitor) } Some(Content::Seq(v)) => { de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) } Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"struct variant", )), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"struct variant", )), } } } /// Not public API. pub struct ContentRefDeserializer<'a, 'de: 'a, E> { content: &'a Content<'de>, err: PhantomData, } impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> where E: de::Error, { #[cold] fn invalid_type(self, exp: &Expected) -> E { de::Error::invalid_type(self.content.unexpected(), exp) } fn deserialize_integer(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_float(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::F32(v) => visitor.visit_f32(v), Content::F64(v) => visitor.visit_f64(v), Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), _ => Err(self.invalid_type(&visitor)), } } } fn visit_content_seq_ref<'a, 'de, V, E>( content: &'a [Content<'de>], visitor: V, ) -> Result where V: Visitor<'de>, E: de::Error, { let mut seq_visitor = SeqDeserializer::new(content.iter()); let value = tri!(visitor.visit_seq(&mut seq_visitor)); tri!(seq_visitor.end()); Ok(value) } fn visit_content_map_ref<'a, 'de, V, E>( content: &'a [(Content<'de>, Content<'de>)], visitor: V, ) -> Result where V: Visitor<'de>, E: de::Error, { fn content_ref_deserializer_pair<'a, 'de>( (k, v): &'a (Content<'de>, Content<'de>), ) -> (&'a Content<'de>, &'a Content<'de>) { (k, v) } let map = content.iter().map(content_ref_deserializer_pair); let mut map_visitor = MapDeserializer::new(map); let value = tri!(visitor.visit_map(&mut map_visitor)); tri!(map_visitor.end()); Ok(value) } /// Used when deserializing an untagged enum because the content may need /// to be used more than once. impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E> where E: de::Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Bool(v) => visitor.visit_bool(v), Content::U8(v) => visitor.visit_u8(v), Content::U16(v) => visitor.visit_u16(v), Content::U32(v) => visitor.visit_u32(v), Content::U64(v) => visitor.visit_u64(v), Content::I8(v) => visitor.visit_i8(v), Content::I16(v) => visitor.visit_i16(v), Content::I32(v) => visitor.visit_i32(v), Content::I64(v) => visitor.visit_i64(v), Content::F32(v) => visitor.visit_f32(v), Content::F64(v) => visitor.visit_f64(v), Content::Char(v) => visitor.visit_char(v), Content::String(ref v) => visitor.visit_str(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(ref v) => visitor.visit_bytes(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::Unit => visitor.visit_unit(), Content::None => visitor.visit_none(), Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)), Content::Newtype(ref v) => { visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) } Content::Seq(ref v) => visit_content_seq_ref(v, visitor), Content::Map(ref v) => visit_content_map_ref(v, visitor), } } fn deserialize_bool(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Bool(v) => visitor.visit_bool(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_i8(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i16(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_i64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u8(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u16(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_u64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_integer(visitor) } fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_float(visitor) } fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_float(visitor) } fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Char(v) => visitor.visit_char(v), Content::String(ref v) => visitor.visit_str(v), Content::Str(v) => visitor.visit_borrowed_str(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_str(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::String(ref v) => visitor.visit_str(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(ref v) => visitor.visit_bytes(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_string(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_str(visitor) } fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::String(ref v) => visitor.visit_str(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(ref v) => visitor.visit_bytes(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::Seq(ref v) => visit_content_seq_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_byte_buf(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_bytes(visitor) } fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, { // Covered by tests/test_enum_untagged.rs // with_optional_field::* match *self.content { Content::None => visitor.visit_none(), Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)), Content::Unit => visitor.visit_unit(), // This case is to support data formats which do not encode an // indication whether a value is optional. An example of such a // format is JSON, and a counterexample is RON. When requesting // `deserialize_any` in JSON, the data format never performs // `Visitor::visit_some` but we still must be able to // deserialize the resulting Content into data structures with // optional fields. _ => visitor.visit_some(self), } } fn deserialize_unit(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Unit => visitor.visit_unit(), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_unit_struct( self, _name: &'static str, visitor: V, ) -> Result where V: Visitor<'de>, { self.deserialize_unit(visitor) } fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result where V: Visitor<'de>, { // Covered by tests/test_enum_untagged.rs // newtype_struct match *self.content { Content::Newtype(ref v) => { visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) } // This case is to support data formats that encode newtype // structs and their underlying data the same, with no // indication whether a newtype wrapper was present. For example // JSON does this, while RON does not. In RON a newtype's name // is included in the serialized representation and it knows to // call `Visitor::visit_newtype_struct` from `deserialize_any`. // JSON's `deserialize_any` never calls `visit_newtype_struct` // but in this code we still must be able to deserialize the // resulting Content into newtypes. _ => visitor.visit_newtype_struct(self), } } fn deserialize_seq(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Seq(ref v) => visit_content_seq_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_tuple(self, _len: usize, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_seq(visitor) } fn deserialize_tuple_struct( self, _name: &'static str, _len: usize, visitor: V, ) -> Result where V: Visitor<'de>, { self.deserialize_seq(visitor) } fn deserialize_map(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::Map(ref v) => visit_content_map_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_struct( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { match *self.content { Content::Seq(ref v) => visit_content_seq_ref(v, visitor), Content::Map(ref v) => visit_content_map_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_enum( self, _name: &str, _variants: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { let (variant, value) = match *self.content { Content::Map(ref value) => { let mut iter = value.iter(); let (variant, value) = match iter.next() { Some(v) => v, None => { return Err(de::Error::invalid_value( de::Unexpected::Map, &"map with a single key", )); } }; // enums are encoded in json as maps with a single key:value pair if iter.next().is_some() { return Err(de::Error::invalid_value( de::Unexpected::Map, &"map with a single key", )); } (variant, Some(value)) } ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None), ref other => { return Err(de::Error::invalid_type( other.unexpected(), &"string or map", )); } }; visitor.visit_enum(EnumRefDeserializer { variant, value, err: PhantomData, }) } fn deserialize_identifier(self, visitor: V) -> Result where V: Visitor<'de>, { match *self.content { Content::String(ref v) => visitor.visit_str(v), Content::Str(v) => visitor.visit_borrowed_str(v), Content::ByteBuf(ref v) => visitor.visit_bytes(v), Content::Bytes(v) => visitor.visit_borrowed_bytes(v), Content::U8(v) => visitor.visit_u8(v), Content::U64(v) => visitor.visit_u64(v), _ => Err(self.invalid_type(&visitor)), } } fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_unit() } fn __deserialize_content( self, _: actually_private::T, visitor: V, ) -> Result, Self::Error> where V: Visitor<'de, Value = Content<'de>>, { let _ = visitor; Ok(self.content.clone()) } } impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> { /// private API, don't use pub fn new(content: &'a Content<'de>) -> Self { ContentRefDeserializer { 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, { variant: &'a Content<'de>, value: Option<&'a Content<'de>>, err: PhantomData, } impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E> where E: de::Error, { type Error = E; type Variant = VariantRefDeserializer<'a, 'de, Self::Error>; fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: de::DeserializeSeed<'de>, { let visitor = VariantRefDeserializer { value: self.value, err: PhantomData, }; seed.deserialize(ContentRefDeserializer::new(self.variant)) .map(|v| (v, visitor)) } } struct VariantRefDeserializer<'a, 'de: 'a, E> where E: de::Error, { value: Option<&'a Content<'de>>, err: PhantomData, } impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E> where E: de::Error, { type Error = E; fn unit_variant(self) -> Result<(), E> { match self.value { Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)), // Covered by tests/test_annotations.rs // test_partially_untagged_adjacently_tagged_enum // Covered by tests/test_enum_untagged.rs // newtype_enum::unit None => Ok(()), } } fn newtype_variant_seed(self, seed: T) -> Result where T: de::DeserializeSeed<'de>, { match self.value { // Covered by tests/test_annotations.rs // test_partially_untagged_enum_desugared // test_partially_untagged_enum_generic // Covered by tests/test_enum_untagged.rs // newtype_enum::newtype Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"newtype variant", )), } } fn tuple_variant(self, _len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { match self.value { // Covered by tests/test_annotations.rs // test_partially_untagged_enum // test_partially_untagged_enum_desugared // Covered by tests/test_enum_untagged.rs // newtype_enum::tuple0 // newtype_enum::tuple2 Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"tuple variant", )), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"tuple variant", )), } } fn struct_variant( self, _fields: &'static [&'static str], visitor: V, ) -> Result where V: de::Visitor<'de>, { match self.value { // Covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_map Some(Content::Map(v)) => visit_content_map_ref(v, visitor), // Covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_seq // newtype_enum::empty_struct_from_seq Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"struct variant", )), None => Err(de::Error::invalid_type( de::Unexpected::UnitVariant, &"struct variant", )), } } } impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E> where E: de::Error, { type Deserializer = Self; fn into_deserializer(self) -> Self { self } } /// Visitor for deserializing an internally tagged unit variant. /// /// Not public API. pub struct InternallyTaggedUnitVisitor<'a> { type_name: &'a str, variant_name: &'a str, } impl<'a> InternallyTaggedUnitVisitor<'a> { /// Not public API. pub fn new(type_name: &'a str, variant_name: &'a str) -> Self { InternallyTaggedUnitVisitor { type_name, variant_name, } } } impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( formatter, "unit variant {}::{}", self.type_name, self.variant_name ) } fn visit_seq(self, _: S) -> Result<(), S::Error> where S: SeqAccess<'de>, { Ok(()) } fn visit_map(self, mut access: M) -> Result<(), M::Error> where M: MapAccess<'de>, { while tri!(access.next_entry::()).is_some() {} Ok(()) } } /// Visitor for deserializing an untagged unit variant. /// /// Not public API. pub struct UntaggedUnitVisitor<'a> { type_name: &'a str, variant_name: &'a str, } impl<'a> UntaggedUnitVisitor<'a> { /// Not public API. pub fn new(type_name: &'a str, variant_name: &'a str) -> Self { UntaggedUnitVisitor { type_name, variant_name, } } } impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( formatter, "unit variant {}::{}", self.type_name, self.variant_name ) } fn visit_unit(self) -> Result<(), E> where E: de::Error, { Ok(()) } fn visit_none(self) -> Result<(), E> where E: de::Error, { Ok(()) } } } //////////////////////////////////////////////////////////////////////////////// // Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for // the newtype fallthrough case of `field_identifier`. // // #[derive(Deserialize)] // #[serde(field_identifier)] // enum F { // A, // B, // Other(String), // deserialized using IdentifierDeserializer // } pub trait IdentifierDeserializer<'de, E: Error> { type Deserializer: Deserializer<'de, Error = E>; fn from(self) -> Self::Deserializer; } pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T); impl<'de, E> IdentifierDeserializer<'de, E> for u64 where E: Error, { type Deserializer = >::Deserializer; fn from(self) -> Self::Deserializer { self.into_deserializer() } } pub struct StrDeserializer<'a, E> { value: &'a str, marker: PhantomData, } impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E> where E: Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_str(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 } } pub struct BorrowedStrDeserializer<'de, E> { value: &'de str, marker: PhantomData, } impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E> where E: Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_borrowed_str(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 } } impl<'a, E> IdentifierDeserializer<'a, E> for &'a str where E: Error, { type Deserializer = StrDeserializer<'a, E>; fn from(self) -> Self::Deserializer { StrDeserializer { value: self, marker: PhantomData, } } } impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str> where E: Error, { type Deserializer = BorrowedStrDeserializer<'de, E>; fn from(self) -> Self::Deserializer { BorrowedStrDeserializer { value: self.0, marker: PhantomData, } } } impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8] where E: Error, { type Deserializer = BytesDeserializer<'a, E>; fn from(self) -> Self::Deserializer { BytesDeserializer::new(self) } } impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]> where E: Error, { type Deserializer = BorrowedBytesDeserializer<'de, E>; fn from(self) -> Self::Deserializer { BorrowedBytesDeserializer::new(self.0) } } #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapDeserializer<'a, 'de: 'a, E>( pub &'a mut Vec, Content<'de>)>>, pub PhantomData, ); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E> where E: Error, { fn deserialize_other() -> Result { Err(Error::custom("can only flatten structs and maps")) } } #[cfg(any(feature = "std", feature = "alloc"))] macro_rules! forward_to_deserialize_other { ($($func:ident ($($arg:ty),*))*) => { $( fn $func(self, $(_: $arg,)* _visitor: V) -> Result where V: Visitor<'de>, { Self::deserialize_other() } )* } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> where E: Error, { type Error = E; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, { self.deserialize_map(visitor) } fn deserialize_enum( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { 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))); } } Err(Error::custom(format_args!( "no variant of enum {} found in flattened data", name ))) } fn deserialize_map(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_map(FlatMapAccess { iter: self.0.iter(), pending_content: None, _marker: PhantomData, }) } fn deserialize_struct( self, _: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result where V: Visitor<'de>, { visitor.visit_map(FlatStructAccess { iter: self.0.iter_mut(), pending_content: None, fields, _marker: PhantomData, }) } fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_newtype_struct(self) } fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, { match visitor.__private_visit_untagged_option(self) { Ok(value) => Ok(value), Err(()) => Self::deserialize_other(), } } fn deserialize_unit(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_unit() } fn deserialize_unit_struct( self, _name: &'static str, visitor: V, ) -> Result where V: Visitor<'de>, { visitor.visit_unit() } fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, { visitor.visit_unit() } forward_to_deserialize_other! { deserialize_bool() deserialize_i8() deserialize_i16() deserialize_i32() deserialize_i64() deserialize_u8() deserialize_u16() deserialize_u32() deserialize_u64() deserialize_f32() deserialize_f64() deserialize_char() deserialize_str() deserialize_string() deserialize_bytes() deserialize_byte_buf() deserialize_seq() deserialize_tuple(usize) deserialize_tuple_struct(&'static str, usize) deserialize_identifier() } } #[cfg(any(feature = "std", feature = "alloc"))] struct FlatMapAccess<'a, 'de: 'a, E> { iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>, pending_content: Option<&'a Content<'de>>, _marker: PhantomData, } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> where E: Error, { type Error = E; fn next_key_seed(&mut self, seed: T) -> Result, Self::Error> where T: DeserializeSeed<'de>, { 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); } } Ok(None) } fn next_value_seed(&mut self, seed: T) -> Result where T: DeserializeSeed<'de>, { match self.pending_content.take() { Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), None => Err(Error::custom("value is missing")), } } } #[cfg(any(feature = "std", feature = "alloc"))] struct FlatStructAccess<'a, 'de: 'a, E> { iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, pending_content: Option>, fields: &'static [&'static str], _marker: PhantomData, } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E> where E: Error, { type Error = E; fn next_key_seed(&mut self, seed: T) -> Result, Self::Error> where T: DeserializeSeed<'de>, { 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); } } Ok(None) } fn next_value_seed(&mut self, seed: T) -> Result where T: DeserializeSeed<'de>, { match self.pending_content.take() { Some(value) => seed.deserialize(ContentDeserializer::new(value)), None => Err(Error::custom("value is missing")), } } } /// 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"))] 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 } } pub struct AdjacentlyTaggedEnumVariantSeed { pub enum_name: &'static str, pub variants: &'static [&'static str], pub fields_enum: PhantomData, } pub struct AdjacentlyTaggedEnumVariantVisitor { enum_name: &'static str, fields_enum: PhantomData, } impl<'de, F> Visitor<'de> for AdjacentlyTaggedEnumVariantVisitor where F: Deserialize<'de>, { type Value = F; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "variant of enum {}", self.enum_name) } fn visit_enum(self, data: A) -> Result where A: EnumAccess<'de>, { let (variant, variant_access) = tri!(data.variant()); tri!(variant_access.unit_variant()); Ok(variant) } } impl<'de, F> DeserializeSeed<'de> for AdjacentlyTaggedEnumVariantSeed where F: Deserialize<'de>, { type Value = F; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { deserializer.deserialize_enum( self.enum_name, self.variants, AdjacentlyTaggedEnumVariantVisitor { enum_name: self.enum_name, fields_enum: PhantomData, }, ) } } serde-1.0.217/src/private/doc.rs000064400000000000000000000115021046102023000144510ustar 00000000000000// Used only by Serde doc tests. Not public API. use crate::lib::*; use crate::ser; #[doc(hidden)] #[derive(Debug)] pub struct Error; impl ser::Error for Error { fn custom(_: T) -> Self where T: Display, { unimplemented!() } } #[cfg(feature = "std")] impl error::Error for Error { fn description(&self) -> &str { unimplemented!() } } impl Display for Error { fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { unimplemented!() } } #[doc(hidden)] #[macro_export] macro_rules! __private_serialize { () => { trait Serialize { fn serialize(&self, serializer: S) -> Result where S: $crate::Serializer; } }; } #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! __serialize_unimplemented { ($($func:ident)*) => { $( __serialize_unimplemented_helper!($func); )* }; } #[doc(hidden)] #[macro_export] macro_rules! __serialize_unimplemented_method { ($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => { fn $func $(<$t>)* (self $(, _: $arg)*) -> $crate::__private::Result where $($t: ?Sized + $crate::Serialize,)* { unimplemented!() } }; } #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! __serialize_unimplemented_helper { (bool) => { __serialize_unimplemented_method!(serialize_bool(bool) -> Ok); }; (i8) => { __serialize_unimplemented_method!(serialize_i8(i8) -> Ok); }; (i16) => { __serialize_unimplemented_method!(serialize_i16(i16) -> Ok); }; (i32) => { __serialize_unimplemented_method!(serialize_i32(i32) -> Ok); }; (i64) => { __serialize_unimplemented_method!(serialize_i64(i64) -> Ok); }; (u8) => { __serialize_unimplemented_method!(serialize_u8(u8) -> Ok); }; (u16) => { __serialize_unimplemented_method!(serialize_u16(u16) -> Ok); }; (u32) => { __serialize_unimplemented_method!(serialize_u32(u32) -> Ok); }; (u64) => { __serialize_unimplemented_method!(serialize_u64(u64) -> Ok); }; (f32) => { __serialize_unimplemented_method!(serialize_f32(f32) -> Ok); }; (f64) => { __serialize_unimplemented_method!(serialize_f64(f64) -> Ok); }; (char) => { __serialize_unimplemented_method!(serialize_char(char) -> Ok); }; (str) => { __serialize_unimplemented_method!(serialize_str(&str) -> Ok); }; (bytes) => { __serialize_unimplemented_method!(serialize_bytes(&[u8]) -> Ok); }; (none) => { __serialize_unimplemented_method!(serialize_none() -> Ok); }; (some) => { __serialize_unimplemented_method!(serialize_some(&T) -> Ok); }; (unit) => { __serialize_unimplemented_method!(serialize_unit() -> Ok); }; (unit_struct) => { __serialize_unimplemented_method!(serialize_unit_struct(&str) -> Ok); }; (unit_variant) => { __serialize_unimplemented_method!(serialize_unit_variant(&str, u32, &str) -> Ok); }; (newtype_struct) => { __serialize_unimplemented_method!(serialize_newtype_struct(&str, &T) -> Ok); }; (newtype_variant) => { __serialize_unimplemented_method!(serialize_newtype_variant(&str, u32, &str, &T) -> Ok); }; (seq) => { type SerializeSeq = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_seq(Option) -> SerializeSeq); }; (tuple) => { type SerializeTuple = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple); }; (tuple_struct) => { type SerializeTupleStruct = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_tuple_struct(&str, usize) -> SerializeTupleStruct); }; (tuple_variant) => { type SerializeTupleVariant = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_tuple_variant(&str, u32, &str, usize) -> SerializeTupleVariant); }; (map) => { type SerializeMap = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_map(Option) -> SerializeMap); }; (struct) => { type SerializeStruct = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_struct(&str, usize) -> SerializeStruct); }; (struct_variant) => { type SerializeStructVariant = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_struct_variant(&str, u32, &str, usize) -> SerializeStructVariant); }; } serde-1.0.217/src/private/mod.rs000064400000000000000000000030371046102023000144670ustar 00000000000000#[cfg(not(no_serde_derive))] pub mod de; #[cfg(not(no_serde_derive))] pub mod ser; // FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed. pub mod doc; 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 crate::lib::{ToString, Vec}; #[cfg(not(no_core_try_from))] pub use crate::lib::convert::TryFrom; mod string { use crate::lib::*; #[cfg(any(feature = "std", feature = "alloc"))] pub fn from_utf8_lossy(bytes: &[u8]) -> Cow { String::from_utf8_lossy(bytes) } // The generated code calls this like: // // let value = &_serde::__private::from_utf8_lossy(bytes); // Err(_serde::de::Error::unknown_variant(value, VARIANTS)) // // so it is okay for the return type to be different from the std case as long // as the above works. #[cfg(not(any(feature = "std", feature = "alloc")))] pub fn from_utf8_lossy(bytes: &[u8]) -> &str { // Three unicode replacement characters if it fails. They look like a // white-on-black question mark. The user will recognize it as invalid // UTF-8. str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}") } } serde-1.0.217/src/private/ser.rs000064400000000000000000001161141046102023000145020ustar 00000000000000use crate::lib::*; use crate::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; #[cfg(any(feature = "std", feature = "alloc"))] use self::content::{ Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue, }; /// Used to check that serde(getter) attributes return the expected type. /// Not public API. pub fn constrain(t: &T) -> &T { t } /// Not public API. pub fn serialize_tagged_newtype( serializer: S, type_ident: &'static str, variant_ident: &'static str, tag: &'static str, variant_name: &'static str, value: &T, ) -> Result where S: Serializer, T: Serialize, { value.serialize(TaggedSerializer { type_ident, variant_ident, tag, variant_name, delegate: serializer, }) } struct TaggedSerializer { type_ident: &'static str, variant_ident: &'static str, tag: &'static str, variant_name: &'static str, delegate: S, } enum Unsupported { Boolean, Integer, Float, Char, String, ByteArray, Optional, Sequence, Tuple, TupleStruct, #[cfg(not(any(feature = "std", feature = "alloc")))] Enum, } impl Display for Unsupported { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match *self { Unsupported::Boolean => formatter.write_str("a boolean"), Unsupported::Integer => formatter.write_str("an integer"), Unsupported::Float => formatter.write_str("a float"), Unsupported::Char => formatter.write_str("a char"), Unsupported::String => formatter.write_str("a string"), Unsupported::ByteArray => formatter.write_str("a byte array"), Unsupported::Optional => formatter.write_str("an optional"), Unsupported::Sequence => formatter.write_str("a sequence"), Unsupported::Tuple => formatter.write_str("a tuple"), Unsupported::TupleStruct => formatter.write_str("a tuple struct"), #[cfg(not(any(feature = "std", feature = "alloc")))] Unsupported::Enum => formatter.write_str("an enum"), } } } impl TaggedSerializer where S: Serializer, { fn bad_type(self, what: Unsupported) -> S::Error { ser::Error::custom(format_args!( "cannot serialize tagged newtype variant {}::{} containing {}", self.type_ident, self.variant_ident, what )) } } impl Serializer for TaggedSerializer where S: Serializer, { type Ok = S::Ok; type Error = S::Error; type SerializeSeq = Impossible; type SerializeTuple = Impossible; type SerializeTupleStruct = Impossible; type SerializeMap = S::SerializeMap; type SerializeStruct = S::SerializeStruct; #[cfg(not(any(feature = "std", feature = "alloc")))] type SerializeTupleVariant = Impossible; #[cfg(any(feature = "std", feature = "alloc"))] type SerializeTupleVariant = SerializeTupleVariantAsMapValue; #[cfg(not(any(feature = "std", feature = "alloc")))] type SerializeStructVariant = Impossible; #[cfg(any(feature = "std", feature = "alloc"))] type SerializeStructVariant = SerializeStructVariantAsMapValue; fn serialize_bool(self, _: bool) -> Result { Err(self.bad_type(Unsupported::Boolean)) } fn serialize_i8(self, _: i8) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_i16(self, _: i16) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_i32(self, _: i32) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_i64(self, _: i64) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_u8(self, _: u8) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_u16(self, _: u16) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_u32(self, _: u32) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_u64(self, _: u64) -> Result { Err(self.bad_type(Unsupported::Integer)) } fn serialize_f32(self, _: f32) -> Result { Err(self.bad_type(Unsupported::Float)) } fn serialize_f64(self, _: f64) -> Result { Err(self.bad_type(Unsupported::Float)) } fn serialize_char(self, _: char) -> Result { Err(self.bad_type(Unsupported::Char)) } fn serialize_str(self, _: &str) -> Result { Err(self.bad_type(Unsupported::String)) } fn serialize_bytes(self, _: &[u8]) -> Result { Err(self.bad_type(Unsupported::ByteArray)) } fn serialize_none(self) -> Result { Err(self.bad_type(Unsupported::Optional)) } fn serialize_some(self, _: &T) -> Result where T: ?Sized + Serialize, { Err(self.bad_type(Unsupported::Optional)) } fn serialize_unit(self) -> Result { 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 { let mut map = tri!(self.delegate.serialize_map(Some(1))); tri!(map.serialize_entry(self.tag, self.variant_name)); map.end() } fn serialize_unit_variant( self, _: &'static str, _: u32, inner_variant: &'static str, ) -> Result { 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() } fn serialize_newtype_struct( self, _: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize, { value.serialize(self) } fn serialize_newtype_variant( self, _: &'static str, _: u32, inner_variant: &'static str, inner_value: &T, ) -> Result where T: ?Sized + Serialize, { 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() } fn serialize_seq(self, _: Option) -> Result { Err(self.bad_type(Unsupported::Sequence)) } fn serialize_tuple(self, _: usize) -> Result { Err(self.bad_type(Unsupported::Tuple)) } fn serialize_tuple_struct( self, _: &'static str, _: usize, ) -> Result { Err(self.bad_type(Unsupported::TupleStruct)) } #[cfg(not(any(feature = "std", feature = "alloc")))] fn serialize_tuple_variant( self, _: &'static str, _: u32, _: &'static str, _: usize, ) -> Result { // Lack of push-based serialization means we need to buffer the content // of the tuple variant, so it requires std. Err(self.bad_type(Unsupported::Enum)) } #[cfg(any(feature = "std", feature = "alloc"))] fn serialize_tuple_variant( self, _: &'static str, _: u32, inner_variant: &'static str, len: usize, ) -> Result { 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, len, )) } fn serialize_map(self, len: Option) -> Result { let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1))); tri!(map.serialize_entry(self.tag, self.variant_name)); Ok(map) } fn serialize_struct( self, name: &'static str, len: usize, ) -> Result { let mut state = tri!(self.delegate.serialize_struct(name, len + 1)); tri!(state.serialize_field(self.tag, self.variant_name)); Ok(state) } #[cfg(not(any(feature = "std", feature = "alloc")))] fn serialize_struct_variant( self, _: &'static str, _: u32, _: &'static str, _: usize, ) -> Result { // Lack of push-based serialization means we need to buffer the content // of the struct variant, so it requires std. Err(self.bad_type(Unsupported::Enum)) } #[cfg(any(feature = "std", feature = "alloc"))] fn serialize_struct_variant( self, _: &'static str, _: u32, inner_variant: &'static str, len: usize, ) -> Result { 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, len, )) } #[cfg(not(any(feature = "std", feature = "alloc")))] fn collect_str(self, _: &T) -> Result where T: ?Sized + Display, { Err(self.bad_type(Unsupported::String)) } } #[cfg(any(feature = "std", feature = "alloc"))] mod content { use crate::lib::*; use crate::ser::{self, Serialize, Serializer}; pub struct SerializeTupleVariantAsMapValue { map: M, name: &'static str, fields: Vec, } impl SerializeTupleVariantAsMapValue { pub fn new(map: M, name: &'static str, len: usize) -> Self { SerializeTupleVariantAsMapValue { map, name, fields: Vec::with_capacity(len), } } } impl ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue where M: ser::SerializeMap, { type Ok = M::Ok; type Error = M::Error; fn serialize_field(&mut self, value: &T) -> Result<(), M::Error> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push(value); Ok(()) } fn end(mut self) -> Result { tri!(self .map .serialize_value(&Content::TupleStruct(self.name, self.fields))); self.map.end() } } pub struct SerializeStructVariantAsMapValue { map: M, name: &'static str, fields: Vec<(&'static str, Content)>, } impl SerializeStructVariantAsMapValue { pub fn new(map: M, name: &'static str, len: usize) -> Self { SerializeStructVariantAsMapValue { map, name, fields: Vec::with_capacity(len), } } } impl ser::SerializeStructVariant for SerializeStructVariantAsMapValue where M: ser::SerializeMap, { type Ok = M::Ok; type Error = M::Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), M::Error> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push((key, value)); Ok(()) } fn end(mut self) -> Result { tri!(self .map .serialize_value(&Content::Struct(self.name, self.fields))); self.map.end() } } pub enum Content { Bool(bool), U8(u8), U16(u16), U32(u32), U64(u64), I8(i8), I16(i16), I32(i32), I64(i64), F32(f32), F64(f64), Char(char), String(String), Bytes(Vec), None, Some(Box), Unit, UnitStruct(&'static str), UnitVariant(&'static str, u32, &'static str), NewtypeStruct(&'static str, Box), NewtypeVariant(&'static str, u32, &'static str, Box), Seq(Vec), Tuple(Vec), TupleStruct(&'static str, Vec), TupleVariant(&'static str, u32, &'static str, Vec), Map(Vec<(Content, Content)>), Struct(&'static str, Vec<(&'static str, Content)>), StructVariant( &'static str, u32, &'static str, Vec<(&'static str, Content)>, ), } impl Serialize for Content { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { Content::Bool(b) => serializer.serialize_bool(b), Content::U8(u) => serializer.serialize_u8(u), Content::U16(u) => serializer.serialize_u16(u), Content::U32(u) => serializer.serialize_u32(u), Content::U64(u) => serializer.serialize_u64(u), Content::I8(i) => serializer.serialize_i8(i), Content::I16(i) => serializer.serialize_i16(i), Content::I32(i) => serializer.serialize_i32(i), Content::I64(i) => serializer.serialize_i64(i), Content::F32(f) => serializer.serialize_f32(f), Content::F64(f) => serializer.serialize_f64(f), Content::Char(c) => serializer.serialize_char(c), Content::String(ref s) => serializer.serialize_str(s), Content::Bytes(ref b) => serializer.serialize_bytes(b), Content::None => serializer.serialize_none(), Content::Some(ref c) => serializer.serialize_some(&**c), Content::Unit => serializer.serialize_unit(), Content::UnitStruct(n) => serializer.serialize_unit_struct(n), Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v), Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c), Content::NewtypeVariant(n, i, v, ref c) => { serializer.serialize_newtype_variant(n, i, v, &**c) } Content::Seq(ref elements) => elements.serialize(serializer), Content::Tuple(ref elements) => { use crate::ser::SerializeTuple; let mut tuple = tri!(serializer.serialize_tuple(elements.len())); for e in elements { tri!(tuple.serialize_element(e)); } tuple.end() } Content::TupleStruct(n, ref fields) => { use crate::ser::SerializeTupleStruct; let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len())); for f in fields { tri!(ts.serialize_field(f)); } ts.end() } Content::TupleVariant(n, i, v, ref fields) => { use crate::ser::SerializeTupleVariant; let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len())); for f in fields { tri!(tv.serialize_field(f)); } tv.end() } Content::Map(ref entries) => { use crate::ser::SerializeMap; let mut map = tri!(serializer.serialize_map(Some(entries.len()))); for (k, v) in entries { tri!(map.serialize_entry(k, v)); } map.end() } Content::Struct(n, ref fields) => { use crate::ser::SerializeStruct; let mut s = tri!(serializer.serialize_struct(n, fields.len())); for &(k, ref v) in fields { tri!(s.serialize_field(k, v)); } s.end() } Content::StructVariant(n, i, v, ref fields) => { use crate::ser::SerializeStructVariant; let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len())); for &(k, ref v) in fields { tri!(sv.serialize_field(k, v)); } sv.end() } } } } pub struct ContentSerializer { error: PhantomData, } impl ContentSerializer { pub fn new() -> Self { ContentSerializer { error: PhantomData } } } impl Serializer for ContentSerializer where E: ser::Error, { type Ok = Content; type Error = E; type SerializeSeq = SerializeSeq; type SerializeTuple = SerializeTuple; type SerializeTupleStruct = SerializeTupleStruct; type SerializeTupleVariant = SerializeTupleVariant; type SerializeMap = SerializeMap; type SerializeStruct = SerializeStruct; type SerializeStructVariant = SerializeStructVariant; fn serialize_bool(self, v: bool) -> Result { Ok(Content::Bool(v)) } fn serialize_i8(self, v: i8) -> Result { Ok(Content::I8(v)) } fn serialize_i16(self, v: i16) -> Result { Ok(Content::I16(v)) } fn serialize_i32(self, v: i32) -> Result { Ok(Content::I32(v)) } fn serialize_i64(self, v: i64) -> Result { Ok(Content::I64(v)) } fn serialize_u8(self, v: u8) -> Result { Ok(Content::U8(v)) } fn serialize_u16(self, v: u16) -> Result { Ok(Content::U16(v)) } fn serialize_u32(self, v: u32) -> Result { Ok(Content::U32(v)) } fn serialize_u64(self, v: u64) -> Result { Ok(Content::U64(v)) } fn serialize_f32(self, v: f32) -> Result { Ok(Content::F32(v)) } fn serialize_f64(self, v: f64) -> Result { Ok(Content::F64(v)) } fn serialize_char(self, v: char) -> Result { Ok(Content::Char(v)) } fn serialize_str(self, value: &str) -> Result { Ok(Content::String(value.to_owned())) } fn serialize_bytes(self, value: &[u8]) -> Result { Ok(Content::Bytes(value.to_owned())) } fn serialize_none(self) -> Result { Ok(Content::None) } fn serialize_some(self, value: &T) -> Result where T: ?Sized + Serialize, { Ok(Content::Some(Box::new(tri!(value.serialize(self))))) } fn serialize_unit(self) -> Result { Ok(Content::Unit) } fn serialize_unit_struct(self, name: &'static str) -> Result { Ok(Content::UnitStruct(name)) } fn serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result { Ok(Content::UnitVariant(name, variant_index, variant)) } fn serialize_newtype_struct(self, name: &'static str, value: &T) -> Result where T: ?Sized + Serialize, { Ok(Content::NewtypeStruct( name, Box::new(tri!(value.serialize(self))), )) } fn serialize_newtype_variant( self, name: &'static str, variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize, { Ok(Content::NewtypeVariant( name, variant_index, variant, Box::new(tri!(value.serialize(self))), )) } fn serialize_seq(self, len: Option) -> Result { Ok(SerializeSeq { elements: Vec::with_capacity(len.unwrap_or(0)), error: PhantomData, }) } fn serialize_tuple(self, len: usize) -> Result { Ok(SerializeTuple { elements: Vec::with_capacity(len), error: PhantomData, }) } fn serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result { Ok(SerializeTupleStruct { name, fields: Vec::with_capacity(len), error: PhantomData, }) } fn serialize_tuple_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result { Ok(SerializeTupleVariant { name, variant_index, variant, fields: Vec::with_capacity(len), error: PhantomData, }) } fn serialize_map(self, len: Option) -> Result { Ok(SerializeMap { entries: Vec::with_capacity(len.unwrap_or(0)), key: None, error: PhantomData, }) } fn serialize_struct( self, name: &'static str, len: usize, ) -> Result { Ok(SerializeStruct { name, fields: Vec::with_capacity(len), error: PhantomData, }) } fn serialize_struct_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result { Ok(SerializeStructVariant { name, variant_index, variant, fields: Vec::with_capacity(len), error: PhantomData, }) } } pub struct SerializeSeq { elements: Vec, error: PhantomData, } impl ser::SerializeSeq for SerializeSeq where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_element(&mut self, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.elements.push(value); Ok(()) } fn end(self) -> Result { Ok(Content::Seq(self.elements)) } } pub struct SerializeTuple { elements: Vec, error: PhantomData, } impl ser::SerializeTuple for SerializeTuple where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_element(&mut self, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.elements.push(value); Ok(()) } fn end(self) -> Result { Ok(Content::Tuple(self.elements)) } } pub struct SerializeTupleStruct { name: &'static str, fields: Vec, error: PhantomData, } impl ser::SerializeTupleStruct for SerializeTupleStruct where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_field(&mut self, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push(value); Ok(()) } fn end(self) -> Result { Ok(Content::TupleStruct(self.name, self.fields)) } } pub struct SerializeTupleVariant { name: &'static str, variant_index: u32, variant: &'static str, fields: Vec, error: PhantomData, } impl ser::SerializeTupleVariant for SerializeTupleVariant where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_field(&mut self, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push(value); Ok(()) } fn end(self) -> Result { Ok(Content::TupleVariant( self.name, self.variant_index, self.variant, self.fields, )) } } pub struct SerializeMap { entries: Vec<(Content, Content)>, key: Option, error: PhantomData, } impl ser::SerializeMap for SerializeMap where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_key(&mut self, key: &T) -> Result<(), E> where T: ?Sized + Serialize, { let key = tri!(key.serialize(ContentSerializer::::new())); self.key = Some(key); Ok(()) } fn serialize_value(&mut self, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let key = self .key .take() .expect("serialize_value called before serialize_key"); let value = tri!(value.serialize(ContentSerializer::::new())); self.entries.push((key, value)); Ok(()) } fn end(self) -> Result { Ok(Content::Map(self.entries)) } fn serialize_entry(&mut self, key: &K, value: &V) -> Result<(), E> where K: ?Sized + Serialize, V: ?Sized + Serialize, { let key = tri!(key.serialize(ContentSerializer::::new())); let value = tri!(value.serialize(ContentSerializer::::new())); self.entries.push((key, value)); Ok(()) } } pub struct SerializeStruct { name: &'static str, fields: Vec<(&'static str, Content)>, error: PhantomData, } impl ser::SerializeStruct for SerializeStruct where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push((key, value)); Ok(()) } fn end(self) -> Result { Ok(Content::Struct(self.name, self.fields)) } } pub struct SerializeStructVariant { name: &'static str, variant_index: u32, variant: &'static str, fields: Vec<(&'static str, Content)>, error: PhantomData, } impl ser::SerializeStructVariant for SerializeStructVariant where E: ser::Error, { type Ok = Content; type Error = E; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), E> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push((key, value)); Ok(()) } fn end(self) -> Result { Ok(Content::StructVariant( self.name, self.variant_index, self.variant, self.fields, )) } } } #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> FlatMapSerializer<'a, M> where M: SerializeMap + 'a, { fn bad_type(what: Unsupported) -> M::Error { ser::Error::custom(format_args!( "can only flatten structs and maps (got {})", what )) } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> Serializer for FlatMapSerializer<'a, M> where M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; type SerializeSeq = Impossible; type SerializeTuple = Impossible; type SerializeTupleStruct = Impossible; type SerializeMap = FlatMapSerializeMap<'a, M>; type SerializeStruct = FlatMapSerializeStruct<'a, M>; type SerializeTupleVariant = FlatMapSerializeTupleVariantAsMapValue<'a, M>; type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>; fn serialize_bool(self, _: bool) -> Result { Err(Self::bad_type(Unsupported::Boolean)) } fn serialize_i8(self, _: i8) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_i16(self, _: i16) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_i32(self, _: i32) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_i64(self, _: i64) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_u8(self, _: u8) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_u16(self, _: u16) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_u32(self, _: u32) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_u64(self, _: u64) -> Result { Err(Self::bad_type(Unsupported::Integer)) } fn serialize_f32(self, _: f32) -> Result { Err(Self::bad_type(Unsupported::Float)) } fn serialize_f64(self, _: f64) -> Result { Err(Self::bad_type(Unsupported::Float)) } fn serialize_char(self, _: char) -> Result { Err(Self::bad_type(Unsupported::Char)) } fn serialize_str(self, _: &str) -> Result { Err(Self::bad_type(Unsupported::String)) } fn serialize_bytes(self, _: &[u8]) -> Result { Err(Self::bad_type(Unsupported::ByteArray)) } fn serialize_none(self) -> Result { Ok(()) } fn serialize_some(self, value: &T) -> Result where T: ?Sized + Serialize, { value.serialize(self) } fn serialize_unit(self) -> Result { Ok(()) } fn serialize_unit_struct(self, _: &'static str) -> Result { Ok(()) } fn serialize_unit_variant( self, _: &'static str, _: u32, variant: &'static str, ) -> Result { self.0.serialize_entry(variant, &()) } fn serialize_newtype_struct( self, _: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize, { value.serialize(self) } fn serialize_newtype_variant( self, _: &'static str, _: u32, variant: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize, { self.0.serialize_entry(variant, value) } fn serialize_seq(self, _: Option) -> Result { Err(Self::bad_type(Unsupported::Sequence)) } fn serialize_tuple(self, _: usize) -> Result { Err(Self::bad_type(Unsupported::Tuple)) } fn serialize_tuple_struct( self, _: &'static str, _: usize, ) -> Result { Err(Self::bad_type(Unsupported::TupleStruct)) } fn serialize_tuple_variant( self, _: &'static str, _: u32, variant: &'static str, _: usize, ) -> Result { tri!(self.0.serialize_key(variant)); Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0)) } fn serialize_map(self, _: Option) -> Result { Ok(FlatMapSerializeMap(self.0)) } fn serialize_struct( self, _: &'static str, _: usize, ) -> Result { Ok(FlatMapSerializeStruct(self.0)) } fn serialize_struct_variant( self, _: &'static str, _: u32, inner_variant: &'static str, _: usize, ) -> Result { tri!(self.0.serialize_key(inner_variant)); Ok(FlatMapSerializeStructVariantAsMapValue::new( self.0, inner_variant, )) } } #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> where M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { self.0.serialize_key(key) } fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { self.0.serialize_value(value) } fn serialize_entry(&mut self, key: &K, value: &V) -> Result<(), Self::Error> where K: ?Sized + Serialize, V: ?Sized + Serialize, { self.0.serialize_entry(key, value) } fn end(self) -> Result<(), Self::Error> { Ok(()) } } #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M); #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> where M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { self.0.serialize_entry(key, value) } fn end(self) -> Result<(), Self::Error> { Ok(()) } } //////////////////////////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] pub struct FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> { map: &'a mut M, fields: Vec, } #[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(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::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, name: &'static str, fields: Vec<(&'static str, Content)>, } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> where M: SerializeMap + 'a, { fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> { FlatMapSerializeStructVariantAsMapValue { map, name, fields: Vec::new(), } } } #[cfg(any(feature = "std", feature = "alloc"))] impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> where M: SerializeMap + 'a, { type Ok = (); type Error = M::Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize, { let value = tri!(value.serialize(ContentSerializer::::new())); self.fields.push((key, value)); Ok(()) } fn end(self) -> Result<(), Self::Error> { 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(&self, serializer: S) -> Result 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(pub T); impl Display for CannotSerializeVariant where T: Debug, { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "enum variant cannot be serialized: {:?}", self.0) } } serde-1.0.217/src/ser/fmt.rs000064400000000000000000000101061046102023000136100ustar 00000000000000use crate::lib::*; use crate::ser::{Error, Impossible, Serialize, Serializer}; impl Error for fmt::Error { fn custom(_msg: T) -> Self { fmt::Error } } macro_rules! fmt_primitives { ($($f:ident: $t:ty,)*) => { $( fn $f(self, v: $t) -> fmt::Result { Display::fmt(&v, self) } )* }; } /// ```edition2021 /// use serde::ser::Serialize; /// use serde_derive::Serialize; /// use std::fmt::{self, Display}; /// /// #[derive(Serialize)] /// #[serde(rename_all = "kebab-case")] /// pub enum MessageType { /// StartRequest, /// EndRequest, /// } /// /// impl Display for MessageType { /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// self.serialize(f) /// } /// } /// ``` impl<'a> Serializer for &mut fmt::Formatter<'a> { type Ok = (); type Error = fmt::Error; type SerializeSeq = Impossible<(), fmt::Error>; type SerializeTuple = Impossible<(), fmt::Error>; type SerializeTupleStruct = Impossible<(), fmt::Error>; type SerializeTupleVariant = Impossible<(), fmt::Error>; type SerializeMap = Impossible<(), fmt::Error>; type SerializeStruct = Impossible<(), fmt::Error>; type SerializeStructVariant = Impossible<(), fmt::Error>; fmt_primitives! { serialize_bool: bool, serialize_i8: i8, serialize_i16: i16, serialize_i32: i32, serialize_i64: i64, serialize_i128: i128, serialize_u8: u8, serialize_u16: u16, serialize_u32: u32, serialize_u64: u64, serialize_u128: u128, serialize_f32: f32, serialize_f64: f64, serialize_char: char, serialize_str: &str, serialize_unit_struct: &'static str, } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> fmt::Result { Display::fmt(variant, self) } fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> fmt::Result where T: ?Sized + Serialize, { Serialize::serialize(value, self) } fn serialize_bytes(self, _v: &[u8]) -> fmt::Result { Err(fmt::Error) } fn serialize_none(self) -> fmt::Result { Err(fmt::Error) } fn serialize_some(self, _value: &T) -> fmt::Result where T: ?Sized + Serialize, { Err(fmt::Error) } fn serialize_unit(self) -> fmt::Result { Err(fmt::Error) } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> fmt::Result where T: ?Sized + Serialize, { Err(fmt::Error) } fn serialize_seq(self, _len: Option) -> Result { Err(fmt::Error) } fn serialize_tuple(self, _len: usize) -> Result { Err(fmt::Error) } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(fmt::Error) } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(fmt::Error) } fn serialize_map(self, _len: Option) -> Result { Err(fmt::Error) } fn serialize_struct( self, _name: &'static str, _len: usize, ) -> Result { Err(fmt::Error) } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { Err(fmt::Error) } fn collect_str(self, value: &T) -> fmt::Result where T: ?Sized + Display, { Display::fmt(value, self) } } serde-1.0.217/src/ser/impls.rs000064400000000000000000000743721046102023000141650ustar 00000000000000use crate::lib::*; use crate::ser::{Error, Serialize, SerializeTuple, Serializer}; //////////////////////////////////////////////////////////////////////////////// macro_rules! primitive_impl { ($ty:ident, $method:ident $($cast:tt)*) => { impl Serialize for $ty { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.$method(*self $($cast)*) } } } } primitive_impl!(bool, serialize_bool); primitive_impl!(isize, serialize_i64 as i64); primitive_impl!(i8, serialize_i8); primitive_impl!(i16, serialize_i16); primitive_impl!(i32, serialize_i32); primitive_impl!(i64, serialize_i64); primitive_impl!(i128, serialize_i128); primitive_impl!(usize, serialize_u64 as u64); primitive_impl!(u8, serialize_u8); primitive_impl!(u16, serialize_u16); primitive_impl!(u32, serialize_u32); primitive_impl!(u64, serialize_u64); primitive_impl!(u128, serialize_u128); primitive_impl!(f32, serialize_f32); primitive_impl!(f64, serialize_f64); primitive_impl!(char, serialize_char); //////////////////////////////////////////////////////////////////////////////// impl Serialize for str { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(self) } } #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl Serialize for String { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(self) } } impl<'a> Serialize for fmt::Arguments<'a> { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_str(self) } } //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", not(no_core_cstr)))] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for CStr { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bytes(self.to_bytes()) } } #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] impl Serialize for CString { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bytes(self.to_bytes()) } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Option where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { Some(ref value) => serializer.serialize_some(value), None => serializer.serialize_none(), } } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for PhantomData where T: ?Sized, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_unit_struct("PhantomData") } } //////////////////////////////////////////////////////////////////////////////// // Does not require T: Serialize. impl Serialize for [T; 0] { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { tri!(serializer.serialize_tuple(0)).end() } } macro_rules! array_impls { ($($len:tt)+) => { $( impl Serialize for [T; $len] where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = tri!(serializer.serialize_tuple($len)); for e in self { tri!(seq.serialize_element(e)); } seq.end() } } )+ } } array_impls! { 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 } //////////////////////////////////////////////////////////////////////////////// impl Serialize for [T] where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_seq(self) } } #[cfg(not(no_relaxed_trait_bounds))] macro_rules! seq_impl { ( $(#[$attr:meta])* $ty:ident ) => { $(#[$attr])* impl Serialize for $ty where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_seq(self) } } } } #[cfg(no_relaxed_trait_bounds)] macro_rules! seq_impl { ( $(#[$attr:meta])* $ty:ident ) => { $(#[$attr])* impl Serialize for $ty where T: Serialize $(+ $tbound1 $(+ $tbound2)*)*, $($typaram: $bound,)* { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_seq(self) } } } } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BinaryHeap } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BTreeSet } seq_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] HashSet } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] LinkedList } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] Vec } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] VecDeque } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Range where Idx: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; 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 Serialize for RangeFrom where Idx: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; let mut state = tri!(serializer.serialize_struct("RangeFrom", 1)); tri!(state.serialize_field("start", &self.start)); state.end() } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for RangeInclusive where Idx: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; 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 Serialize for RangeTo where Idx: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; let mut state = tri!(serializer.serialize_struct("RangeTo", 1)); tri!(state.serialize_field("end", &self.end)); state.end() } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Bound where T: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { Bound::Unbounded => serializer.serialize_unit_variant("Bound", 0, "Unbounded"), Bound::Included(ref value) => { serializer.serialize_newtype_variant("Bound", 1, "Included", value) } Bound::Excluded(ref value) => { serializer.serialize_newtype_variant("Bound", 2, "Excluded", value) } } } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for () { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_unit() } } #[cfg(feature = "unstable")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] impl Serialize for ! { fn serialize(&self, _serializer: S) -> Result where S: Serializer, { *self } } //////////////////////////////////////////////////////////////////////////////// macro_rules! tuple_impls { ($($len:expr => ($($n:tt $name:ident)+))+) => { $( #[cfg_attr(docsrs, doc(hidden))] impl<$($name),+> Serialize for ($($name,)+) where $($name: Serialize,)+ { tuple_impl_body!($len => ($($n)+)); } )+ }; } macro_rules! tuple_impl_body { ($len:expr => ($($n:tt)+)) => { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut tuple = tri!(serializer.serialize_tuple($len)); $( tri!(tuple.serialize_element(&self.$n)); )+ tuple.end() } }; } #[cfg_attr(docsrs, doc(fake_variadic))] #[cfg_attr( docsrs, doc = "This trait is implemented for tuples up to 16 items long." )] impl Serialize for (T,) where T: Serialize, { tuple_impl_body!(1 => (0)); } tuple_impls! { 2 => (0 T0 1 T1) 3 => (0 T0 1 T1 2 T2) 4 => (0 T0 1 T1 2 T2 3 T3) 5 => (0 T0 1 T1 2 T2 3 T3 4 T4) 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5) 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6) 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7) 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8) 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9) 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10) 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11) 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12) 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13) 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14) 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15) } //////////////////////////////////////////////////////////////////////////////// #[cfg(not(no_relaxed_trait_bounds))] macro_rules! map_impl { ( $(#[$attr:meta])* $ty:ident ) => { $(#[$attr])* impl Serialize for $ty where K: Serialize, V: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_map(self) } } } } #[cfg(no_relaxed_trait_bounds)] macro_rules! map_impl { ( $(#[$attr:meta])* $ty:ident ) => { $(#[$attr])* impl Serialize for $ty where K: Serialize $(+ $kbound1 $(+ $kbound2)*)*, V: Serialize, $($typaram: $bound,)* { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.collect_map(self) } } } } map_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] BTreeMap } map_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] HashMap } //////////////////////////////////////////////////////////////////////////////// macro_rules! deref_impl { ( $(#[$attr:meta])* <$($desc:tt)+ ) => { $(#[$attr])* impl <$($desc)+ { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) } } }; } deref_impl! { <'a, T> Serialize for &'a T where T: ?Sized + Serialize } deref_impl! { <'a, T> Serialize for &'a mut T where T: ?Sized + Serialize } deref_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] Serialize for Box where T: ?Sized + Serialize } deref_impl! { /// This impl requires the [`"rc"`] Cargo feature of Serde. /// /// Serializing a data structure containing `Rc` will serialize a copy of /// the contents of the `Rc` each time the `Rc` is referenced within the /// data structure. Serialization will not attempt to deduplicate these /// repeated data. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] Serialize for Rc where T: ?Sized + Serialize } deref_impl! { /// This impl requires the [`"rc"`] Cargo feature of Serde. /// /// Serializing a data structure containing `Arc` will serialize a copy of /// the contents of the `Arc` each time the `Arc` is referenced within the /// data structure. Serialization will not attempt to deduplicate these /// repeated data. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))] Serialize for Arc where T: ?Sized + Serialize } deref_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] <'a, T> Serialize for Cow<'a, T> where T: ?Sized + Serialize + ToOwned } //////////////////////////////////////////////////////////////////////////////// /// This impl requires the [`"rc"`] Cargo feature of Serde. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr( docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) )] impl Serialize for RcWeak where T: ?Sized + Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.upgrade().serialize(serializer) } } /// This impl requires the [`"rc"`] Cargo feature of Serde. /// /// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc #[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))] #[cfg_attr( docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))) )] impl Serialize for ArcWeak where T: ?Sized + Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.upgrade().serialize(serializer) } } //////////////////////////////////////////////////////////////////////////////// macro_rules! nonzero_integers { ($($T:ident,)+) => { $( impl Serialize for num::$T { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.get().serialize(serializer) } } )+ } } nonzero_integers! { NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, } #[cfg(not(no_num_nonzero_signed))] nonzero_integers! { NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, } impl Serialize for Cell where T: Serialize + Copy, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.get().serialize(serializer) } } impl Serialize for RefCell where T: ?Sized + Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.try_borrow() { Ok(value) => value.serialize(serializer), Err(_) => Err(S::Error::custom("already mutably borrowed")), } } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for Mutex where T: ?Sized + Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.lock() { Ok(locked) => locked.serialize(serializer), Err(_) => Err(S::Error::custom("lock poison error while serializing")), } } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for RwLock where T: ?Sized + Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.read() { Ok(locked) => locked.serialize(serializer), Err(_) => Err(S::Error::custom("lock poison error while serializing")), } } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Result where T: Serialize, E: Serialize, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { Result::Ok(ref value) => serializer.serialize_newtype_variant("Result", 0, "Ok", value), Result::Err(ref value) => { serializer.serialize_newtype_variant("Result", 1, "Err", value) } } } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Duration { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; 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() } } //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for SystemTime { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use super::SerializeStruct; let duration_since_epoch = match self.duration_since(UNIX_EPOCH) { Ok(duration_since_epoch) => duration_since_epoch, Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")), }; 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() } } //////////////////////////////////////////////////////////////////////////////// /// Serialize a value that implements `Display` as a string, when that string is /// statically known to never have more than a constant `MAX_LEN` bytes. /// /// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes. #[cfg(any(feature = "std", not(no_core_net)))] macro_rules! serialize_display_bounded_length { ($value:expr, $max:expr, $serializer:expr) => {{ let mut buffer = [0u8; $max]; let mut writer = crate::format::Buf::new(&mut buffer); write!(&mut writer, "{}", $value).unwrap(); $serializer.serialize_str(writer.as_str()) }}; } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::IpAddr { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { match *self { net::IpAddr::V4(ref a) => a.serialize(serializer), net::IpAddr::V6(ref a) => a.serialize(serializer), } } else { match *self { net::IpAddr::V4(ref a) => { serializer.serialize_newtype_variant("IpAddr", 0, "V4", a) } net::IpAddr::V6(ref a) => { serializer.serialize_newtype_variant("IpAddr", 1, "V6", a) } } } } } #[cfg(any(feature = "std", not(no_core_net)))] const DEC_DIGITS_LUT: &[u8] = b"\ 0001020304050607080910111213141516171819\ 2021222324252627282930313233343536373839\ 4041424344454647484950515253545556575859\ 6061626364656667686970717273747576777879\ 8081828384858687888990919293949596979899"; #[cfg(any(feature = "std", not(no_core_net)))] #[inline] fn format_u8(mut n: u8, out: &mut [u8]) -> usize { if n >= 100 { let d1 = ((n % 100) << 1) as usize; n /= 100; out[0] = b'0' + n; out[1] = DEC_DIGITS_LUT[d1]; out[2] = DEC_DIGITS_LUT[d1 + 1]; 3 } else if n >= 10 { let d1 = (n << 1) as usize; out[0] = DEC_DIGITS_LUT[d1]; out[1] = DEC_DIGITS_LUT[d1 + 1]; 2 } else { out[0] = b'0' + n; 1 } } #[cfg(any(feature = "std", not(no_core_net)))] #[test] fn test_format_u8() { let mut i = 0u8; loop { let mut buf = [0u8; 3]; let written = format_u8(i, &mut buf); assert_eq!(i.to_string().as_bytes(), &buf[..written]); match i.checked_add(1) { Some(next) => i = next, None => break, } } } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::Ipv4Addr { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { const MAX_LEN: usize = 15; debug_assert_eq!(MAX_LEN, "101.102.103.104".len()); let mut buf = [b'.'; MAX_LEN]; let mut written = format_u8(self.octets()[0], &mut buf); for oct in &self.octets()[1..] { // Skip over delimiters that we initialized buf with written += format_u8(*oct, &mut buf[written + 1..]) + 1; } // Safety: We've only written ASCII bytes to the buffer, so it is valid UTF-8 let buf = unsafe { str::from_utf8_unchecked(&buf[..written]) }; serializer.serialize_str(buf) } else { self.octets().serialize(serializer) } } } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::Ipv6Addr { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { const MAX_LEN: usize = 39; debug_assert_eq!(MAX_LEN, "1001:1002:1003:1004:1005:1006:1007:1008".len()); serialize_display_bounded_length!(self, MAX_LEN, serializer) } else { self.octets().serialize(serializer) } } } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::SocketAddr { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { match *self { net::SocketAddr::V4(ref addr) => addr.serialize(serializer), net::SocketAddr::V6(ref addr) => addr.serialize(serializer), } } else { match *self { net::SocketAddr::V4(ref addr) => { serializer.serialize_newtype_variant("SocketAddr", 0, "V4", addr) } net::SocketAddr::V6(ref addr) => { serializer.serialize_newtype_variant("SocketAddr", 1, "V6", addr) } } } } } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::SocketAddrV4 { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { const MAX_LEN: usize = 21; debug_assert_eq!(MAX_LEN, "101.102.103.104:65000".len()); serialize_display_bounded_length!(self, MAX_LEN, serializer) } else { (self.ip(), self.port()).serialize(serializer) } } } #[cfg(any(feature = "std", not(no_core_net)))] impl Serialize for net::SocketAddrV6 { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if serializer.is_human_readable() { const MAX_LEN: usize = 58; debug_assert_eq!( MAX_LEN, "[1001:1002:1003:1004:1005:1006:1007:1008%4294967295]:65000".len() ); serialize_display_bounded_length!(self, MAX_LEN, serializer) } else { (self.ip(), self.port()).serialize(serializer) } } } //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for Path { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.to_str() { Some(s) => s.serialize(serializer), None => Err(Error::custom("path contains invalid UTF-8 characters")), } } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] impl Serialize for PathBuf { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.as_path().serialize(serializer) } } #[cfg(all(feature = "std", any(unix, windows)))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))] impl Serialize for OsStr { #[cfg(unix)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { use std::os::unix::ffi::OsStrExt; serializer.serialize_newtype_variant("OsString", 0, "Unix", self.as_bytes()) } #[cfg(windows)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { use std::os::windows::ffi::OsStrExt; let val = self.encode_wide().collect::>(); serializer.serialize_newtype_variant("OsString", 1, "Windows", &val) } } #[cfg(all(feature = "std", any(unix, windows)))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))] impl Serialize for OsString { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.as_os_str().serialize(serializer) } } //////////////////////////////////////////////////////////////////////////////// impl Serialize for Wrapping where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.0.serialize(serializer) } } #[cfg(not(no_core_num_saturating))] impl Serialize for Saturating where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.0.serialize(serializer) } } impl Serialize for Reverse where T: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.0.serialize(serializer) } } //////////////////////////////////////////////////////////////////////////////// #[cfg(all(feature = "std", not(no_std_atomic)))] macro_rules! atomic_impl { ($($ty:ident $size:expr)*) => { $( #[cfg(any(no_target_has_atomic, target_has_atomic = $size))] #[cfg_attr(docsrs, doc(cfg(all(feature = "std", target_has_atomic = $size))))] impl Serialize for $ty { fn serialize(&self, serializer: S) -> Result where S: Serializer, { // Matches the atomic ordering used in libcore for the Debug impl self.load(Ordering::Relaxed).serialize(serializer) } } )* } } #[cfg(all(feature = "std", not(no_std_atomic)))] atomic_impl! { AtomicBool "8" AtomicI8 "8" AtomicI16 "16" AtomicI32 "32" AtomicIsize "ptr" AtomicU8 "8" AtomicU16 "16" AtomicU32 "32" AtomicUsize "ptr" } #[cfg(all(feature = "std", not(no_std_atomic64)))] atomic_impl! { AtomicI64 "64" AtomicU64 "64" } serde-1.0.217/src/ser/impossible.rs000064400000000000000000000122571046102023000152010ustar 00000000000000//! This module contains `Impossible` serializer and its implementations. use crate::lib::*; use crate::ser::{ self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, SerializeTupleStruct, SerializeTupleVariant, }; /// Helper type for implementing a `Serializer` that does not support /// serializing one of the compound types. /// /// This type cannot be instantiated, but implements every one of the traits /// corresponding to the [`Serializer`] compound types: [`SerializeSeq`], /// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`], /// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`]. /// /// ```edition2021 /// # use serde::ser::{Serializer, Impossible}; /// # use serde::__private::doc::Error; /// # /// # struct MySerializer; /// # /// impl Serializer for MySerializer { /// type Ok = (); /// type Error = Error; /// /// type SerializeSeq = Impossible<(), Error>; /// /* other associated types */ /// /// /// This data format does not support serializing sequences. /// fn serialize_seq(self, /// len: Option) /// -> Result { /// // Given Impossible cannot be instantiated, the only /// // thing we can do here is to return an error. /// # stringify! { /// Err(...) /// # }; /// # unimplemented!() /// } /// /// /* other Serializer methods */ /// # serde::__serialize_unimplemented! { /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some /// # unit unit_struct unit_variant newtype_struct newtype_variant /// # tuple tuple_struct tuple_variant map struct struct_variant /// # } /// } /// ``` /// /// [`Serializer`]: trait.Serializer.html /// [`SerializeSeq`]: trait.SerializeSeq.html /// [`SerializeTuple`]: trait.SerializeTuple.html /// [`SerializeTupleStruct`]: trait.SerializeTupleStruct.html /// [`SerializeTupleVariant`]: trait.SerializeTupleVariant.html /// [`SerializeMap`]: trait.SerializeMap.html /// [`SerializeStruct`]: trait.SerializeStruct.html /// [`SerializeStructVariant`]: trait.SerializeStructVariant.html pub struct Impossible { void: Void, ok: PhantomData, error: PhantomData, } enum Void {} impl SerializeSeq for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeTuple for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeTupleStruct for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeTupleVariant for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeMap for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_key(&mut self, key: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = key; match self.void {} } fn serialize_value(&mut self, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeStruct for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = key; let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } impl SerializeStructVariant for Impossible where Error: ser::Error, { type Ok = Ok; type Error = Error; fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Error> where T: ?Sized + Serialize, { let _ = key; let _ = value; match self.void {} } fn end(self) -> Result { match self.void {} } } serde-1.0.217/src/ser/mod.rs000064400000000000000000001740331046102023000136130ustar 00000000000000//! Generic data structure serialization framework. //! //! The two most important traits in this module are [`Serialize`] and //! [`Serializer`]. //! //! - **A type that implements `Serialize` is a data structure** that can be //! serialized to any data format supported by Serde, and conversely //! - **A type that implements `Serializer` is a data format** that can //! serialize any data structure supported by Serde. //! //! # The Serialize trait //! //! Serde provides [`Serialize`] implementations for many Rust primitive and //! standard library types. The complete list is below. All of these can be //! serialized using Serde out of the box. //! //! Additionally, Serde provides a procedural macro called [`serde_derive`] to //! automatically generate [`Serialize`] implementations for structs and enums //! in your program. See the [derive section of the manual] for how to use this. //! //! In rare cases it may be necessary to implement [`Serialize`] manually for //! some type in your program. See the [Implementing `Serialize`] section of the //! manual for more about this. //! //! Third-party crates may provide [`Serialize`] implementations for types that //! they expose. For example the [`linked-hash-map`] crate provides a //! [`LinkedHashMap`] type that is serializable by Serde because the crate //! provides an implementation of [`Serialize`] for it. //! //! # The Serializer trait //! //! [`Serializer`] implementations are provided by third-party crates, for //! example [`serde_json`], [`serde_yaml`] and [`postcard`]. //! //! A partial list of well-maintained formats is given on the [Serde //! website][data formats]. //! //! # Implementations of Serialize provided by Serde //! //! - **Primitive types**: //! - bool //! - i8, i16, i32, i64, i128, isize //! - u8, u16, u32, u64, u128, usize //! - f32, f64 //! - char //! - str //! - &T and &mut T //! - **Compound types**: //! - \[T\] //! - \[T; 0\] through \[T; 32\] //! - tuples up to size 16 //! - **Common standard library types**: //! - String //! - Option\ //! - Result\ //! - PhantomData\ //! - **Wrapper types**: //! - Box\ //! - Cow\<'a, T\> //! - Cell\ //! - RefCell\ //! - Mutex\ //! - RwLock\ //! - Rc\ *(if* features = \["rc"\] *is enabled)* //! - Arc\ *(if* features = \["rc"\] *is enabled)* //! - **Collection types**: //! - BTreeMap\ //! - BTreeSet\ //! - BinaryHeap\ //! - HashMap\ //! - HashSet\ //! - LinkedList\ //! - VecDeque\ //! - Vec\ //! - **FFI types**: //! - CStr //! - CString //! - OsStr //! - OsString //! - **Miscellaneous standard library types**: //! - Duration //! - SystemTime //! - Path //! - PathBuf //! - Range\ //! - RangeInclusive\ //! - Bound\ //! - num::NonZero* //! - `!` *(unstable)* //! - **Net types**: //! - IpAddr //! - Ipv4Addr //! - Ipv6Addr //! - SocketAddr //! - SocketAddrV4 //! - SocketAddrV6 //! //! [Implementing `Serialize`]: https://serde.rs/impl-serialize.html //! [`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html //! [`Serialize`]: ../trait.Serialize.html //! [`Serializer`]: ../trait.Serializer.html //! [`postcard`]: https://github.com/jamesmunns/postcard //! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map //! [`serde_derive`]: https://crates.io/crates/serde_derive //! [`serde_json`]: https://github.com/serde-rs/json //! [`serde_yaml`]: https://github.com/dtolnay/serde-yaml //! [derive section of the manual]: https://serde.rs/derive.html //! [data formats]: https://serde.rs/#data-formats use crate::lib::*; mod fmt; mod impls; mod impossible; pub use self::impossible::Impossible; #[cfg(all(not(feature = "std"), no_core_error))] #[doc(no_inline)] pub use crate::std_error::Error as StdError; #[cfg(not(any(feature = "std", no_core_error)))] #[doc(no_inline)] pub use core::error::Error as StdError; #[cfg(feature = "std")] #[doc(no_inline)] pub use std::error::Error as StdError; //////////////////////////////////////////////////////////////////////////////// macro_rules! declare_error_trait { (Error: Sized $(+ $($supertrait:ident)::+)*) => { /// Trait used by `Serialize` implementations to generically construct /// errors belonging to the `Serializer` against which they are /// currently running. /// /// # Example implementation /// /// The [example data format] presented on the website shows an error /// type appropriate for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait Error: Sized $(+ $($supertrait)::+)* { /// Used when a [`Serialize`] implementation encounters any error /// while serializing a type. /// /// The message should not be capitalized and should not end with a /// period. /// /// For example, a filesystem [`Path`] may refuse to serialize /// itself if it contains invalid UTF-8 data. /// /// ```edition2021 /// # struct Path; /// # /// # impl Path { /// # fn to_str(&self) -> Option<&str> { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{self, Serialize, Serializer}; /// /// impl Serialize for Path { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match self.to_str() { /// Some(s) => serializer.serialize_str(s), /// None => Err(ser::Error::custom("path contains invalid UTF-8 characters")), /// } /// } /// } /// ``` /// /// [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html /// [`Serialize`]: ../trait.Serialize.html fn custom(msg: T) -> Self where T: Display; } } } #[cfg(feature = "std")] declare_error_trait!(Error: Sized + StdError); #[cfg(not(feature = "std"))] declare_error_trait!(Error: Sized + Debug + Display); //////////////////////////////////////////////////////////////////////////////// /// A **data structure** that can be serialized into any data format supported /// by Serde. /// /// Serde provides `Serialize` implementations for many Rust primitive and /// standard library types. The complete list is [here][crate::ser]. All of /// these can be serialized using Serde out of the box. /// /// Additionally, Serde provides a procedural macro called [`serde_derive`] to /// automatically generate `Serialize` implementations for structs and enums in /// your program. See the [derive section of the manual] for how to use this. /// /// In rare cases it may be necessary to implement `Serialize` manually for some /// type in your program. See the [Implementing `Serialize`] section of the /// manual for more about this. /// /// Third-party crates may provide `Serialize` implementations for types that /// they expose. For example the [`linked-hash-map`] crate provides a /// [`LinkedHashMap`] type that is serializable by Serde because the crate /// provides an implementation of `Serialize` for it. /// /// [Implementing `Serialize`]: https://serde.rs/impl-serialize.html /// [`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html /// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map /// [`serde_derive`]: https://crates.io/crates/serde_derive /// [derive section of the manual]: https://serde.rs/derive.html #[cfg_attr( not(no_diagnostic_namespace), diagnostic::on_unimplemented( note = "for local types consider adding `#[derive(serde::Serialize)]` to your `{Self}` type", note = "for types from other crates check whether the crate offers a `serde` feature flag", ) )] pub trait Serialize { /// Serialize this value into the given Serde serializer. /// /// See the [Implementing `Serialize`] section of the manual for more /// information about how to implement this method. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeStruct, Serializer}; /// /// struct Person { /// name: String, /// age: u8, /// phones: Vec, /// } /// /// // This is what #[derive(Serialize)] would generate. /// impl Serialize for Person { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut s = serializer.serialize_struct("Person", 3)?; /// s.serialize_field("name", &self.name)?; /// s.serialize_field("age", &self.age)?; /// s.serialize_field("phones", &self.phones)?; /// s.end() /// } /// } /// ``` /// /// [Implementing `Serialize`]: https://serde.rs/impl-serialize.html fn serialize(&self, serializer: S) -> Result where S: Serializer; } //////////////////////////////////////////////////////////////////////////////// /// A **data format** that can serialize any data structure supported by Serde. /// /// The role of this trait is to define the serialization half of the [Serde /// data model], which is a way to categorize every Rust data structure into one /// of 29 possible types. Each method of the `Serializer` trait corresponds to /// one of the types of the data model. /// /// Implementations of `Serialize` map themselves into this data model by /// invoking exactly one of the `Serializer` methods. /// /// The types that make up the Serde data model are: /// /// - **14 primitive types** /// - bool /// - i8, i16, i32, i64, i128 /// - u8, u16, u32, u64, u128 /// - f32, f64 /// - char /// - **string** /// - UTF-8 bytes with a length and no null terminator. /// - When serializing, all strings are handled equally. When deserializing, /// there are three flavors of strings: transient, owned, and borrowed. /// - **byte array** - \[u8\] /// - Similar to strings, during deserialization byte arrays can be /// transient, owned, or borrowed. /// - **option** /// - Either none or some value. /// - **unit** /// - The type of `()` in Rust. It represents an anonymous value containing /// no data. /// - **unit_struct** /// - For example `struct Unit` or `PhantomData`. It represents a named /// value containing no data. /// - **unit_variant** /// - For example the `E::A` and `E::B` in `enum E { A, B }`. /// - **newtype_struct** /// - For example `struct Millimeters(u8)`. /// - **newtype_variant** /// - For example the `E::N` in `enum E { N(u8) }`. /// - **seq** /// - A variably sized heterogeneous sequence of values, for example /// `Vec` or `HashSet`. When serializing, the length may or may not /// be known before iterating through all the data. When deserializing, /// the length is determined by looking at the serialized data. /// - **tuple** /// - A statically sized heterogeneous sequence of values for which the /// length will be known at deserialization time without looking at the /// serialized data, for example `(u8,)` or `(String, u64, Vec)` or /// `[u64; 10]`. /// - **tuple_struct** /// - A named tuple, for example `struct Rgb(u8, u8, u8)`. /// - **tuple_variant** /// - For example the `E::T` in `enum E { T(u8, u8) }`. /// - **map** /// - A heterogeneous key-value pairing, for example `BTreeMap`. /// - **struct** /// - A heterogeneous key-value pairing in which the keys are strings and /// will be known at deserialization time without looking at the /// serialized data, for example `struct S { r: u8, g: u8, b: u8 }`. /// - **struct_variant** /// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`. /// /// Many Serde serializers produce text or binary data as output, for example /// JSON or Postcard. This is not a requirement of the `Serializer` trait, and /// there are serializers that do not produce text or binary output. One example /// is the `serde_json::value::Serializer` (distinct from the main `serde_json` /// serializer) that produces a `serde_json::Value` data structure in memory as /// output. /// /// [Serde data model]: https://serde.rs/data-model.html /// /// # Example implementation /// /// The [example data format] presented on the website contains example code for /// a basic JSON `Serializer`. /// /// [example data format]: https://serde.rs/data-format.html pub trait Serializer: Sized { /// The output type produced by this `Serializer` during successful /// serialization. Most serializers that produce text or binary output /// should set `Ok = ()` and serialize into an [`io::Write`] or buffer /// contained within the `Serializer` instance. Serializers that build /// in-memory data structures may be simplified by using `Ok` to propagate /// the data structure around. /// /// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html type Ok; /// The error type when some error occurs during serialization. type Error: Error; /// Type returned from [`serialize_seq`] for serializing the content of the /// sequence. /// /// [`serialize_seq`]: #tymethod.serialize_seq type SerializeSeq: SerializeSeq; /// Type returned from [`serialize_tuple`] for serializing the content of /// the tuple. /// /// [`serialize_tuple`]: #tymethod.serialize_tuple type SerializeTuple: SerializeTuple; /// Type returned from [`serialize_tuple_struct`] for serializing the /// content of the tuple struct. /// /// [`serialize_tuple_struct`]: #tymethod.serialize_tuple_struct type SerializeTupleStruct: SerializeTupleStruct; /// Type returned from [`serialize_tuple_variant`] for serializing the /// content of the tuple variant. /// /// [`serialize_tuple_variant`]: #tymethod.serialize_tuple_variant type SerializeTupleVariant: SerializeTupleVariant; /// Type returned from [`serialize_map`] for serializing the content of the /// map. /// /// [`serialize_map`]: #tymethod.serialize_map type SerializeMap: SerializeMap; /// Type returned from [`serialize_struct`] for serializing the content of /// the struct. /// /// [`serialize_struct`]: #tymethod.serialize_struct type SerializeStruct: SerializeStruct; /// Type returned from [`serialize_struct_variant`] for serializing the /// content of the struct variant. /// /// [`serialize_struct_variant`]: #tymethod.serialize_struct_variant type SerializeStructVariant: SerializeStructVariant; /// Serialize a `bool` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for bool { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_bool(*self) /// } /// } /// ``` fn serialize_bool(self, v: bool) -> Result; /// Serialize an `i8` value. /// /// If the format does not differentiate between `i8` and `i64`, a /// reasonable implementation would be to cast the value to `i64` and /// forward to `serialize_i64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for i8 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_i8(*self) /// } /// } /// ``` fn serialize_i8(self, v: i8) -> Result; /// Serialize an `i16` value. /// /// If the format does not differentiate between `i16` and `i64`, a /// reasonable implementation would be to cast the value to `i64` and /// forward to `serialize_i64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for i16 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_i16(*self) /// } /// } /// ``` fn serialize_i16(self, v: i16) -> Result; /// Serialize an `i32` value. /// /// If the format does not differentiate between `i32` and `i64`, a /// reasonable implementation would be to cast the value to `i64` and /// forward to `serialize_i64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for i32 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_i32(*self) /// } /// } /// ``` fn serialize_i32(self, v: i32) -> Result; /// Serialize an `i64` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for i64 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_i64(*self) /// } /// } /// ``` fn serialize_i64(self, v: i64) -> Result; /// Serialize an `i128` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for i128 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_i128(*self) /// } /// } /// ``` /// /// The default behavior unconditionally returns an error. fn serialize_i128(self, v: i128) -> Result { let _ = v; Err(Error::custom("i128 is not supported")) } /// Serialize a `u8` value. /// /// If the format does not differentiate between `u8` and `u64`, a /// reasonable implementation would be to cast the value to `u64` and /// forward to `serialize_u64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for u8 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_u8(*self) /// } /// } /// ``` fn serialize_u8(self, v: u8) -> Result; /// Serialize a `u16` value. /// /// If the format does not differentiate between `u16` and `u64`, a /// reasonable implementation would be to cast the value to `u64` and /// forward to `serialize_u64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for u16 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_u16(*self) /// } /// } /// ``` fn serialize_u16(self, v: u16) -> Result; /// Serialize a `u32` value. /// /// If the format does not differentiate between `u32` and `u64`, a /// reasonable implementation would be to cast the value to `u64` and /// forward to `serialize_u64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for u32 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_u32(*self) /// } /// } /// ``` fn serialize_u32(self, v: u32) -> Result; /// Serialize a `u64` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for u64 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_u64(*self) /// } /// } /// ``` fn serialize_u64(self, v: u64) -> Result; /// Serialize a `u128` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for u128 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_u128(*self) /// } /// } /// ``` /// /// The default behavior unconditionally returns an error. fn serialize_u128(self, v: u128) -> Result { let _ = v; Err(Error::custom("u128 is not supported")) } /// Serialize an `f32` value. /// /// If the format does not differentiate between `f32` and `f64`, a /// reasonable implementation would be to cast the value to `f64` and /// forward to `serialize_f64`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for f32 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_f32(*self) /// } /// } /// ``` fn serialize_f32(self, v: f32) -> Result; /// Serialize an `f64` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for f64 { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_f64(*self) /// } /// } /// ``` fn serialize_f64(self, v: f64) -> Result; /// Serialize a character. /// /// If the format does not support characters, it is reasonable to serialize /// it as a single element `str` or a `u32`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for char { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_char(*self) /// } /// } /// ``` fn serialize_char(self, v: char) -> Result; /// Serialize a `&str`. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for str { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_str(self) /// } /// } /// ``` fn serialize_str(self, v: &str) -> Result; /// Serialize a chunk of raw byte data. /// /// Enables serializers to serialize byte slices more compactly or more /// efficiently than other types of slices. If no efficient implementation /// is available, a reasonable implementation would be to forward to /// `serialize_seq`. If forwarded, the implementation looks usually just /// like this: /// /// ```edition2021 /// # use serde::ser::{Serializer, SerializeSeq}; /// # use serde::__private::doc::Error; /// # /// # struct MySerializer; /// # /// # impl Serializer for MySerializer { /// # type Ok = (); /// # type Error = Error; /// # /// fn serialize_bytes(self, v: &[u8]) -> Result { /// let mut seq = self.serialize_seq(Some(v.len()))?; /// for b in v { /// seq.serialize_element(b)?; /// } /// seq.end() /// } /// # /// # serde::__serialize_unimplemented! { /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some /// # unit unit_struct unit_variant newtype_struct newtype_variant /// # seq tuple tuple_struct tuple_variant map struct struct_variant /// # } /// # } /// ``` fn serialize_bytes(self, v: &[u8]) -> Result; /// Serialize a [`None`] value. /// /// ```edition2021 /// # use serde::{Serialize, Serializer}; /// # /// # enum Option { /// # Some(T), /// # None, /// # } /// # /// # use self::Option::{Some, None}; /// # /// impl Serialize for Option /// where /// T: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// Some(ref value) => serializer.serialize_some(value), /// None => serializer.serialize_none(), /// } /// } /// } /// # /// # fn main() {} /// ``` /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None fn serialize_none(self) -> Result; /// Serialize a [`Some(T)`] value. /// /// ```edition2021 /// # use serde::{Serialize, Serializer}; /// # /// # enum Option { /// # Some(T), /// # None, /// # } /// # /// # use self::Option::{Some, None}; /// # /// impl Serialize for Option /// where /// T: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// Some(ref value) => serializer.serialize_some(value), /// None => serializer.serialize_none(), /// } /// } /// } /// # /// # fn main() {} /// ``` /// /// [`Some(T)`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some fn serialize_some(self, value: &T) -> Result where T: ?Sized + Serialize; /// Serialize a `()` value. /// /// ```edition2021 /// # use serde::Serializer; /// # /// # serde::__private_serialize!(); /// # /// impl Serialize for () { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_unit() /// } /// } /// ``` fn serialize_unit(self) -> Result; /// Serialize a unit struct like `struct Unit` or `PhantomData`. /// /// A reasonable implementation would be to forward to `serialize_unit`. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// /// struct Nothing; /// /// impl Serialize for Nothing { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_unit_struct("Nothing") /// } /// } /// ``` fn serialize_unit_struct(self, name: &'static str) -> Result; /// Serialize a unit variant like `E::A` in `enum E { A, B }`. /// /// The `name` is the name of the enum, the `variant_index` is the index of /// this variant within the enum, and the `variant` is the name of the /// variant. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// /// enum E { /// A, /// B, /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::A => serializer.serialize_unit_variant("E", 0, "A"), /// E::B => serializer.serialize_unit_variant("E", 1, "B"), /// } /// } /// } /// ``` fn serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result; /// Serialize a newtype struct like `struct Millimeters(u8)`. /// /// Serializers are encouraged to treat newtype structs as insignificant /// wrappers around the data they contain. A reasonable implementation would /// be to forward to `value.serialize(self)`. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// /// struct Millimeters(u8); /// /// impl Serialize for Millimeters { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.serialize_newtype_struct("Millimeters", &self.0) /// } /// } /// ``` fn serialize_newtype_struct( self, name: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize; /// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`. /// /// The `name` is the name of the enum, the `variant_index` is the index of /// this variant within the enum, and the `variant` is the name of the /// variant. The `value` is the data contained within this newtype variant. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// /// enum E { /// M(String), /// N(u8), /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::M(ref s) => serializer.serialize_newtype_variant("E", 0, "M", s), /// E::N(n) => serializer.serialize_newtype_variant("E", 1, "N", &n), /// } /// } /// } /// ``` fn serialize_newtype_variant( self, name: &'static str, variant_index: u32, variant: &'static str, value: &T, ) -> Result where T: ?Sized + Serialize; /// Begin to serialize a variably sized sequence. This call must be /// followed by zero or more calls to `serialize_element`, then a call to /// `end`. /// /// The argument is the number of elements in the sequence, which may or may /// not be computable before the sequence is iterated. Some serializers only /// support sequences whose length is known up front. /// /// ```edition2021 /// # use std::marker::PhantomData; /// # /// # struct Vec(PhantomData); /// # /// # impl Vec { /// # fn len(&self) -> usize { /// # unimplemented!() /// # } /// # } /// # /// # impl<'a, T> IntoIterator for &'a Vec { /// # type Item = &'a T; /// # type IntoIter = Box>; /// # /// # fn into_iter(self) -> Self::IntoIter { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{Serialize, SerializeSeq, Serializer}; /// /// impl Serialize for Vec /// where /// T: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut seq = serializer.serialize_seq(Some(self.len()))?; /// for element in self { /// seq.serialize_element(element)?; /// } /// seq.end() /// } /// } /// ``` fn serialize_seq(self, len: Option) -> Result; /// Begin to serialize a statically sized sequence whose length will be /// known at deserialization time without looking at the serialized data. /// This call must be followed by zero or more calls to `serialize_element`, /// then a call to `end`. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTuple, Serializer}; /// /// # mod fool { /// # trait Serialize {} /// impl Serialize for (A, B, C) /// # {} /// # } /// # /// # struct Tuple3(A, B, C); /// # /// # impl Serialize for Tuple3 /// where /// A: Serialize, /// B: Serialize, /// C: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut tup = serializer.serialize_tuple(3)?; /// tup.serialize_element(&self.0)?; /// tup.serialize_element(&self.1)?; /// tup.serialize_element(&self.2)?; /// tup.end() /// } /// } /// ``` /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTuple, Serializer}; /// /// const VRAM_SIZE: usize = 386; /// struct Vram([u16; VRAM_SIZE]); /// /// impl Serialize for Vram { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut seq = serializer.serialize_tuple(VRAM_SIZE)?; /// for element in &self.0[..] { /// seq.serialize_element(element)?; /// } /// seq.end() /// } /// } /// ``` fn serialize_tuple(self, len: usize) -> Result; /// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This /// call must be followed by zero or more calls to `serialize_field`, then a /// call to `end`. /// /// The `name` is the name of the tuple struct and the `len` is the number /// of data fields that will be serialized. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTupleStruct, Serializer}; /// /// struct Rgb(u8, u8, u8); /// /// impl Serialize for Rgb { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut ts = serializer.serialize_tuple_struct("Rgb", 3)?; /// ts.serialize_field(&self.0)?; /// ts.serialize_field(&self.1)?; /// ts.serialize_field(&self.2)?; /// ts.end() /// } /// } /// ``` fn serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result; /// Begin to serialize a tuple variant like `E::T` in `enum E { T(u8, u8) /// }`. This call must be followed by zero or more calls to /// `serialize_field`, then a call to `end`. /// /// The `name` is the name of the enum, the `variant_index` is the index of /// 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. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTupleVariant, Serializer}; /// /// enum E { /// T(u8, u8), /// U(String, u32, u32), /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::T(ref a, ref b) => { /// let mut tv = serializer.serialize_tuple_variant("E", 0, "T", 2)?; /// tv.serialize_field(a)?; /// tv.serialize_field(b)?; /// tv.end() /// } /// E::U(ref a, ref b, ref c) => { /// let mut tv = serializer.serialize_tuple_variant("E", 1, "U", 3)?; /// tv.serialize_field(a)?; /// tv.serialize_field(b)?; /// tv.serialize_field(c)?; /// tv.end() /// } /// } /// } /// } /// ``` fn serialize_tuple_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result; /// Begin to serialize a map. This call must be followed by zero or more /// calls to `serialize_key` and `serialize_value`, then a call to `end`. /// /// The argument is the number of elements in the map, which may or may not /// be computable before the map is iterated. Some serializers only support /// maps whose length is known up front. /// /// ```edition2021 /// # use std::marker::PhantomData; /// # /// # struct HashMap(PhantomData, PhantomData); /// # /// # impl HashMap { /// # fn len(&self) -> usize { /// # unimplemented!() /// # } /// # } /// # /// # impl<'a, K, V> IntoIterator for &'a HashMap { /// # type Item = (&'a K, &'a V); /// # type IntoIter = Box>; /// # /// # fn into_iter(self) -> Self::IntoIter { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{Serialize, SerializeMap, Serializer}; /// /// impl Serialize for HashMap /// where /// K: Serialize, /// V: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut map = serializer.serialize_map(Some(self.len()))?; /// for (k, v) in self { /// map.serialize_entry(k, v)?; /// } /// map.end() /// } /// } /// ``` fn serialize_map(self, len: Option) -> Result; /// Begin to serialize a struct like `struct Rgb { r: u8, g: u8, b: u8 }`. /// This call must be followed by zero or more calls to `serialize_field`, /// then a call to `end`. /// /// The `name` is the name of the struct and the `len` is the number of /// data fields that will be serialized. `len` does not include fields /// which are skipped with [`SerializeStruct::skip_field`]. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeStruct, Serializer}; /// /// struct Rgb { /// r: u8, /// g: u8, /// b: u8, /// } /// /// impl Serialize for Rgb { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut rgb = serializer.serialize_struct("Rgb", 3)?; /// rgb.serialize_field("r", &self.r)?; /// rgb.serialize_field("g", &self.g)?; /// rgb.serialize_field("b", &self.b)?; /// rgb.end() /// } /// } /// ``` fn serialize_struct( self, name: &'static str, len: usize, ) -> Result; /// Begin to serialize a struct variant like `E::S` in `enum E { S { r: u8, /// g: u8, b: u8 } }`. This call must be followed by zero or more calls to /// `serialize_field`, then a call to `end`. /// /// The `name` is the name of the enum, the `variant_index` is the index of /// 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. /// `len` does not include fields which are skipped with /// [`SerializeStructVariant::skip_field`]. /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeStructVariant, Serializer}; /// /// enum E { /// S { r: u8, g: u8, b: u8 }, /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::S { /// ref r, /// ref g, /// ref b, /// } => { /// let mut sv = serializer.serialize_struct_variant("E", 0, "S", 3)?; /// sv.serialize_field("r", r)?; /// sv.serialize_field("g", g)?; /// sv.serialize_field("b", b)?; /// sv.end() /// } /// } /// } /// } /// ``` fn serialize_struct_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result; /// Collect an iterator as a sequence. /// /// The default implementation serializes each item yielded by the iterator /// using [`serialize_seq`]. Implementors should not need to override this /// method. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// /// struct SecretlyOneHigher { /// data: Vec, /// } /// /// impl Serialize for SecretlyOneHigher { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.collect_seq(self.data.iter().map(|x| x + 1)) /// } /// } /// ``` /// /// [`serialize_seq`]: #tymethod.serialize_seq fn collect_seq(self, iter: I) -> Result where I: IntoIterator, ::Item: Serialize, { 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() } /// Collect an iterator as a map. /// /// The default implementation serializes each pair yielded by the iterator /// using [`serialize_map`]. Implementors should not need to override this /// method. /// /// ```edition2021 /// use serde::{Serialize, Serializer}; /// use std::collections::BTreeSet; /// /// struct MapToUnit { /// keys: BTreeSet, /// } /// /// // Serializes as a map in which the values are all unit. /// impl Serialize for MapToUnit { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.collect_map(self.keys.iter().map(|k| (k, ()))) /// } /// } /// ``` /// /// [`serialize_map`]: #tymethod.serialize_map fn collect_map(self, iter: I) -> Result where K: Serialize, V: Serialize, I: IntoIterator, { 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() } /// Serialize a string produced by an implementation of `Display`. /// /// The default implementation builds a heap-allocated [`String`] and /// delegates to [`serialize_str`]. Serializers are encouraged to provide a /// more efficient implementation if possible. /// /// ```edition2021 /// # struct DateTime; /// # /// # impl DateTime { /// # fn naive_local(&self) -> () { () } /// # fn offset(&self) -> () { () } /// # } /// # /// use serde::{Serialize, Serializer}; /// /// impl Serialize for DateTime { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset())) /// } /// } /// ``` /// /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html /// [`serialize_str`]: #tymethod.serialize_str #[cfg(any(feature = "std", feature = "alloc"))] fn collect_str(self, value: &T) -> Result where T: ?Sized + Display, { self.serialize_str(&value.to_string()) } /// Serialize a string produced by an implementation of `Display`. /// /// Serializers that use `no_std` are required to provide an implementation /// of this method. If no more sensible behavior is possible, the /// implementation is expected to return an error. /// /// ```edition2021 /// # struct DateTime; /// # /// # impl DateTime { /// # fn naive_local(&self) -> () { () } /// # fn offset(&self) -> () { () } /// # } /// # /// use serde::{Serialize, Serializer}; /// /// impl Serialize for DateTime { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset())) /// } /// } /// ``` #[cfg(not(any(feature = "std", feature = "alloc")))] fn collect_str(self, value: &T) -> Result where T: ?Sized + Display; /// Determine whether `Serialize` implementations should serialize in /// human-readable form. /// /// Some types have a human-readable form that may be somewhat expensive to /// construct, as well as a binary form that is compact and efficient. /// Generally text-based formats like JSON and YAML will prefer to use the /// human-readable one and binary formats like Postcard will prefer the /// compact one. /// /// ```edition2021 /// # use std::fmt::{self, Display}; /// # /// # struct Timestamp; /// # /// # impl Timestamp { /// # fn seconds_since_epoch(&self) -> u64 { unimplemented!() } /// # } /// # /// # impl Display for Timestamp { /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// # unimplemented!() /// # } /// # } /// # /// use serde::{Serialize, Serializer}; /// /// impl Serialize for Timestamp { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// if serializer.is_human_readable() { /// // Serialize to a human-readable string "2015-05-15T17:01:00Z". /// self.to_string().serialize(serializer) /// } else { /// // Serialize to a compact binary representation. /// self.seconds_since_epoch().serialize(serializer) /// } /// } /// } /// ``` /// /// The default implementation of this method returns `true`. Data formats /// may override this to `false` to request a compact form for types that /// support one. Note that modifying this method to change a format from /// human-readable to compact or vice versa should be regarded as a breaking /// change, as a value serialized in human-readable mode is not required to /// deserialize from the same data in compact mode. #[inline] fn is_human_readable(&self) -> bool { true } } /// Returned from `Serializer::serialize_seq`. /// /// # Example use /// /// ```edition2021 /// # use std::marker::PhantomData; /// # /// # struct Vec(PhantomData); /// # /// # impl Vec { /// # fn len(&self) -> usize { /// # unimplemented!() /// # } /// # } /// # /// # impl<'a, T> IntoIterator for &'a Vec { /// # type Item = &'a T; /// # type IntoIter = Box>; /// # fn into_iter(self) -> Self::IntoIter { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{Serialize, SerializeSeq, Serializer}; /// /// impl Serialize for Vec /// where /// T: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut seq = serializer.serialize_seq(Some(self.len()))?; /// for element in self { /// seq.serialize_element(element)?; /// } /// seq.end() /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeSeq` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeSeq { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a sequence element. fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Finish serializing a sequence. fn end(self) -> Result; } /// Returned from `Serializer::serialize_tuple`. /// /// # Example use /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTuple, Serializer}; /// /// # mod fool { /// # trait Serialize {} /// impl Serialize for (A, B, C) /// # {} /// # } /// # /// # struct Tuple3(A, B, C); /// # /// # impl Serialize for Tuple3 /// where /// A: Serialize, /// B: Serialize, /// C: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut tup = serializer.serialize_tuple(3)?; /// tup.serialize_element(&self.0)?; /// tup.serialize_element(&self.1)?; /// tup.serialize_element(&self.2)?; /// tup.end() /// } /// } /// ``` /// /// ```edition2021 /// # use std::marker::PhantomData; /// # /// # struct Array(PhantomData); /// # /// # impl Array { /// # fn len(&self) -> usize { /// # unimplemented!() /// # } /// # } /// # /// # impl<'a, T> IntoIterator for &'a Array { /// # type Item = &'a T; /// # type IntoIter = Box>; /// # fn into_iter(self) -> Self::IntoIter { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{Serialize, SerializeTuple, Serializer}; /// /// # mod fool { /// # trait Serialize {} /// impl Serialize for [T; 16] /// # {} /// # } /// # /// # impl Serialize for Array /// where /// T: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut seq = serializer.serialize_tuple(16)?; /// for element in self { /// seq.serialize_element(element)?; /// } /// seq.end() /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeTuple` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeTuple { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a tuple element. fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Finish serializing a tuple. fn end(self) -> Result; } /// Returned from `Serializer::serialize_tuple_struct`. /// /// # Example use /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTupleStruct, Serializer}; /// /// struct Rgb(u8, u8, u8); /// /// impl Serialize for Rgb { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut ts = serializer.serialize_tuple_struct("Rgb", 3)?; /// ts.serialize_field(&self.0)?; /// ts.serialize_field(&self.1)?; /// ts.serialize_field(&self.2)?; /// ts.end() /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeTupleStruct` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeTupleStruct { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a tuple struct field. fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Finish serializing a tuple struct. fn end(self) -> Result; } /// Returned from `Serializer::serialize_tuple_variant`. /// /// # Example use /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeTupleVariant, Serializer}; /// /// enum E { /// T(u8, u8), /// U(String, u32, u32), /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::T(ref a, ref b) => { /// let mut tv = serializer.serialize_tuple_variant("E", 0, "T", 2)?; /// tv.serialize_field(a)?; /// tv.serialize_field(b)?; /// tv.end() /// } /// E::U(ref a, ref b, ref c) => { /// let mut tv = serializer.serialize_tuple_variant("E", 1, "U", 3)?; /// tv.serialize_field(a)?; /// tv.serialize_field(b)?; /// tv.serialize_field(c)?; /// tv.end() /// } /// } /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeTupleVariant` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeTupleVariant { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a tuple variant field. fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Finish serializing a tuple variant. fn end(self) -> Result; } /// Returned from `Serializer::serialize_map`. /// /// # Example use /// /// ```edition2021 /// # use std::marker::PhantomData; /// # /// # struct HashMap(PhantomData, PhantomData); /// # /// # impl HashMap { /// # fn len(&self) -> usize { /// # unimplemented!() /// # } /// # } /// # /// # impl<'a, K, V> IntoIterator for &'a HashMap { /// # type Item = (&'a K, &'a V); /// # type IntoIter = Box>; /// # /// # fn into_iter(self) -> Self::IntoIter { /// # unimplemented!() /// # } /// # } /// # /// use serde::ser::{Serialize, SerializeMap, Serializer}; /// /// impl Serialize for HashMap /// where /// K: Serialize, /// V: Serialize, /// { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut map = serializer.serialize_map(Some(self.len()))?; /// for (k, v) in self { /// map.serialize_entry(k, v)?; /// } /// map.end() /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeMap` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeMap { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a map key. /// /// If possible, `Serialize` implementations are encouraged to use /// `serialize_entry` instead as it may be implemented more efficiently in /// some formats compared to a pair of calls to `serialize_key` and /// `serialize_value`. fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Serialize a map value. /// /// # Panics /// /// Calling `serialize_value` before `serialize_key` is incorrect and is /// allowed to panic or produce bogus results. fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Serialize a map entry consisting of a key and a value. /// /// Some [`Serialize`] types are not able to hold a key and value in memory /// at the same time so `SerializeMap` implementations are required to /// support [`serialize_key`] and [`serialize_value`] individually. The /// `serialize_entry` method allows serializers to optimize for the case /// where key and value are both available. [`Serialize`] implementations /// are encouraged to use `serialize_entry` if possible. /// /// The default implementation delegates to [`serialize_key`] and /// [`serialize_value`]. This is appropriate for serializers that do not /// care about performance or are not able to optimize `serialize_entry` any /// better than this. /// /// [`Serialize`]: ../trait.Serialize.html /// [`serialize_key`]: #tymethod.serialize_key /// [`serialize_value`]: #tymethod.serialize_value fn serialize_entry(&mut self, key: &K, value: &V) -> Result<(), Self::Error> where K: ?Sized + Serialize, V: ?Sized + Serialize, { tri!(self.serialize_key(key)); self.serialize_value(value) } /// Finish serializing a map. fn end(self) -> Result; } /// Returned from `Serializer::serialize_struct`. /// /// # Example use /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeStruct, Serializer}; /// /// struct Rgb { /// r: u8, /// g: u8, /// b: u8, /// } /// /// impl Serialize for Rgb { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// let mut rgb = serializer.serialize_struct("Rgb", 3)?; /// rgb.serialize_field("r", &self.r)?; /// rgb.serialize_field("g", &self.g)?; /// rgb.serialize_field("b", &self.b)?; /// rgb.end() /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeStruct` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeStruct { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a struct field. fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Indicate that a struct field has been skipped. /// /// The default implementation does nothing. #[inline] fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> { let _ = key; Ok(()) } /// Finish serializing a struct. fn end(self) -> Result; } /// Returned from `Serializer::serialize_struct_variant`. /// /// # Example use /// /// ```edition2021 /// use serde::ser::{Serialize, SerializeStructVariant, Serializer}; /// /// enum E { /// S { r: u8, g: u8, b: u8 }, /// } /// /// impl Serialize for E { /// fn serialize(&self, serializer: S) -> Result /// where /// S: Serializer, /// { /// match *self { /// E::S { /// ref r, /// ref g, /// ref b, /// } => { /// let mut sv = serializer.serialize_struct_variant("E", 0, "S", 3)?; /// sv.serialize_field("r", r)?; /// sv.serialize_field("g", g)?; /// sv.serialize_field("b", b)?; /// sv.end() /// } /// } /// } /// } /// ``` /// /// # Example implementation /// /// The [example data format] presented on the website demonstrates an /// implementation of `SerializeStructVariant` for a basic JSON data format. /// /// [example data format]: https://serde.rs/data-format.html pub trait SerializeStructVariant { /// Must match the `Ok` type of our `Serializer`. type Ok; /// Must match the `Error` type of our `Serializer`. type Error: Error; /// Serialize a struct variant field. fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: ?Sized + Serialize; /// Indicate that a struct variant field has been skipped. /// /// The default implementation does nothing. #[inline] fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> { let _ = key; Ok(()) } /// Finish serializing a struct variant. fn end(self) -> Result; } fn iterator_len_hint(iter: &I) -> Option where I: Iterator, { match iter.size_hint() { (lo, Some(hi)) if lo == hi => Some(lo), _ => None, } } serde-1.0.217/src/std_error.rs000064400000000000000000000025031046102023000142360ustar 00000000000000use 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. /// /// Serde's error traits [`serde::ser::Error`] and [`serde::de::Error`] require /// [`std::error::Error`] as a supertrait, but only when Serde is built with /// "std" enabled. Data formats that don't care about no\_std support should /// generally provide their error types with a `std::error::Error` impl /// directly: /// /// ```edition2021 /// #[derive(Debug)] /// struct MySerError {...} /// /// impl serde::ser::Error for MySerError {...} /// /// impl std::fmt::Display for MySerError {...} /// /// // We don't support no_std! /// impl std::error::Error for MySerError {} /// ``` /// /// Data formats that *do* support no\_std may either have a "std" feature of /// their own: /// /// ```toml /// [features] /// std = ["serde/std"] /// ``` /// /// ```edition2021 /// #[cfg(feature = "std")] /// impl std::error::Error for MySerError {} /// ``` /// /// ... or else provide the std Error impl unconditionally via Serde's /// re-export: /// /// ```edition2021 /// impl serde::ser::StdError for MySerError {} /// ``` pub trait Error: Debug + Display { /// The underlying cause of this error, if any. fn source(&self) -> Option<&(Error + 'static)> { None } }