mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-27 14:37:55 +00:00
Compare commits
119 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dcbc3e0162 | |||
| 0289d31724 | |||
| 015e39776f | |||
| 6a9a21f178 | |||
| 81ac54b20d | |||
| 6b4e75520a | |||
| b053b4f492 | |||
| c0ba323166 | |||
| 20a48c9580 | |||
| 09938803af | |||
| 6d0b43a220 | |||
| 8a4dfa7231 | |||
| 107018c628 | |||
| a398237930 | |||
| b63c65d7f5 | |||
| f60324e883 | |||
| 361c23a09a | |||
| 43b23c7ea0 | |||
| 6081497506 | |||
| 48e5753e76 | |||
| bbba632ab3 | |||
| e77db40b8d | |||
| 2c1f62d4b4 | |||
| 1aebdc2760 | |||
| 705e58be8c | |||
| 7c2c12aa43 | |||
| a0f850f15b | |||
| fccb9499bc | |||
| a139ab2572 | |||
| 1d910a484c | |||
| ee9166ec97 | |||
| b5a9eff32e | |||
| 9441a29663 | |||
| ab6588ef74 | |||
| 1d11f03449 | |||
| e11d01fe1d | |||
| a901f50850 | |||
| c399e9c368 | |||
| 25381be0c9 | |||
| ef2a7c753f | |||
| 99f165b45a | |||
| 2fb5560746 | |||
| bd653ab30c | |||
| b5d68aedaa | |||
| 624879c4c6 | |||
| bd9e9abf35 | |||
| 3e4a23cbd0 | |||
| 6326ceec3f | |||
| 8f4d37c7ec | |||
| 1b8290b318 | |||
| 48193fbccd | |||
| ac8ea72d88 | |||
| f583401284 | |||
| 2d88228b7d | |||
| 0c6a2bbf79 | |||
| a80d830f27 | |||
| 5f3fd9994e | |||
| d6de911855 | |||
| 04af32230e | |||
| 4cb8d079f8 | |||
| 6ab55a1e52 | |||
| acfd19cb46 | |||
| e3058105f0 | |||
| dc200a6450 | |||
| 2c0999a0b9 | |||
| dd460f82a1 | |||
| c3d637f397 | |||
| 479a00a215 | |||
| c42e7c8012 | |||
| 5b8e0657d4 | |||
| 9fc0d13e2c | |||
| bc22641359 | |||
| 05098105a8 | |||
| 5b23634dc6 | |||
| 32f0d00ff9 | |||
| 9d87851f0c | |||
| c0296ee11b | |||
| 54671259aa | |||
| 994f7c7924 | |||
| 7a8e4977e2 | |||
| fb7b6ea7ea | |||
| 063dd5b93f | |||
| a38aa31ade | |||
| f42b2581da | |||
| 2ba406726f | |||
| 7e9826e17b | |||
| f4dcc5c918 | |||
| 8b1887c440 | |||
| bbfb1d3504 | |||
| e106feb5ec | |||
| 696f6f56db | |||
| b7b636a23f | |||
| 183b91775e | |||
| 0e70f59021 | |||
| 4d9b76db73 | |||
| 9af132f594 | |||
| 6c063569c0 | |||
| 7e9b98401d | |||
| f301e09e02 | |||
| b80e722f81 | |||
| 1714c262c4 | |||
| a42cdafdcd | |||
| eb4c3f16f7 | |||
| ce86f351d6 | |||
| 0b90f6c96a | |||
| 2198463218 | |||
| be57a5e00a | |||
| b1b09eba60 | |||
| eb1e8c140d | |||
| 43da87939d | |||
| 06d99a13a6 | |||
| 49a911d7de | |||
| 27d6628785 | |||
| e4e2956e79 | |||
| ea2f7b81d9 | |||
| f0dfdb5247 | |||
| a803ec1c1f | |||
| f7636428ed | |||
| bd4a0981ba |
@@ -1 +0,0 @@
|
|||||||
test_suite/tests/expand/*.expanded.rs linguist-generated
|
|
||||||
@@ -3,6 +3,7 @@ name: CI
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
schedule: [cron: "40 1 * * *"]
|
schedule: [cron: "40 1 * * *"]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
@@ -108,12 +109,12 @@ jobs:
|
|||||||
- run: cd serde && cargo build
|
- run: cd serde && cargo build
|
||||||
|
|
||||||
derive:
|
derive:
|
||||||
name: Rust 1.31.0
|
name: Rust 1.56.0
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: dtolnay/rust-toolchain@1.31.0
|
- uses: dtolnay/rust-toolchain@1.56.0
|
||||||
- run: cd serde && cargo check --no-default-features
|
- run: cd serde && cargo check --no-default-features
|
||||||
- run: cd serde && cargo check
|
- run: cd serde && cargo check
|
||||||
- run: cd serde_derive && cargo check
|
- run: cd serde_derive && cargo check
|
||||||
@@ -149,6 +150,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: dtolnay/rust-toolchain@miri
|
- uses: dtolnay/rust-toolchain@miri
|
||||||
|
- run: cargo miri setup
|
||||||
- run: cd serde && cargo miri test --features derive,rc,unstable
|
- run: cd serde && cargo miri test --features derive,rc,unstable
|
||||||
env:
|
env:
|
||||||
MIRIFLAGS: -Zmiri-strict-provenance
|
MIRIFLAGS: -Zmiri-strict-provenance
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.19+]][Rust 1.19] [![serde_derive: rustc 1.31+]][Rust 1.31]
|
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.19+]][Rust 1.19] [![serde_derive: rustc 1.56+]][Rust 1.56]
|
||||||
|
|
||||||
[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/serde/ci.yml?branch=master
|
[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
|
[actions]: https://github.com/serde-rs/serde/actions?query=branch%3Amaster
|
||||||
[Latest Version]: https://img.shields.io/crates/v/serde.svg
|
[Latest Version]: https://img.shields.io/crates/v/serde.svg
|
||||||
[crates.io]: https://crates.io/crates/serde
|
[crates.io]: https://crates.io/crates/serde
|
||||||
[serde: rustc 1.19+]: https://img.shields.io/badge/serde-rustc_1.19+-lightgray.svg
|
[serde: rustc 1.19+]: https://img.shields.io/badge/serde-rustc_1.19+-lightgray.svg
|
||||||
[serde_derive: rustc 1.31+]: https://img.shields.io/badge/serde_derive-rustc_1.31+-lightgray.svg
|
[serde_derive: rustc 1.56+]: https://img.shields.io/badge/serde_derive-rustc_1.56+-lightgray.svg
|
||||||
[Rust 1.19]: https://blog.rust-lang.org/2017/07/20/Rust-1.19.html
|
[Rust 1.19]: https://blog.rust-lang.org/2017/07/20/Rust-1.19.html
|
||||||
[Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
|
[Rust 1.56]: https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
|
||||||
|
|
||||||
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
|
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
msrv = "1.13.0"
|
|
||||||
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.154" # remember to update html_root_url and serde_derive dependency
|
version = "1.0.165" # remember to update html_root_url and serde_derive dependency
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["encoding", "no-std"]
|
categories = ["encoding", "no-std"]
|
||||||
@@ -15,7 +15,7 @@ repository = "https://github.com/serde-rs/serde"
|
|||||||
rust-version = "1.19"
|
rust-version = "1.19"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_derive = { version = "=1.0.154", optional = true, path = "../serde_derive" }
|
serde_derive = { version = "=1.0.165", optional = true, path = "../serde_derive" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_derive = { version = "1.0", path = "../serde_derive" }
|
serde_derive = { version = "1.0", path = "../serde_derive" }
|
||||||
@@ -27,6 +27,7 @@ doc-scrape-examples = false
|
|||||||
features = ["derive", "rc"]
|
features = ["derive", "rc"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
|
features = ["derive"]
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+11
-5
@@ -78,11 +78,6 @@ fn main() {
|
|||||||
println!("cargo:rustc-cfg=no_num_nonzero");
|
println!("cargo:rustc-cfg=no_num_nonzero");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current minimum supported version of serde_derive crate is Rust 1.31.
|
|
||||||
if minor < 31 {
|
|
||||||
println!("cargo:rustc-cfg=no_serde_derive");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
|
// TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
|
||||||
// stabilized in Rust 1.34:
|
// 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#tryfrom-and-tryinto
|
||||||
@@ -94,6 +89,11 @@ fn main() {
|
|||||||
println!("cargo:rustc-cfg=no_relaxed_trait_bounds");
|
println!("cargo:rustc-cfg=no_relaxed_trait_bounds");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Current minimum supported version of serde_derive crate is Rust 1.56.
|
||||||
|
if minor < 56 {
|
||||||
|
println!("cargo:rustc-cfg=no_serde_derive");
|
||||||
|
}
|
||||||
|
|
||||||
// Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60.
|
// Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60.
|
||||||
if minor < 60 {
|
if minor < 60 {
|
||||||
println!("cargo:rustc-cfg=no_target_has_atomic");
|
println!("cargo:rustc-cfg=no_target_has_atomic");
|
||||||
@@ -114,6 +114,12 @@ fn main() {
|
|||||||
println!("cargo:rustc-cfg=no_std_atomic");
|
println!("cargo:rustc-cfg=no_std_atomic");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rustc_minor_version() -> Option<u32> {
|
fn rustc_minor_version() -> Option<u32> {
|
||||||
|
|||||||
+21
-21
@@ -666,10 +666,10 @@ impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||||
struct CStringVisitor;
|
struct CStringVisitor;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||||
impl<'de> Visitor<'de> for CStringVisitor {
|
impl<'de> Visitor<'de> for CStringVisitor {
|
||||||
type Value = CString;
|
type Value = CString;
|
||||||
|
|
||||||
@@ -720,7 +720,7 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||||
impl<'de> Deserialize<'de> for CString {
|
impl<'de> Deserialize<'de> for CString {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
@@ -747,7 +747,10 @@ macro_rules! forwarded_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_de_boxed_c_str)))]
|
#[cfg(all(
|
||||||
|
any(feature = "std", all(not(no_core_cstr), feature = "alloc")),
|
||||||
|
not(no_de_boxed_c_str)
|
||||||
|
))]
|
||||||
forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
|
forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
|
||||||
|
|
||||||
#[cfg(not(no_core_reverse))]
|
#[cfg(not(no_core_reverse))]
|
||||||
@@ -991,7 +994,8 @@ seq_impl!(
|
|||||||
HashSet::clear,
|
HashSet::clear,
|
||||||
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
||||||
HashSet::reserve,
|
HashSet::reserve,
|
||||||
HashSet::insert);
|
HashSet::insert
|
||||||
|
);
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
seq_impl!(
|
seq_impl!(
|
||||||
@@ -1406,16 +1410,14 @@ macro_rules! map_impl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
map_impl!(
|
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
|
||||||
BTreeMap<K: Ord, V>,
|
|
||||||
map,
|
|
||||||
BTreeMap::new());
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
map_impl!(
|
map_impl!(
|
||||||
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
|
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
|
||||||
map,
|
map,
|
||||||
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default()));
|
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default())
|
||||||
|
);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -1585,7 +1587,7 @@ macro_rules! parse_socket_impl {
|
|||||||
if deserializer.is_human_readable() {
|
if deserializer.is_human_readable() {
|
||||||
deserializer.deserialize_str(FromStrVisitor::new($expecting))
|
deserializer.deserialize_str(FromStrVisitor::new($expecting))
|
||||||
} else {
|
} else {
|
||||||
<(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
|
<(_, u16)>::deserialize(deserializer).map($new)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1612,12 +1614,10 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, net::SocketAddrV4::new);
|
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, |(ip, port)| net::SocketAddrV4::new(ip, port));
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new(
|
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0));
|
||||||
ip, port, 0, 0
|
|
||||||
));
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -2096,7 +2096,7 @@ impl<'de> Deserialize<'de> for Duration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const FIELDS: &'static [&'static str] = &["secs", "nanos"];
|
const FIELDS: &[&str] = &["secs", "nanos"];
|
||||||
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
|
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2238,7 +2238,7 @@ impl<'de> Deserialize<'de> for SystemTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const FIELDS: &'static [&'static str] = &["secs_since_epoch", "nanos_since_epoch"];
|
const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"];
|
||||||
let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
|
let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
|
||||||
#[cfg(not(no_systemtime_checked_add))]
|
#[cfg(not(no_systemtime_checked_add))]
|
||||||
let ret = UNIX_EPOCH
|
let ret = UNIX_EPOCH
|
||||||
@@ -2306,7 +2306,7 @@ mod range {
|
|||||||
|
|
||||||
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
|
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
|
||||||
|
|
||||||
pub const FIELDS: &'static [&'static str] = &["start", "end"];
|
pub const FIELDS: &[&str] = &["start", "end"];
|
||||||
|
|
||||||
// If this were outside of the serde crate, it would just use:
|
// If this were outside of the serde crate, it would just use:
|
||||||
//
|
//
|
||||||
@@ -2532,7 +2532,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const VARIANTS: &'static [&'static str] = &["Unbounded", "Included", "Excluded"];
|
const VARIANTS: &[&str] = &["Unbounded", "Included", "Excluded"];
|
||||||
|
|
||||||
deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData))
|
deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData))
|
||||||
}
|
}
|
||||||
@@ -2640,7 +2640,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const VARIANTS: &'static [&'static str] = &["Ok", "Err"];
|
const VARIANTS: &[&str] = &["Ok", "Err"];
|
||||||
|
|
||||||
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
|
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
|
||||||
}
|
}
|
||||||
@@ -2706,7 +2706,7 @@ struct FromStrVisitor<T> {
|
|||||||
impl<T> FromStrVisitor<T> {
|
impl<T> FromStrVisitor<T> {
|
||||||
fn new(expecting: &'static str) -> Self {
|
fn new(expecting: &'static str) -> Self {
|
||||||
FromStrVisitor {
|
FromStrVisitor {
|
||||||
expecting: expecting,
|
expecting,
|
||||||
ty: PhantomData,
|
ty: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ pub fn encode(c: char) -> Encode {
|
|||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
Encode { buf: buf, pos: pos }
|
Encode { buf, pos }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Encode {
|
pub struct Encode {
|
||||||
|
|||||||
+14
-18
@@ -251,7 +251,7 @@ macro_rules! primitive_deserializer {
|
|||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub fn new(value: $ty) -> Self {
|
pub fn new(value: $ty) -> Self {
|
||||||
$name {
|
$name {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -330,7 +330,7 @@ impl<E> U32Deserializer<E> {
|
|||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub fn new(value: u32) -> Self {
|
pub fn new(value: u32) -> Self {
|
||||||
U32Deserializer {
|
U32Deserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -419,7 +419,7 @@ impl<'a, E> StrDeserializer<'a, E> {
|
|||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub fn new(value: &'a str) -> Self {
|
pub fn new(value: &'a str) -> Self {
|
||||||
StrDeserializer {
|
StrDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -498,7 +498,7 @@ impl<'de, E> BorrowedStrDeserializer<'de, E> {
|
|||||||
/// Create a new borrowed deserializer from the given string.
|
/// Create a new borrowed deserializer from the given string.
|
||||||
pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
|
pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
|
||||||
BorrowedStrDeserializer {
|
BorrowedStrDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -598,7 +598,7 @@ impl<E> StringDeserializer<E> {
|
|||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub fn new(value: String) -> Self {
|
pub fn new(value: String) -> Self {
|
||||||
StringDeserializer {
|
StringDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -701,7 +701,7 @@ impl<'a, E> CowStrDeserializer<'a, E> {
|
|||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
pub fn new(value: Cow<'a, str>) -> Self {
|
pub fn new(value: Cow<'a, str>) -> Self {
|
||||||
CowStrDeserializer {
|
CowStrDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -783,7 +783,7 @@ impl<'a, E> BytesDeserializer<'a, E> {
|
|||||||
/// Create a new deserializer from the given bytes.
|
/// Create a new deserializer from the given bytes.
|
||||||
pub fn new(value: &'a [u8]) -> Self {
|
pub fn new(value: &'a [u8]) -> Self {
|
||||||
BytesDeserializer {
|
BytesDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -842,7 +842,7 @@ impl<'de, E> BorrowedBytesDeserializer<'de, E> {
|
|||||||
/// Create a new borrowed deserializer from the given borrowed bytes.
|
/// Create a new borrowed deserializer from the given borrowed bytes.
|
||||||
pub fn new(value: &'de [u8]) -> Self {
|
pub fn new(value: &'de [u8]) -> Self {
|
||||||
BorrowedBytesDeserializer {
|
BorrowedBytesDeserializer {
|
||||||
value: value,
|
value,
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1053,7 +1053,7 @@ pub struct SeqAccessDeserializer<A> {
|
|||||||
impl<A> SeqAccessDeserializer<A> {
|
impl<A> SeqAccessDeserializer<A> {
|
||||||
/// Construct a new `SeqAccessDeserializer<A>`.
|
/// Construct a new `SeqAccessDeserializer<A>`.
|
||||||
pub fn new(seq: A) -> Self {
|
pub fn new(seq: A) -> Self {
|
||||||
SeqAccessDeserializer { seq: seq }
|
SeqAccessDeserializer { seq }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1454,7 +1454,7 @@ pub struct MapAccessDeserializer<A> {
|
|||||||
impl<A> MapAccessDeserializer<A> {
|
impl<A> MapAccessDeserializer<A> {
|
||||||
/// Construct a new `MapAccessDeserializer<A>`.
|
/// Construct a new `MapAccessDeserializer<A>`.
|
||||||
pub fn new(map: A) -> Self {
|
pub fn new(map: A) -> Self {
|
||||||
MapAccessDeserializer { map: map }
|
MapAccessDeserializer { map }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1519,7 +1519,7 @@ pub struct EnumAccessDeserializer<A> {
|
|||||||
impl<A> EnumAccessDeserializer<A> {
|
impl<A> EnumAccessDeserializer<A> {
|
||||||
/// Construct a new `EnumAccessDeserializer<A>`.
|
/// Construct a new `EnumAccessDeserializer<A>`.
|
||||||
pub fn new(access: A) -> Self {
|
pub fn new(access: A) -> Self {
|
||||||
EnumAccessDeserializer { access: access }
|
EnumAccessDeserializer { access }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1613,7 +1613,7 @@ mod private {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> {
|
pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> {
|
||||||
MapAsEnum { map: map }
|
MapAsEnum { map }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, A> VariantAccess<'de> for MapAsEnum<A>
|
impl<'de, A> VariantAccess<'de> for MapAsEnum<A>
|
||||||
@@ -1637,10 +1637,7 @@ mod private {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
self.map.next_value_seed(SeedTupleVariant {
|
self.map.next_value_seed(SeedTupleVariant { len, visitor })
|
||||||
len: len,
|
|
||||||
visitor: visitor,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_variant<V>(
|
fn struct_variant<V>(
|
||||||
@@ -1651,8 +1648,7 @@ mod private {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
self.map
|
self.map.next_value_seed(SeedStructVariant { visitor })
|
||||||
.next_value_seed(SeedStructVariant { visitor: visitor })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-4
@@ -93,7 +93,7 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Serde types in rustdoc of other crates get linked to here.
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.154")]
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.165")]
|
||||||
// Support using Serde without the standard library!
|
// Support using Serde without the standard library!
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
// Unstable functionality only if the user asks for it. For tracking and
|
// Unstable functionality only if the user asks for it. For tracking and
|
||||||
@@ -219,13 +219,23 @@ mod lib {
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||||
|
|
||||||
|
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
|
||||||
|
pub use 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(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::{error, net};
|
pub use std::{error, net};
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::collections::{HashMap, HashSet};
|
pub use std::collections::{HashMap, HashSet};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::ffi::{CStr, CString, OsStr, OsString};
|
pub use std::ffi::{OsStr, OsString};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::hash::{BuildHasher, Hash};
|
pub use std::hash::{BuildHasher, Hash};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@@ -328,9 +338,10 @@ mod std_error;
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
|
/// Derive macro available if serde is built with `features = ["derive"]`.
|
||||||
#[cfg(feature = "serde_derive")]
|
#[cfg(feature = "serde_derive")]
|
||||||
#[doc(hidden)]
|
pub use serde_derive::{Deserialize, Serialize};
|
||||||
pub use serde_derive::*;
|
|
||||||
|
|
||||||
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
||||||
mod actually_private {
|
mod actually_private {
|
||||||
|
|||||||
+70
-112
@@ -518,7 +518,7 @@ mod content {
|
|||||||
impl<'de> TagOrContentVisitor<'de> {
|
impl<'de> TagOrContentVisitor<'de> {
|
||||||
fn new(name: &'static str) -> Self {
|
fn new(name: &'static str) -> Self {
|
||||||
TagOrContentVisitor {
|
TagOrContentVisitor {
|
||||||
name: name,
|
name,
|
||||||
value: PhantomData,
|
value: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -815,7 +815,7 @@ mod content {
|
|||||||
pub fn new(name: &'static str, expecting: &'static str) -> Self {
|
pub fn new(name: &'static str, expecting: &'static str) -> Self {
|
||||||
TaggedContentVisitor {
|
TaggedContentVisitor {
|
||||||
tag_name: name,
|
tag_name: name,
|
||||||
expecting: expecting,
|
expecting,
|
||||||
value: PhantomData,
|
value: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -859,7 +859,7 @@ mod content {
|
|||||||
};
|
};
|
||||||
let rest = de::value::SeqAccessDeserializer::new(seq);
|
let rest = de::value::SeqAccessDeserializer::new(seq);
|
||||||
Ok(TaggedContent {
|
Ok(TaggedContent {
|
||||||
tag: tag,
|
tag,
|
||||||
content: try!(Content::deserialize(rest)),
|
content: try!(Content::deserialize(rest)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -887,7 +887,7 @@ mod content {
|
|||||||
match tag {
|
match tag {
|
||||||
None => Err(de::Error::missing_field(self.tag_name)),
|
None => Err(de::Error::missing_field(self.tag_name)),
|
||||||
Some(tag) => Ok(TaggedContent {
|
Some(tag) => Ok(TaggedContent {
|
||||||
tag: tag,
|
tag,
|
||||||
content: Content::Map(vec),
|
content: Content::Map(vec),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -982,9 +982,16 @@ mod content {
|
|||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
{
|
{
|
||||||
if field == self.tag {
|
self.visit_bytes(field.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
if field == self.tag.as_bytes() {
|
||||||
Ok(TagContentOtherField::Tag)
|
Ok(TagContentOtherField::Tag)
|
||||||
} else if field == self.content {
|
} else if field == self.content.as_bytes() {
|
||||||
Ok(TagContentOtherField::Content)
|
Ok(TagContentOtherField::Content)
|
||||||
} else {
|
} else {
|
||||||
Ok(TagContentOtherField::Other)
|
Ok(TagContentOtherField::Other)
|
||||||
@@ -1457,7 +1464,7 @@ mod content {
|
|||||||
/// private API, don't use
|
/// private API, don't use
|
||||||
pub fn new(content: Content<'de>) -> Self {
|
pub fn new(content: Content<'de>) -> Self {
|
||||||
ContentDeserializer {
|
ContentDeserializer {
|
||||||
content: content,
|
content,
|
||||||
err: PhantomData,
|
err: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1478,8 +1485,8 @@ mod content {
|
|||||||
{
|
{
|
||||||
pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
|
pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
|
||||||
EnumDeserializer {
|
EnumDeserializer {
|
||||||
variant: variant,
|
variant,
|
||||||
value: value,
|
value,
|
||||||
err: PhantomData,
|
err: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2135,8 +2142,8 @@ mod content {
|
|||||||
};
|
};
|
||||||
|
|
||||||
visitor.visit_enum(EnumRefDeserializer {
|
visitor.visit_enum(EnumRefDeserializer {
|
||||||
variant: variant,
|
variant,
|
||||||
value: value,
|
value,
|
||||||
err: PhantomData,
|
err: PhantomData,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -2180,12 +2187,20 @@ mod content {
|
|||||||
/// private API, don't use
|
/// private API, don't use
|
||||||
pub fn new(content: &'a Content<'de>) -> Self {
|
pub fn new(content: &'a Content<'de>) -> Self {
|
||||||
ContentRefDeserializer {
|
ContentRefDeserializer {
|
||||||
content: content,
|
content,
|
||||||
err: PhantomData,
|
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>
|
struct EnumRefDeserializer<'a, 'de: 'a, E>
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -2481,8 +2496,8 @@ mod content {
|
|||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
||||||
InternallyTaggedUnitVisitor {
|
InternallyTaggedUnitVisitor {
|
||||||
type_name: type_name,
|
type_name,
|
||||||
variant_name: variant_name,
|
variant_name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2526,8 +2541,8 @@ mod content {
|
|||||||
/// Not public API.
|
/// Not public API.
|
||||||
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
||||||
UntaggedUnitVisitor {
|
UntaggedUnitVisitor {
|
||||||
type_name: type_name,
|
type_name,
|
||||||
variant_name: variant_name,
|
variant_name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2731,11 +2746,7 @@ where
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
visitor.visit_map(FlatInternallyTaggedAccess {
|
self.deserialize_map(visitor)
|
||||||
iter: self.0.iter_mut(),
|
|
||||||
pending: None,
|
|
||||||
_marker: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(
|
fn deserialize_enum<V>(
|
||||||
@@ -2747,17 +2758,8 @@ where
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
for item in self.0.iter_mut() {
|
for entry in self.0 {
|
||||||
// items in the vector are nulled out when used. So we can only use
|
if let Some((key, value)) = flat_map_take_entry(entry, variants) {
|
||||||
// an item if it's still filled in and if the field is one we care
|
|
||||||
// about.
|
|
||||||
let use_item = match *item {
|
|
||||||
None => false,
|
|
||||||
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
|
|
||||||
};
|
|
||||||
|
|
||||||
if use_item {
|
|
||||||
let (key, value) = item.take().unwrap();
|
|
||||||
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
|
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2772,7 +2774,11 @@ where
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
visitor.visit_map(FlatMapAccess::new(self.0.iter()))
|
visitor.visit_map(FlatMapAccess {
|
||||||
|
iter: self.0.iter(),
|
||||||
|
pending_content: None,
|
||||||
|
_marker: PhantomData,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_struct<V>(
|
fn deserialize_struct<V>(
|
||||||
@@ -2784,7 +2790,12 @@ where
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
|
visitor.visit_map(FlatStructAccess {
|
||||||
|
iter: self.0.iter_mut(),
|
||||||
|
pending_content: None,
|
||||||
|
fields,
|
||||||
|
_marker: PhantomData,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
@@ -2838,25 +2849,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub struct FlatMapAccess<'a, 'de: 'a, E> {
|
struct FlatMapAccess<'a, 'de: 'a, E> {
|
||||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||||
pending_content: Option<&'a Content<'de>>,
|
pending_content: Option<&'a Content<'de>>,
|
||||||
_marker: PhantomData<E>,
|
_marker: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
|
|
||||||
fn new(
|
|
||||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
|
||||||
) -> FlatMapAccess<'a, 'de, E> {
|
|
||||||
FlatMapAccess {
|
|
||||||
iter: iter,
|
|
||||||
pending_content: None,
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
|
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
|
||||||
where
|
where
|
||||||
@@ -2871,6 +2869,10 @@ where
|
|||||||
for item in &mut self.iter {
|
for item in &mut self.iter {
|
||||||
// Items in the vector are nulled out when used by a struct.
|
// Items in the vector are nulled out when used by a struct.
|
||||||
if let Some((ref key, ref content)) = *item {
|
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);
|
self.pending_content = Some(content);
|
||||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
||||||
}
|
}
|
||||||
@@ -2890,28 +2892,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub struct FlatStructAccess<'a, 'de: 'a, E> {
|
struct FlatStructAccess<'a, 'de: 'a, E> {
|
||||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||||
pending_content: Option<Content<'de>>,
|
pending_content: Option<Content<'de>>,
|
||||||
fields: &'static [&'static str],
|
fields: &'static [&'static str],
|
||||||
_marker: PhantomData<E>,
|
_marker: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
|
|
||||||
fn new(
|
|
||||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
|
||||||
fields: &'static [&'static str],
|
|
||||||
) -> FlatStructAccess<'a, 'de, E> {
|
|
||||||
FlatStructAccess {
|
|
||||||
iter: iter,
|
|
||||||
pending_content: None,
|
|
||||||
fields: fields,
|
|
||||||
_marker: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
|
impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
|
||||||
where
|
where
|
||||||
@@ -2923,17 +2910,8 @@ where
|
|||||||
where
|
where
|
||||||
T: DeserializeSeed<'de>,
|
T: DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
while let Some(item) = self.iter.next() {
|
for entry in self.iter.by_ref() {
|
||||||
// items in the vector are nulled out when used. So we can only use
|
if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
|
||||||
// an item if it's still filled in and if the field is one we care
|
|
||||||
// about. In case we do not know which fields we want, we take them all.
|
|
||||||
let use_item = match *item {
|
|
||||||
None => false,
|
|
||||||
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
|
|
||||||
};
|
|
||||||
|
|
||||||
if use_item {
|
|
||||||
let (key, content) = item.take().unwrap();
|
|
||||||
self.pending_content = Some(content);
|
self.pending_content = Some(content);
|
||||||
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
|
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
|
||||||
}
|
}
|
||||||
@@ -2952,44 +2930,24 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
|
||||||
|
/// field name matches any of the recognized ones.
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
|
fn flat_map_take_entry<'de>(
|
||||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
entry: &mut Option<(Content<'de>, Content<'de>)>,
|
||||||
pending: Option<&'a Content<'de>>,
|
recognized: &[&str],
|
||||||
_marker: PhantomData<E>,
|
) -> 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)),
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
if is_recognized {
|
||||||
impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
|
entry.take()
|
||||||
where
|
} else {
|
||||||
E: Error,
|
None
|
||||||
{
|
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
|
||||||
where
|
|
||||||
T: DeserializeSeed<'de>,
|
|
||||||
{
|
|
||||||
for item in &mut self.iter {
|
|
||||||
if let Some((ref key, ref content)) = *item {
|
|
||||||
// Do not take(), instead borrow this entry. The internally tagged
|
|
||||||
// enum does its own buffering so we can't tell whether this entry
|
|
||||||
// is going to be consumed. Borrowing here leaves the entry
|
|
||||||
// available for later flattened fields.
|
|
||||||
self.pending = Some(content);
|
|
||||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
|
|
||||||
where
|
|
||||||
T: DeserializeSeed<'de>,
|
|
||||||
{
|
|
||||||
match self.pending.take() {
|
|
||||||
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
|
|
||||||
None => panic!("value is missing"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-18
@@ -27,10 +27,10 @@ where
|
|||||||
T: Serialize,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
value.serialize(TaggedSerializer {
|
value.serialize(TaggedSerializer {
|
||||||
type_ident: type_ident,
|
type_ident,
|
||||||
variant_ident: variant_ident,
|
variant_ident,
|
||||||
tag: tag,
|
tag,
|
||||||
variant_name: variant_name,
|
variant_name,
|
||||||
delegate: serializer,
|
delegate: serializer,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -350,8 +350,8 @@ mod content {
|
|||||||
impl<M> SerializeTupleVariantAsMapValue<M> {
|
impl<M> SerializeTupleVariantAsMapValue<M> {
|
||||||
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
||||||
SerializeTupleVariantAsMapValue {
|
SerializeTupleVariantAsMapValue {
|
||||||
map: map,
|
map,
|
||||||
name: name,
|
name,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,8 +390,8 @@ mod content {
|
|||||||
impl<M> SerializeStructVariantAsMapValue<M> {
|
impl<M> SerializeStructVariantAsMapValue<M> {
|
||||||
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
||||||
SerializeStructVariantAsMapValue {
|
SerializeStructVariantAsMapValue {
|
||||||
map: map,
|
map,
|
||||||
name: name,
|
name,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -711,7 +711,7 @@ mod content {
|
|||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeTupleStruct, E> {
|
) -> Result<Self::SerializeTupleStruct, E> {
|
||||||
Ok(SerializeTupleStruct {
|
Ok(SerializeTupleStruct {
|
||||||
name: name,
|
name,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
error: PhantomData,
|
error: PhantomData,
|
||||||
})
|
})
|
||||||
@@ -725,9 +725,9 @@ mod content {
|
|||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeTupleVariant, E> {
|
) -> Result<Self::SerializeTupleVariant, E> {
|
||||||
Ok(SerializeTupleVariant {
|
Ok(SerializeTupleVariant {
|
||||||
name: name,
|
name,
|
||||||
variant_index: variant_index,
|
variant_index,
|
||||||
variant: variant,
|
variant,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
error: PhantomData,
|
error: PhantomData,
|
||||||
})
|
})
|
||||||
@@ -747,7 +747,7 @@ mod content {
|
|||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeStruct, E> {
|
) -> Result<Self::SerializeStruct, E> {
|
||||||
Ok(SerializeStruct {
|
Ok(SerializeStruct {
|
||||||
name: name,
|
name,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
error: PhantomData,
|
error: PhantomData,
|
||||||
})
|
})
|
||||||
@@ -761,9 +761,9 @@ mod content {
|
|||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeStructVariant, E> {
|
) -> Result<Self::SerializeStructVariant, E> {
|
||||||
Ok(SerializeStructVariant {
|
Ok(SerializeStructVariant {
|
||||||
name: name,
|
name,
|
||||||
variant_index: variant_index,
|
variant_index,
|
||||||
variant: variant,
|
variant,
|
||||||
fields: Vec::with_capacity(len),
|
fields: Vec::with_capacity(len),
|
||||||
error: PhantomData,
|
error: PhantomData,
|
||||||
})
|
})
|
||||||
@@ -1273,8 +1273,8 @@ where
|
|||||||
{
|
{
|
||||||
fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
|
fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
|
||||||
FlatMapSerializeStructVariantAsMapValue {
|
FlatMapSerializeStructVariantAsMapValue {
|
||||||
map: map,
|
map,
|
||||||
name: name,
|
name,
|
||||||
fields: Vec::new(),
|
fields: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ impl<'a> Serialize for fmt::Arguments<'a> {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_cstr)))]
|
||||||
impl Serialize for CStr {
|
impl Serialize for CStr {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -83,7 +83,7 @@ impl Serialize for CStr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||||
impl Serialize for CString {
|
impl Serialize for CString {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -713,7 +713,7 @@ impl Serialize for net::IpAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
const DEC_DIGITS_LUT: &'static [u8] = b"\
|
const DEC_DIGITS_LUT: &[u8] = b"\
|
||||||
0001020304050607080910111213141516171819\
|
0001020304050607080910111213141516171819\
|
||||||
2021222324252627282930313233343536373839\
|
2021222324252627282930313233343536373839\
|
||||||
4041424344454647484950515253545556575859\
|
4041424344454647484950515253545556575859\
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
msrv = "1.31.0"
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.154" # remember to update html_root_url
|
version = "1.0.165" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
categories = ["no-std"]
|
categories = ["no-std"]
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
@@ -11,7 +11,7 @@ keywords = ["serde", "serialization", "no_std", "derive"]
|
|||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
readme = "crates-io.md"
|
readme = "crates-io.md"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
rust-version = "1.31"
|
rust-version = "1.56"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
@@ -24,7 +24,7 @@ proc-macro = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
syn = "1.0.104"
|
syn = "2.0.21"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde = { version = "1.0", path = "../serde" }
|
serde = { version = "1.0", path = "../serde" }
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
use std::env;
|
|
||||||
use std::process::Command;
|
|
||||||
use std::str;
|
|
||||||
|
|
||||||
// The rustc-cfg strings below are *not* public API. Please let us know by
|
|
||||||
// opening a GitHub issue if your build environment requires some way to enable
|
|
||||||
// these cfgs other than by executing our build script.
|
|
||||||
fn main() {
|
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
|
||||||
|
|
||||||
let minor = match rustc_minor_version() {
|
|
||||||
Some(minor) => minor,
|
|
||||||
None => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Underscore const names stabilized in Rust 1.37:
|
|
||||||
// https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#using-unnamed-const-items-for-macros
|
|
||||||
if minor < 37 {
|
|
||||||
println!("cargo:rustc-cfg=no_underscore_consts");
|
|
||||||
}
|
|
||||||
|
|
||||||
// The ptr::addr_of! macro stabilized in Rust 1.51:
|
|
||||||
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
|
|
||||||
if minor < 51 {
|
|
||||||
println!("cargo:rustc-cfg=no_ptr_addr_of");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rustc_minor_version() -> Option<u32> {
|
|
||||||
let rustc = env::var_os("RUSTC")?;
|
|
||||||
let output = Command::new(rustc).arg("--version").output().ok()?;
|
|
||||||
let version = str::from_utf8(&output.stdout).ok()?;
|
|
||||||
let mut pieces = version.split('.');
|
|
||||||
if pieces.next() != Some("rustc 1") {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
pieces.next()?.parse().ok()
|
|
||||||
}
|
|
||||||
@@ -200,10 +200,16 @@ pub fn with_bound(
|
|||||||
for arg in &arguments.args {
|
for arg in &arguments.args {
|
||||||
match arg {
|
match arg {
|
||||||
syn::GenericArgument::Type(arg) => self.visit_type(arg),
|
syn::GenericArgument::Type(arg) => self.visit_type(arg),
|
||||||
syn::GenericArgument::Binding(arg) => self.visit_type(&arg.ty),
|
syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),
|
||||||
syn::GenericArgument::Lifetime(_)
|
syn::GenericArgument::Lifetime(_)
|
||||||
| syn::GenericArgument::Constraint(_)
|
| syn::GenericArgument::Const(_)
|
||||||
| syn::GenericArgument::Const(_) => {}
|
| syn::GenericArgument::AssocConst(_)
|
||||||
|
| syn::GenericArgument::Constraint(_) => {}
|
||||||
|
#[cfg_attr(
|
||||||
|
all(test, exhaustive),
|
||||||
|
deny(non_exhaustive_omitted_patterns)
|
||||||
|
)]
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,7 +232,9 @@ pub fn with_bound(
|
|||||||
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
|
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
|
||||||
match bound {
|
match bound {
|
||||||
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
|
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
|
||||||
syn::TypeParamBound::Lifetime(_) => {}
|
syn::TypeParamBound::Lifetime(_) | syn::TypeParamBound::Verbatim(_) => {}
|
||||||
|
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +259,7 @@ pub fn with_bound(
|
|||||||
};
|
};
|
||||||
match &cont.data {
|
match &cont.data {
|
||||||
Data::Enum(variants) => {
|
Data::Enum(variants) => {
|
||||||
for variant in variants.iter() {
|
for variant in variants {
|
||||||
let relevant_fields = variant
|
let relevant_fields = variant
|
||||||
.fields
|
.fields
|
||||||
.iter()
|
.iter()
|
||||||
@@ -334,7 +342,7 @@ pub fn with_self_bound(
|
|||||||
|
|
||||||
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
|
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
|
||||||
let bound = syn::Lifetime::new(lifetime, Span::call_site());
|
let bound = syn::Lifetime::new(lifetime, Span::call_site());
|
||||||
let def = syn::LifetimeDef {
|
let def = syn::LifetimeParam {
|
||||||
attrs: Vec::new(),
|
attrs: Vec::new(),
|
||||||
lifetime: bound.clone(),
|
lifetime: bound.clone(),
|
||||||
colon_token: None,
|
colon_token: None,
|
||||||
|
|||||||
+100
-58
@@ -15,9 +15,7 @@ use this;
|
|||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
pub fn expand_derive_deserialize(
|
pub fn expand_derive_deserialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
||||||
input: &mut syn::DeriveInput,
|
|
||||||
) -> Result<TokenStream, Vec<syn::Error>> {
|
|
||||||
replace_receiver(input);
|
replace_receiver(input);
|
||||||
|
|
||||||
let ctxt = Ctxt::new();
|
let ctxt = Ctxt::new();
|
||||||
@@ -69,8 +67,6 @@ pub fn expand_derive_deserialize(
|
|||||||
|
|
||||||
Ok(dummy::wrap_in_const(
|
Ok(dummy::wrap_in_const(
|
||||||
cont.attrs.custom_serde_path(),
|
cont.attrs.custom_serde_path(),
|
||||||
"DESERIALIZE",
|
|
||||||
ident,
|
|
||||||
impl_block,
|
impl_block,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -244,9 +240,9 @@ impl BorrowedLifetimes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn de_lifetime_def(&self) -> Option<syn::LifetimeDef> {
|
fn de_lifetime_param(&self) -> Option<syn::LifetimeParam> {
|
||||||
match self {
|
match self {
|
||||||
BorrowedLifetimes::Borrowed(bounds) => Some(syn::LifetimeDef {
|
BorrowedLifetimes::Borrowed(bounds) => Some(syn::LifetimeParam {
|
||||||
attrs: Vec::new(),
|
attrs: Vec::new(),
|
||||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
||||||
colon_token: None,
|
colon_token: None,
|
||||||
@@ -419,6 +415,7 @@ fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fra
|
|||||||
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor;
|
struct __Visitor;
|
||||||
|
|
||||||
impl<'de> _serde::de::Visitor<'de> for __Visitor {
|
impl<'de> _serde::de::Visitor<'de> for __Visitor {
|
||||||
@@ -448,14 +445,19 @@ fn deserialize_tuple(
|
|||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
deserializer: Option<TokenStream>,
|
deserializer: Option<TokenStream>,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
|
assert!(!cattrs.has_flatten());
|
||||||
|
|
||||||
|
let field_count = fields
|
||||||
|
.iter()
|
||||||
|
.filter(|field| !field.attrs.skip_deserializing())
|
||||||
|
.count();
|
||||||
|
|
||||||
let this_type = ¶ms.this_type;
|
let this_type = ¶ms.this_type;
|
||||||
let this_value = ¶ms.this_value;
|
let this_value = ¶ms.this_value;
|
||||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
split_with_de_lifetime(params);
|
split_with_de_lifetime(params);
|
||||||
let delife = params.borrowed.de_lifetime();
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
assert!(!cattrs.has_flatten());
|
|
||||||
|
|
||||||
// If there are getters (implying private fields), construct the local type
|
// If there are getters (implying private fields), construct the local type
|
||||||
// and use an `Into` conversion to get the remote type. If there are no
|
// and use an `Into` conversion to get the remote type. If there are no
|
||||||
// getters then construct the target type directly.
|
// getters then construct the target type directly.
|
||||||
@@ -496,25 +498,25 @@ fn deserialize_tuple(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let dispatch = if let Some(deserializer) = deserializer {
|
let dispatch = if let Some(deserializer) = deserializer {
|
||||||
quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #nfields, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #field_count, #visitor_expr))
|
||||||
} else if is_enum {
|
} else if is_enum {
|
||||||
quote!(_serde::de::VariantAccess::tuple_variant(__variant, #nfields, #visitor_expr))
|
quote!(_serde::de::VariantAccess::tuple_variant(__variant, #field_count, #visitor_expr))
|
||||||
} else if nfields == 1 {
|
} else if nfields == 1 {
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
||||||
} else {
|
} else {
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #nfields, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr))
|
||||||
};
|
};
|
||||||
|
|
||||||
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
let visitor_var = if field_count == 0 {
|
||||||
let visitor_var = if all_skipped {
|
|
||||||
quote!(_)
|
quote!(_)
|
||||||
} else {
|
} else {
|
||||||
quote!(mut __seq)
|
quote!(mut __seq)
|
||||||
};
|
};
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #de_impl_generics #where_clause {
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -550,13 +552,18 @@ fn deserialize_tuple_in_place(
|
|||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
deserializer: Option<TokenStream>,
|
deserializer: Option<TokenStream>,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
|
assert!(!cattrs.has_flatten());
|
||||||
|
|
||||||
|
let field_count = fields
|
||||||
|
.iter()
|
||||||
|
.filter(|field| !field.attrs.skip_deserializing())
|
||||||
|
.count();
|
||||||
|
|
||||||
let this_type = ¶ms.this_type;
|
let this_type = ¶ms.this_type;
|
||||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
split_with_de_lifetime(params);
|
split_with_de_lifetime(params);
|
||||||
let delife = params.borrowed.de_lifetime();
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
assert!(!cattrs.has_flatten());
|
|
||||||
|
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let expecting = match variant_ident {
|
let expecting = match variant_ident {
|
||||||
Some(variant_ident) => format!("tuple variant {}::{}", params.type_name(), variant_ident),
|
Some(variant_ident) => format!("tuple variant {}::{}", params.type_name(), variant_ident),
|
||||||
@@ -582,19 +589,18 @@ fn deserialize_tuple_in_place(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let dispatch = if let Some(deserializer) = deserializer {
|
let dispatch = if let Some(deserializer) = deserializer {
|
||||||
quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #nfields, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #field_count, #visitor_expr))
|
||||||
} else if is_enum {
|
} else if is_enum {
|
||||||
quote!(_serde::de::VariantAccess::tuple_variant(__variant, #nfields, #visitor_expr))
|
quote!(_serde::de::VariantAccess::tuple_variant(__variant, #field_count, #visitor_expr))
|
||||||
} else if nfields == 1 {
|
} else if nfields == 1 {
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
||||||
} else {
|
} else {
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #nfields, #visitor_expr))
|
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr))
|
||||||
};
|
};
|
||||||
|
|
||||||
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
let visitor_var = if field_count == 0 {
|
||||||
let visitor_var = if all_skipped {
|
|
||||||
quote!(_)
|
quote!(_)
|
||||||
} else {
|
} else {
|
||||||
quote!(mut __seq)
|
quote!(mut __seq)
|
||||||
@@ -605,6 +611,7 @@ fn deserialize_tuple_in_place(
|
|||||||
let place_life = place_lifetime();
|
let place_life = place_lifetime();
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #in_place_impl_generics #where_clause {
|
struct __Visitor #in_place_impl_generics #where_clause {
|
||||||
place: &#place_life mut #this_type #ty_generics,
|
place: &#place_life mut #this_type #ty_generics,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -1020,6 +1027,7 @@ fn deserialize_struct(
|
|||||||
quote_block! {
|
quote_block! {
|
||||||
#field_visitor
|
#field_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #de_impl_generics #where_clause {
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -1132,6 +1140,7 @@ fn deserialize_struct_in_place(
|
|||||||
Some(quote_block! {
|
Some(quote_block! {
|
||||||
#field_visitor
|
#field_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #in_place_impl_generics #where_clause {
|
struct __Visitor #in_place_impl_generics #where_clause {
|
||||||
place: &#place_life mut #this_type #ty_generics,
|
place: &#place_life mut #this_type #ty_generics,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -1165,6 +1174,22 @@ fn deserialize_enum(
|
|||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
variants: &[Variant],
|
variants: &[Variant],
|
||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
// The variants have already been checked (in ast.rs) that all untagged variants appear at the end
|
||||||
|
match variants.iter().position(|var| var.attrs.untagged()) {
|
||||||
|
Some(variant_idx) => {
|
||||||
|
let (tagged, untagged) = variants.split_at(variant_idx);
|
||||||
|
let tagged_frag = Expr(deserialize_homogeneous_enum(params, tagged, cattrs));
|
||||||
|
deserialize_untagged_enum_after(params, untagged, cattrs, Some(tagged_frag))
|
||||||
|
}
|
||||||
|
None => deserialize_homogeneous_enum(params, variants, cattrs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_homogeneous_enum(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
match cattrs.tag() {
|
match cattrs.tag() {
|
||||||
attr::TagType::External => deserialize_externally_tagged_enum(params, variants, cattrs),
|
attr::TagType::External => deserialize_externally_tagged_enum(params, variants, cattrs),
|
||||||
@@ -1203,6 +1228,7 @@ fn prepare_enum_variant_enum(
|
|||||||
let variants_stmt = {
|
let variants_stmt = {
|
||||||
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
||||||
quote! {
|
quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1275,6 +1301,7 @@ fn deserialize_externally_tagged_enum(
|
|||||||
quote_block! {
|
quote_block! {
|
||||||
#variant_visitor
|
#variant_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #de_impl_generics #where_clause {
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -1522,6 +1549,7 @@ fn deserialize_adjacently_tagged_enum(
|
|||||||
|
|
||||||
#variants_stmt
|
#variants_stmt
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Seed #de_impl_generics #where_clause {
|
struct __Seed #de_impl_generics #where_clause {
|
||||||
field: __Field,
|
field: __Field,
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
@@ -1541,6 +1569,7 @@ fn deserialize_adjacently_tagged_enum(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __Visitor #de_impl_generics #where_clause {
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -1643,6 +1672,7 @@ fn deserialize_adjacently_tagged_enum(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
const FIELDS: &'static [&'static str] = &[#tag, #content];
|
const FIELDS: &'static [&'static str] = &[#tag, #content];
|
||||||
_serde::Deserializer::deserialize_struct(
|
_serde::Deserializer::deserialize_struct(
|
||||||
__deserializer,
|
__deserializer,
|
||||||
@@ -1660,6 +1690,16 @@ fn deserialize_untagged_enum(
|
|||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
variants: &[Variant],
|
variants: &[Variant],
|
||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
let first_attempt = None;
|
||||||
|
deserialize_untagged_enum_after(params, variants, cattrs, first_attempt)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_untagged_enum_after(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
first_attempt: Option<Expr>,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let attempts = variants
|
let attempts = variants
|
||||||
.iter()
|
.iter()
|
||||||
@@ -1669,12 +1709,10 @@ fn deserialize_untagged_enum(
|
|||||||
params,
|
params,
|
||||||
variant,
|
variant,
|
||||||
cattrs,
|
cattrs,
|
||||||
quote!(
|
quote!(__deserializer),
|
||||||
_serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content)
|
|
||||||
),
|
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
|
let attempts = first_attempt.into_iter().chain(attempts);
|
||||||
// TODO this message could be better by saving the errors from the failed
|
// TODO this message could be better by saving the errors from the failed
|
||||||
// attempts. The heuristic used by TOML was to count the number of fields
|
// attempts. The heuristic used by TOML was to count the number of fields
|
||||||
// processed before an error, and use the error that happened after the
|
// processed before an error, and use the error that happened after the
|
||||||
@@ -1689,6 +1727,7 @@ fn deserialize_untagged_enum(
|
|||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer));
|
let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer));
|
||||||
|
let __deserializer = _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
|
||||||
|
|
||||||
#(
|
#(
|
||||||
if let _serde::__private::Ok(__ok) = #attempts {
|
if let _serde::__private::Ok(__ok) = #attempts {
|
||||||
@@ -1954,11 +1993,13 @@ fn deserialize_generated_identifier(
|
|||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
|
#[doc(hidden)]
|
||||||
enum __Field #lifetime {
|
enum __Field #lifetime {
|
||||||
#(#field_idents,)*
|
#(#field_idents,)*
|
||||||
#ignore_variant
|
#ignore_variant
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __FieldVisitor;
|
struct __FieldVisitor;
|
||||||
|
|
||||||
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
|
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
|
||||||
@@ -2046,11 +2087,13 @@ fn deserialize_custom_identifier(
|
|||||||
None
|
None
|
||||||
} else if is_variant {
|
} else if is_variant {
|
||||||
let variants = quote! {
|
let variants = quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
|
const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
|
||||||
};
|
};
|
||||||
Some(variants)
|
Some(variants)
|
||||||
} else {
|
} else {
|
||||||
let fields = quote! {
|
let fields = quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
const FIELDS: &'static [&'static str] = &[ #(#names),* ];
|
const FIELDS: &'static [&'static str] = &[ #(#names),* ];
|
||||||
};
|
};
|
||||||
Some(fields)
|
Some(fields)
|
||||||
@@ -2072,6 +2115,7 @@ fn deserialize_custom_identifier(
|
|||||||
quote_block! {
|
quote_block! {
|
||||||
#names_const
|
#names_const
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
struct __FieldVisitor #de_impl_generics #where_clause {
|
struct __FieldVisitor #de_impl_generics #where_clause {
|
||||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||||
@@ -2406,6 +2450,7 @@ fn deserialize_struct_as_struct_visitor(
|
|||||||
.flat_map(|(_, _, aliases)| aliases);
|
.flat_map(|(_, _, aliases)| aliases);
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -2684,6 +2729,7 @@ fn deserialize_struct_as_struct_in_place_visitor(
|
|||||||
let fields_stmt = {
|
let fields_stmt = {
|
||||||
let field_names = field_names_idents.iter().map(|(name, _, _)| name);
|
let field_names = field_names_idents.iter().map(|(name, _, _)| name);
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -2864,6 +2910,7 @@ fn wrap_deserialize_with(
|
|||||||
let delife = params.borrowed.de_lifetime();
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
let wrapper = quote! {
|
let wrapper = quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __DeserializeWith #de_impl_generics #where_clause {
|
struct __DeserializeWith #de_impl_generics #where_clause {
|
||||||
value: #value_ty,
|
value: #value_ty,
|
||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
@@ -3011,7 +3058,7 @@ struct InPlaceImplGenerics<'a>(&'a Parameters);
|
|||||||
impl<'a> ToTokens for DeImplGenerics<'a> {
|
impl<'a> ToTokens for DeImplGenerics<'a> {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
let mut generics = self.0.generics.clone();
|
let mut generics = self.0.generics.clone();
|
||||||
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_def() {
|
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {
|
||||||
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(generics.params)
|
.chain(generics.params)
|
||||||
@@ -3046,7 +3093,7 @@ impl<'a> ToTokens for InPlaceImplGenerics<'a> {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(generics.params)
|
.chain(generics.params)
|
||||||
.collect();
|
.collect();
|
||||||
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_def() {
|
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {
|
||||||
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(generics.params)
|
.chain(generics.params)
|
||||||
@@ -3068,23 +3115,31 @@ struct DeTypeGenerics<'a>(&'a Parameters);
|
|||||||
#[cfg(feature = "deserialize_in_place")]
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
struct InPlaceTypeGenerics<'a>(&'a Parameters);
|
struct InPlaceTypeGenerics<'a>(&'a Parameters);
|
||||||
|
|
||||||
|
fn de_type_generics_to_tokens(
|
||||||
|
mut generics: syn::Generics,
|
||||||
|
borrowed: &BorrowedLifetimes,
|
||||||
|
tokens: &mut TokenStream,
|
||||||
|
) {
|
||||||
|
if borrowed.de_lifetime_param().is_some() {
|
||||||
|
let def = syn::LifetimeParam {
|
||||||
|
attrs: Vec::new(),
|
||||||
|
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
||||||
|
colon_token: None,
|
||||||
|
bounds: Punctuated::new(),
|
||||||
|
};
|
||||||
|
// Prepend 'de lifetime to list of generics
|
||||||
|
generics.params = Some(syn::GenericParam::Lifetime(def))
|
||||||
|
.into_iter()
|
||||||
|
.chain(generics.params)
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
let (_, ty_generics, _) = generics.split_for_impl();
|
||||||
|
ty_generics.to_tokens(tokens);
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> ToTokens for DeTypeGenerics<'a> {
|
impl<'a> ToTokens for DeTypeGenerics<'a> {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
let mut generics = self.0.generics.clone();
|
de_type_generics_to_tokens(self.0.generics.clone(), &self.0.borrowed, tokens);
|
||||||
if self.0.borrowed.de_lifetime_def().is_some() {
|
|
||||||
let def = syn::LifetimeDef {
|
|
||||||
attrs: Vec::new(),
|
|
||||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
|
||||||
colon_token: None,
|
|
||||||
bounds: Punctuated::new(),
|
|
||||||
};
|
|
||||||
generics.params = Some(syn::GenericParam::Lifetime(def))
|
|
||||||
.into_iter()
|
|
||||||
.chain(generics.params)
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
let (_, ty_generics, _) = generics.split_for_impl();
|
|
||||||
ty_generics.to_tokens(tokens);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3097,20 +3152,7 @@ impl<'a> ToTokens for InPlaceTypeGenerics<'a> {
|
|||||||
.chain(generics.params)
|
.chain(generics.params)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if self.0.borrowed.de_lifetime_def().is_some() {
|
de_type_generics_to_tokens(generics, &self.0.borrowed, tokens);
|
||||||
let def = syn::LifetimeDef {
|
|
||||||
attrs: Vec::new(),
|
|
||||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
|
||||||
colon_token: None,
|
|
||||||
bounds: Punctuated::new(),
|
|
||||||
};
|
|
||||||
generics.params = Some(syn::GenericParam::Lifetime(def))
|
|
||||||
.into_iter()
|
|
||||||
.chain(generics.params)
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
let (_, ty_generics, _) = generics.split_for_impl();
|
|
||||||
ty_generics.to_tokens(tokens);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3122,8 +3164,8 @@ impl<'a> DeTypeGenerics<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "deserialize_in_place")]
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
fn place_lifetime() -> syn::LifetimeDef {
|
fn place_lifetime() -> syn::LifetimeParam {
|
||||||
syn::LifetimeDef {
|
syn::LifetimeParam {
|
||||||
attrs: Vec::new(),
|
attrs: Vec::new(),
|
||||||
lifetime: syn::Lifetime::new("'place", Span::call_site()),
|
lifetime: syn::Lifetime::new("'place", Span::call_site()),
|
||||||
colon_token: None,
|
colon_token: None,
|
||||||
|
|||||||
@@ -1,23 +1,11 @@
|
|||||||
use proc_macro2::{Ident, TokenStream};
|
use proc_macro2::TokenStream;
|
||||||
use quote::format_ident;
|
|
||||||
|
|
||||||
use syn;
|
use syn;
|
||||||
use try;
|
use try;
|
||||||
|
|
||||||
pub fn wrap_in_const(
|
pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream {
|
||||||
serde_path: Option<&syn::Path>,
|
|
||||||
trait_: &str,
|
|
||||||
ty: &Ident,
|
|
||||||
code: TokenStream,
|
|
||||||
) -> TokenStream {
|
|
||||||
let try_replacement = try::replacement();
|
let try_replacement = try::replacement();
|
||||||
|
|
||||||
let dummy_const = if cfg!(no_underscore_consts) {
|
|
||||||
format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty))
|
|
||||||
} else {
|
|
||||||
format_ident!("_")
|
|
||||||
};
|
|
||||||
|
|
||||||
let use_serde = match serde_path {
|
let use_serde = match serde_path {
|
||||||
Some(path) => quote! {
|
Some(path) => quote! {
|
||||||
use #path as _serde;
|
use #path as _serde;
|
||||||
@@ -31,14 +19,10 @@ pub fn wrap_in_const(
|
|||||||
quote! {
|
quote! {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||||
const #dummy_const: () = {
|
const _: () = {
|
||||||
#use_serde
|
#use_serde
|
||||||
#try_replacement
|
#try_replacement
|
||||||
#code
|
#code
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unraw(ident: &Ident) -> String {
|
|
||||||
ident.to_string().trim_start_matches("r#").to_owned()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ fn enum_from_ast<'a>(
|
|||||||
variants: &'a Punctuated<syn::Variant, Token![,]>,
|
variants: &'a Punctuated<syn::Variant, Token![,]>,
|
||||||
container_default: &attr::Default,
|
container_default: &attr::Default,
|
||||||
) -> Vec<Variant<'a>> {
|
) -> Vec<Variant<'a>> {
|
||||||
variants
|
let variants: Vec<Variant> = variants
|
||||||
.iter()
|
.iter()
|
||||||
.map(|variant| {
|
.map(|variant| {
|
||||||
let attrs = attr::Variant::from_ast(cx, variant);
|
let attrs = attr::Variant::from_ast(cx, variant);
|
||||||
@@ -154,7 +154,20 @@ fn enum_from_ast<'a>(
|
|||||||
original: variant,
|
original: variant,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect();
|
||||||
|
|
||||||
|
let index_of_last_tagged_variant = variants
|
||||||
|
.iter()
|
||||||
|
.rposition(|variant| !variant.attrs.untagged());
|
||||||
|
if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant {
|
||||||
|
for variant in &variants[..index_of_last_tagged_variant] {
|
||||||
|
if variant.attrs.untagged() {
|
||||||
|
cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variants
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_from_ast<'a>(
|
fn struct_from_ast<'a>(
|
||||||
|
|||||||
+582
-725
File diff suppressed because it is too large
Load Diff
@@ -3,8 +3,8 @@ use internals::attr::{Identifier, TagType};
|
|||||||
use internals::{ungroup, Ctxt, Derive};
|
use internals::{ungroup, Ctxt, Derive};
|
||||||
use syn::{Member, Type};
|
use syn::{Member, Type};
|
||||||
|
|
||||||
/// Cross-cutting checks that require looking at more than a single attrs
|
// Cross-cutting checks that require looking at more than a single attrs object.
|
||||||
/// object. Simpler checks should happen when parsing and building the attrs.
|
// Simpler checks should happen when parsing and building the attrs.
|
||||||
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||||
check_remote_generic(cx, cont);
|
check_remote_generic(cx, cont);
|
||||||
check_getter(cx, cont);
|
check_getter(cx, cont);
|
||||||
@@ -17,18 +17,18 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
|||||||
check_from_and_try_from(cx, cont);
|
check_from_and_try_from(cx, cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remote derive definition type must have either all of the generics of the
|
// Remote derive definition type must have either all of the generics of the
|
||||||
/// remote type:
|
// remote type:
|
||||||
///
|
//
|
||||||
/// #[serde(remote = "Generic")]
|
// #[serde(remote = "Generic")]
|
||||||
/// struct Generic<T> {…}
|
// struct Generic<T> {…}
|
||||||
///
|
//
|
||||||
/// or none of them, i.e. defining impls for one concrete instantiation of the
|
// or none of them, i.e. defining impls for one concrete instantiation of the
|
||||||
/// remote type only:
|
// remote type only:
|
||||||
///
|
//
|
||||||
/// #[serde(remote = "Generic<T>")]
|
// #[serde(remote = "Generic<T>")]
|
||||||
/// struct ConcreteDef {…}
|
// struct ConcreteDef {…}
|
||||||
///
|
//
|
||||||
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
||||||
if let Some(remote) = cont.attrs.remote() {
|
if let Some(remote) = cont.attrs.remote() {
|
||||||
let local_has_generic = !cont.generics.params.is_empty();
|
let local_has_generic = !cont.generics.params.is_empty();
|
||||||
@@ -39,8 +39,8 @@ fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Getters are only allowed inside structs (not enums) with the `remote`
|
// Getters are only allowed inside structs (not enums) with the `remote`
|
||||||
/// attribute.
|
// attribute.
|
||||||
fn check_getter(cx: &Ctxt, cont: &Container) {
|
fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||||
match cont.data {
|
match cont.data {
|
||||||
Data::Enum(_) => {
|
Data::Enum(_) => {
|
||||||
@@ -62,7 +62,7 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flattening has some restrictions we can test.
|
// Flattening has some restrictions we can test.
|
||||||
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
||||||
match &cont.data {
|
match &cont.data {
|
||||||
Data::Enum(variants) => {
|
Data::Enum(variants) => {
|
||||||
@@ -101,18 +101,16 @@ fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The `other` attribute must be used at most once and it must be the last
|
// The `other` attribute must be used at most once and it must be the last
|
||||||
/// variant of an enum.
|
// variant of an enum.
|
||||||
///
|
//
|
||||||
/// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
||||||
/// `field_identifier` all but possibly one variant must be unit variants. The
|
// `field_identifier` all but possibly one variant must be unit variants. The
|
||||||
/// last variant may be a newtype variant which is an implicit "other" case.
|
// last variant may be a newtype variant which is an implicit "other" case.
|
||||||
fn check_identifier(cx: &Ctxt, cont: &Container) {
|
fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||||
let variants = match &cont.data {
|
let variants = match &cont.data {
|
||||||
Data::Enum(variants) => variants,
|
Data::Enum(variants) => variants,
|
||||||
Data::Struct(_, _) => {
|
Data::Struct(_, _) => return,
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (i, variant) in variants.iter().enumerate() {
|
for (i, variant) in variants.iter().enumerate() {
|
||||||
@@ -189,17 +187,15 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Skip-(de)serializing attributes are not allowed on variants marked
|
// Skip-(de)serializing attributes are not allowed on variants marked
|
||||||
/// (de)serialize_with.
|
// (de)serialize_with.
|
||||||
fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||||
let variants = match &cont.data {
|
let variants = match &cont.data {
|
||||||
Data::Enum(variants) => variants,
|
Data::Enum(variants) => variants,
|
||||||
Data::Struct(_, _) => {
|
Data::Struct(_, _) => return,
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for variant in variants.iter() {
|
for variant in variants {
|
||||||
if variant.attrs.serialize_with().is_some() {
|
if variant.attrs.serialize_with().is_some() {
|
||||||
if variant.attrs.skip_serializing() {
|
if variant.attrs.skip_serializing() {
|
||||||
cx.error_spanned_by(
|
cx.error_spanned_by(
|
||||||
@@ -264,10 +260,9 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The tag of an internally-tagged struct variant must not be
|
// The tag of an internally-tagged struct variant must not be the same as either
|
||||||
/// the same as either one of its fields, as this would result in
|
// one of its fields, as this would result in duplicate keys in the serialized
|
||||||
/// duplicate keys in the serialized output and/or ambiguity in
|
// output and/or ambiguity in the to-be-deserialized input.
|
||||||
/// the to-be-deserialized input.
|
|
||||||
fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||||
let variants = match &cont.data {
|
let variants = match &cont.data {
|
||||||
Data::Enum(variants) => variants,
|
Data::Enum(variants) => variants,
|
||||||
@@ -313,8 +308,8 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// In the case of adjacently-tagged enums, the type and the
|
// In the case of adjacently-tagged enums, the type and the contents tag must
|
||||||
/// contents tag must differ, for the same reason.
|
// differ, for the same reason.
|
||||||
fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
||||||
let (type_tag, content_tag) = match cont.attrs.tag() {
|
let (type_tag, content_tag) = match cont.attrs.tag() {
|
||||||
TagType::Adjacent { tag, content } => (tag, content),
|
TagType::Adjacent { tag, content } => (tag, content),
|
||||||
@@ -332,7 +327,7 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enums and unit structs cannot be transparent.
|
// Enums and unit structs cannot be transparent.
|
||||||
fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||||
if !cont.attrs.transparent() {
|
if !cont.attrs.transparent() {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -44,12 +44,19 @@ impl Ctxt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Consume this object, producing a formatted error string if there are errors.
|
/// Consume this object, producing a formatted error string if there are errors.
|
||||||
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
pub fn check(self) -> syn::Result<()> {
|
||||||
let errors = self.errors.borrow_mut().take().unwrap();
|
let mut errors = self.errors.borrow_mut().take().unwrap().into_iter();
|
||||||
match errors.len() {
|
|
||||||
0 => Ok(()),
|
let mut combined = match errors.next() {
|
||||||
_ => Err(errors),
|
Some(first) => first,
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
for rest in errors {
|
||||||
|
combined.combine(rest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Err(combined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -179,10 +179,13 @@ impl ReplaceReceiver<'_> {
|
|||||||
for arg in &mut arguments.args {
|
for arg in &mut arguments.args {
|
||||||
match arg {
|
match arg {
|
||||||
GenericArgument::Type(arg) => self.visit_type_mut(arg),
|
GenericArgument::Type(arg) => self.visit_type_mut(arg),
|
||||||
GenericArgument::Binding(arg) => self.visit_type_mut(&mut arg.ty),
|
GenericArgument::AssocType(arg) => self.visit_type_mut(&mut arg.ty),
|
||||||
GenericArgument::Lifetime(_)
|
GenericArgument::Lifetime(_)
|
||||||
| GenericArgument::Constraint(_)
|
| GenericArgument::Const(_)
|
||||||
| GenericArgument::Const(_) => {}
|
| GenericArgument::AssocConst(_)
|
||||||
|
| GenericArgument::Constraint(_) => {}
|
||||||
|
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,7 +208,9 @@ impl ReplaceReceiver<'_> {
|
|||||||
fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {
|
fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {
|
||||||
match bound {
|
match bound {
|
||||||
TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),
|
TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),
|
||||||
TypeParamBound::Lifetime(_) => {}
|
TypeParamBound::Lifetime(_) | TypeParamBound::Verbatim(_) => {}
|
||||||
|
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +234,9 @@ impl ReplaceReceiver<'_> {
|
|||||||
self.visit_type_param_bound_mut(bound);
|
self.visit_type_param_bound_mut(bound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WherePredicate::Lifetime(_) | WherePredicate::Eq(_) => {}
|
WherePredicate::Lifetime(_) => {}
|
||||||
|
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub const DEFAULT: Symbol = Symbol("default");
|
|||||||
pub const DENY_UNKNOWN_FIELDS: Symbol = Symbol("deny_unknown_fields");
|
pub const DENY_UNKNOWN_FIELDS: Symbol = Symbol("deny_unknown_fields");
|
||||||
pub const DESERIALIZE: Symbol = Symbol("deserialize");
|
pub const DESERIALIZE: Symbol = Symbol("deserialize");
|
||||||
pub const DESERIALIZE_WITH: Symbol = Symbol("deserialize_with");
|
pub const DESERIALIZE_WITH: Symbol = Symbol("deserialize_with");
|
||||||
|
pub const EXPECTING: Symbol = Symbol("expecting");
|
||||||
pub const FIELD_IDENTIFIER: Symbol = Symbol("field_identifier");
|
pub const FIELD_IDENTIFIER: Symbol = Symbol("field_identifier");
|
||||||
pub const FLATTEN: Symbol = Symbol("flatten");
|
pub const FLATTEN: Symbol = Symbol("flatten");
|
||||||
pub const FROM: Symbol = Symbol("from");
|
pub const FROM: Symbol = Symbol("from");
|
||||||
@@ -22,6 +23,7 @@ pub const OTHER: Symbol = Symbol("other");
|
|||||||
pub const REMOTE: Symbol = Symbol("remote");
|
pub const REMOTE: Symbol = Symbol("remote");
|
||||||
pub const RENAME: Symbol = Symbol("rename");
|
pub const RENAME: Symbol = Symbol("rename");
|
||||||
pub const RENAME_ALL: Symbol = Symbol("rename_all");
|
pub const RENAME_ALL: Symbol = Symbol("rename_all");
|
||||||
|
pub const REPR: Symbol = Symbol("repr");
|
||||||
pub const SERDE: Symbol = Symbol("serde");
|
pub const SERDE: Symbol = Symbol("serde");
|
||||||
pub const SERIALIZE: Symbol = Symbol("serialize");
|
pub const SERIALIZE: Symbol = Symbol("serialize");
|
||||||
pub const SERIALIZE_WITH: Symbol = Symbol("serialize_with");
|
pub const SERIALIZE_WITH: Symbol = Symbol("serialize_with");
|
||||||
@@ -35,7 +37,6 @@ pub const TRY_FROM: Symbol = Symbol("try_from");
|
|||||||
pub const UNTAGGED: Symbol = Symbol("untagged");
|
pub const UNTAGGED: Symbol = Symbol("untagged");
|
||||||
pub const VARIANT_IDENTIFIER: Symbol = Symbol("variant_identifier");
|
pub const VARIANT_IDENTIFIER: Symbol = Symbol("variant_identifier");
|
||||||
pub const WITH: Symbol = Symbol("with");
|
pub const WITH: Symbol = Symbol("with");
|
||||||
pub const EXPECTING: Symbol = Symbol("expecting");
|
|
||||||
|
|
||||||
impl PartialEq<Symbol> for Ident {
|
impl PartialEq<Symbol> for Ident {
|
||||||
fn eq(&self, word: &Symbol) -> bool {
|
fn eq(&self, word: &Symbol) -> bool {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
//!
|
//!
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.154")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.165")]
|
||||||
#![allow(unknown_lints, bare_trait_objects)]
|
#![allow(unknown_lints, bare_trait_objects)]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
@@ -92,7 +92,7 @@ mod try;
|
|||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
ser::expand_derive_serialize(&mut input)
|
ser::expand_derive_serialize(&mut input)
|
||||||
.unwrap_or_else(to_compile_errors)
|
.unwrap_or_else(syn::Error::into_compile_error)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,11 +100,6 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
de::expand_derive_deserialize(&mut input)
|
de::expand_derive_deserialize(&mut input)
|
||||||
.unwrap_or_else(to_compile_errors)
|
.unwrap_or_else(syn::Error::into_compile_error)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
|
|
||||||
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
|
|
||||||
quote!(#(#compile_errors)*)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -97,29 +97,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke
|
|||||||
|
|
||||||
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
|
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
|
||||||
|
|
||||||
#[cfg(not(no_ptr_addr_of))]
|
quote! {
|
||||||
{
|
match _serde::__private::None::<&#type_ident #ty_generics> {
|
||||||
quote! {
|
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
||||||
match _serde::__private::None::<&#type_ident #ty_generics> {
|
#(
|
||||||
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
||||||
#(
|
)*
|
||||||
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
|
||||||
)*
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(no_ptr_addr_of)]
|
|
||||||
{
|
|
||||||
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
match _serde::__private::None::<#type_ident #ty_generics> {
|
|
||||||
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-10
@@ -10,9 +10,7 @@ use internals::{attr, replace_receiver, Ctxt, Derive};
|
|||||||
use pretend;
|
use pretend;
|
||||||
use this;
|
use this;
|
||||||
|
|
||||||
pub fn expand_derive_serialize(
|
pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
||||||
input: &mut syn::DeriveInput,
|
|
||||||
) -> Result<TokenStream, Vec<syn::Error>> {
|
|
||||||
replace_receiver(input);
|
replace_receiver(input);
|
||||||
|
|
||||||
let ctxt = Ctxt::new();
|
let ctxt = Ctxt::new();
|
||||||
@@ -59,8 +57,6 @@ pub fn expand_derive_serialize(
|
|||||||
|
|
||||||
Ok(dummy::wrap_in_const(
|
Ok(dummy::wrap_in_const(
|
||||||
cont.attrs.custom_serde_path(),
|
cont.attrs.custom_serde_path(),
|
||||||
"SERIALIZE",
|
|
||||||
ident,
|
|
||||||
impl_block,
|
impl_block,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -477,17 +473,19 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = Match(match cattrs.tag() {
|
let body = Match(match (cattrs.tag(), variant.attrs.untagged()) {
|
||||||
attr::TagType::External => {
|
(attr::TagType::External, false) => {
|
||||||
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
|
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
|
||||||
}
|
}
|
||||||
attr::TagType::Internal { tag } => {
|
(attr::TagType::Internal { tag }, false) => {
|
||||||
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
||||||
}
|
}
|
||||||
attr::TagType::Adjacent { tag, content } => {
|
(attr::TagType::Adjacent { tag, content }, false) => {
|
||||||
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
|
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
|
||||||
}
|
}
|
||||||
attr::TagType::None => serialize_untagged_variant(params, variant, cattrs),
|
(attr::TagType::None, _) | (_, true) => {
|
||||||
|
serialize_untagged_variant(params, variant, cattrs)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
@@ -719,6 +717,7 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __AdjacentlyTagged #wrapper_generics #where_clause {
|
struct __AdjacentlyTagged #wrapper_generics #where_clause {
|
||||||
data: (#(&'__a #fields_ty,)*),
|
data: (#(&'__a #fields_ty,)*),
|
||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
@@ -982,6 +981,7 @@ fn serialize_struct_variant_with_flatten(
|
|||||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
struct __EnumFlatten #wrapper_generics #where_clause {
|
struct __EnumFlatten #wrapper_generics #where_clause {
|
||||||
data: (#(&'__a #fields_ty,)*),
|
data: (#(&'__a #fields_ty,)*),
|
||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
@@ -1212,6 +1212,7 @@ fn wrap_serialize_with(
|
|||||||
});
|
});
|
||||||
|
|
||||||
quote!({
|
quote!({
|
||||||
|
#[doc(hidden)]
|
||||||
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
||||||
values: (#(&'__a #field_tys, )*),
|
values: (#(&'__a #field_tys, )*),
|
||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
msrv = "1.31.0"
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive_internals"
|
name = "serde_derive_internals"
|
||||||
version = "0.26.0" # remember to update html_root_url
|
version = "0.28.0" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
description = "AST representation used by Serde derive macros. Unstable."
|
description = "AST representation used by Serde derive macros. Unstable."
|
||||||
documentation = "https://docs.rs/serde_derive_internals"
|
documentation = "https://docs.rs/serde_derive_internals"
|
||||||
@@ -9,7 +9,7 @@ include = ["lib.rs", "src/**/*.rs", "LICENSE-APACHE", "LICENSE-MIT"]
|
|||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
rust-version = "1.31"
|
rust-version = "1.56"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
@@ -17,7 +17,7 @@ path = "lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
syn = { version = "1.0.104", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
|
syn = { version = "2.0.21", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.26.0")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.28.0")]
|
||||||
#![allow(unknown_lints, bare_trait_objects)]
|
#![allow(unknown_lints, bare_trait_objects)]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
clippy::module_name_repetitions,
|
clippy::module_name_repetitions,
|
||||||
clippy::must_use_candidate,
|
clippy::must_use_candidate,
|
||||||
clippy::similar_names,
|
clippy::similar_names,
|
||||||
|
clippy::single_match_else,
|
||||||
clippy::struct_excessive_bools,
|
clippy::struct_excessive_bools,
|
||||||
clippy::too_many_lines,
|
clippy::too_many_lines,
|
||||||
clippy::unused_self,
|
clippy::unused_self,
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
msrv = "1.13.0"
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_test"
|
name = "serde_test"
|
||||||
version = "1.0.154" # remember to update html_root_url
|
version = "1.0.165" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["development-tools::testing"]
|
categories = ["development-tools::testing"]
|
||||||
|
|||||||
+83
-95
@@ -12,32 +12,29 @@ pub struct Deserializer<'de> {
|
|||||||
tokens: &'de [Token],
|
tokens: &'de [Token],
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! assert_next_token {
|
fn assert_next_token(de: &mut Deserializer, expected: Token) -> Result<(), Error> {
|
||||||
($de:expr, $expected:expr) => {
|
match de.next_token_opt() {
|
||||||
match $de.next_token_opt() {
|
Some(token) if token == expected => Ok(()),
|
||||||
Some(token) if token == $expected => {}
|
Some(other) => Err(de::Error::custom(format!(
|
||||||
Some(other) => panic!(
|
"expected Token::{} but deserialization wants Token::{}",
|
||||||
"expected Token::{} but deserialization wants Token::{}",
|
other, expected,
|
||||||
other, $expected
|
))),
|
||||||
),
|
None => Err(de::Error::custom(format!(
|
||||||
None => panic!(
|
"end of tokens but deserialization wants Token::{}",
|
||||||
"end of tokens but deserialization wants Token::{}",
|
expected,
|
||||||
$expected
|
))),
|
||||||
),
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! unexpected {
|
fn unexpected(token: Token) -> Error {
|
||||||
($token:expr) => {
|
de::Error::custom(format!(
|
||||||
panic!("deserialization did not expect this token: {}", $token)
|
"deserialization did not expect this token: {}",
|
||||||
};
|
token,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! end_of_tokens {
|
fn end_of_tokens() -> Error {
|
||||||
() => {
|
de::Error::custom("ran out of tokens to deserialize")
|
||||||
panic!("ran out of tokens to deserialize")
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> Deserializer<'de> {
|
impl<'de> Deserializer<'de> {
|
||||||
@@ -49,11 +46,8 @@ impl<'de> Deserializer<'de> {
|
|||||||
self.tokens.first().cloned()
|
self.tokens.first().cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn peek_token(&self) -> Token {
|
fn peek_token(&self) -> Result<Token, Error> {
|
||||||
match self.peek_token_opt() {
|
self.peek_token_opt().ok_or_else(end_of_tokens)
|
||||||
Some(token) => token,
|
|
||||||
None => end_of_tokens!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_token_opt(&mut self) -> Option<Token> {
|
pub fn next_token_opt(&mut self) -> Option<Token> {
|
||||||
@@ -66,14 +60,10 @@ impl<'de> Deserializer<'de> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_token(&mut self) -> Token {
|
fn next_token(&mut self) -> Result<Token, Error> {
|
||||||
match self.tokens.split_first() {
|
let (&first, rest) = self.tokens.split_first().ok_or_else(end_of_tokens)?;
|
||||||
Some((&first, rest)) => {
|
self.tokens = rest;
|
||||||
self.tokens = rest;
|
Ok(first)
|
||||||
first
|
|
||||||
}
|
|
||||||
None => end_of_tokens!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remaining(&self) -> usize {
|
pub fn remaining(&self) -> usize {
|
||||||
@@ -94,7 +84,7 @@ impl<'de> Deserializer<'de> {
|
|||||||
len: len,
|
len: len,
|
||||||
end: end,
|
end: end,
|
||||||
})?;
|
})?;
|
||||||
assert_next_token!(self, end);
|
assert_next_token(self, end)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +102,7 @@ impl<'de> Deserializer<'de> {
|
|||||||
len: len,
|
len: len,
|
||||||
end: end,
|
end: end,
|
||||||
})?;
|
})?;
|
||||||
assert_next_token!(self, end);
|
assert_next_token(self, end)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,7 +119,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
let token = self.next_token();
|
let token = self.next_token()?;
|
||||||
match token {
|
match token {
|
||||||
Token::Bool(v) => visitor.visit_bool(v),
|
Token::Bool(v) => visitor.visit_bool(v),
|
||||||
Token::I8(v) => visitor.visit_i8(v),
|
Token::I8(v) => visitor.visit_i8(v),
|
||||||
@@ -161,50 +151,50 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
Token::Map { len } => self.visit_map(len, Token::MapEnd, visitor),
|
Token::Map { len } => self.visit_map(len, Token::MapEnd, visitor),
|
||||||
Token::Struct { len, .. } => self.visit_map(Some(len), Token::StructEnd, visitor),
|
Token::Struct { len, .. } => self.visit_map(Some(len), Token::StructEnd, visitor),
|
||||||
Token::Enum { .. } => {
|
Token::Enum { .. } => {
|
||||||
let variant = self.next_token();
|
let variant = self.next_token()?;
|
||||||
let next = self.peek_token();
|
let next = self.peek_token()?;
|
||||||
match (variant, next) {
|
match (variant, next) {
|
||||||
(Token::Str(variant), Token::Unit) => {
|
(Token::Str(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_str(variant)
|
visitor.visit_str(variant)
|
||||||
}
|
}
|
||||||
(Token::BorrowedStr(variant), Token::Unit) => {
|
(Token::BorrowedStr(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_borrowed_str(variant)
|
visitor.visit_borrowed_str(variant)
|
||||||
}
|
}
|
||||||
(Token::String(variant), Token::Unit) => {
|
(Token::String(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_string(variant.to_string())
|
visitor.visit_string(variant.to_string())
|
||||||
}
|
}
|
||||||
(Token::Bytes(variant), Token::Unit) => {
|
(Token::Bytes(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_bytes(variant)
|
visitor.visit_bytes(variant)
|
||||||
}
|
}
|
||||||
(Token::BorrowedBytes(variant), Token::Unit) => {
|
(Token::BorrowedBytes(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_borrowed_bytes(variant)
|
visitor.visit_borrowed_bytes(variant)
|
||||||
}
|
}
|
||||||
(Token::ByteBuf(variant), Token::Unit) => {
|
(Token::ByteBuf(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_byte_buf(variant.to_vec())
|
visitor.visit_byte_buf(variant.to_vec())
|
||||||
}
|
}
|
||||||
(Token::U8(variant), Token::Unit) => {
|
(Token::U8(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_u8(variant)
|
visitor.visit_u8(variant)
|
||||||
}
|
}
|
||||||
(Token::U16(variant), Token::Unit) => {
|
(Token::U16(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_u16(variant)
|
visitor.visit_u16(variant)
|
||||||
}
|
}
|
||||||
(Token::U32(variant), Token::Unit) => {
|
(Token::U32(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_u32(variant)
|
visitor.visit_u32(variant)
|
||||||
}
|
}
|
||||||
(Token::U64(variant), Token::Unit) => {
|
(Token::U64(variant), Token::Unit) => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_u64(variant)
|
visitor.visit_u64(variant)
|
||||||
}
|
}
|
||||||
(variant, Token::Unit) => unexpected!(variant),
|
(variant, Token::Unit) => Err(unexpected(variant)),
|
||||||
(variant, _) => {
|
(variant, _) => {
|
||||||
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
|
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
|
||||||
}
|
}
|
||||||
@@ -232,9 +222,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
| Token::MapEnd
|
| Token::MapEnd
|
||||||
| Token::StructEnd
|
| Token::StructEnd
|
||||||
| Token::TupleVariantEnd
|
| Token::TupleVariantEnd
|
||||||
| Token::StructVariantEnd => {
|
| Token::StructVariantEnd => Err(unexpected(token)),
|
||||||
unexpected!(token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,13 +230,13 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::Unit | Token::None => {
|
Token::Unit | Token::None => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_none()
|
visitor.visit_none()
|
||||||
}
|
}
|
||||||
Token::Some => {
|
Token::Some => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_some(self)
|
visitor.visit_some(self)
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -264,9 +252,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::Enum { name: n } if name == n => {
|
Token::Enum { name: n } if name == n => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
|
|
||||||
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||||
}
|
}
|
||||||
@@ -286,9 +274,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::UnitStruct { .. } => {
|
Token::UnitStruct { .. } => {
|
||||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
assert_next_token(self, Token::UnitStruct { name: name })?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -303,9 +291,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::NewtypeStruct { .. } => {
|
Token::NewtypeStruct { .. } => {
|
||||||
assert_next_token!(self, Token::NewtypeStruct { name: name });
|
assert_next_token(self, Token::NewtypeStruct { name: name })?;
|
||||||
visitor.visit_newtype_struct(self)
|
visitor.visit_newtype_struct(self)
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -316,21 +304,21 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::Unit | Token::UnitStruct { .. } => {
|
Token::Unit | Token::UnitStruct { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
Token::Seq { .. } => {
|
Token::Seq { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||||
}
|
}
|
||||||
Token::Tuple { .. } => {
|
Token::Tuple { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||||
}
|
}
|
||||||
Token::TupleStruct { .. } => {
|
Token::TupleStruct { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -346,25 +334,25 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::Unit => {
|
Token::Unit => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
Token::UnitStruct { .. } => {
|
Token::UnitStruct { .. } => {
|
||||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
assert_next_token(self, Token::UnitStruct { name: name })?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
}
|
}
|
||||||
Token::Seq { .. } => {
|
Token::Seq { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||||
}
|
}
|
||||||
Token::Tuple { .. } => {
|
Token::Tuple { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||||
}
|
}
|
||||||
Token::TupleStruct { len: n, .. } => {
|
Token::TupleStruct { len: n, .. } => {
|
||||||
assert_next_token!(self, Token::TupleStruct { name: name, len: n });
|
assert_next_token(self, Token::TupleStruct { name: name, len: n })?;
|
||||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -380,13 +368,13 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.peek_token() {
|
match self.peek_token()? {
|
||||||
Token::Struct { len: n, .. } => {
|
Token::Struct { len: n, .. } => {
|
||||||
assert_next_token!(self, Token::Struct { name: name, len: n });
|
assert_next_token(self, Token::Struct { name: name, len: n })?;
|
||||||
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
|
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
|
||||||
}
|
}
|
||||||
Token::Map { .. } => {
|
Token::Map { .. } => {
|
||||||
self.next_token();
|
self.next_token()?;
|
||||||
self.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
self.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||||
}
|
}
|
||||||
_ => self.deserialize_any(visitor),
|
_ => self.deserialize_any(visitor),
|
||||||
@@ -476,7 +464,7 @@ impl<'de, 'a> EnumAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
|||||||
where
|
where
|
||||||
V: DeserializeSeed<'de>,
|
V: DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
match self.de.peek_token() {
|
match self.de.peek_token()? {
|
||||||
Token::UnitVariant { variant: v, .. }
|
Token::UnitVariant { variant: v, .. }
|
||||||
| Token::NewtypeVariant { variant: v, .. }
|
| Token::NewtypeVariant { variant: v, .. }
|
||||||
| Token::TupleVariant { variant: v, .. }
|
| Token::TupleVariant { variant: v, .. }
|
||||||
@@ -497,9 +485,9 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn unit_variant(self) -> Result<(), Error> {
|
fn unit_variant(self) -> Result<(), Error> {
|
||||||
match self.de.peek_token() {
|
match self.de.peek_token()? {
|
||||||
Token::UnitVariant { .. } => {
|
Token::UnitVariant { .. } => {
|
||||||
self.de.next_token();
|
self.de.next_token()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
_ => Deserialize::deserialize(self.de),
|
_ => Deserialize::deserialize(self.de),
|
||||||
@@ -510,9 +498,9 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
|||||||
where
|
where
|
||||||
T: DeserializeSeed<'de>,
|
T: DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
match self.de.peek_token() {
|
match self.de.peek_token()? {
|
||||||
Token::NewtypeVariant { .. } => {
|
Token::NewtypeVariant { .. } => {
|
||||||
self.de.next_token();
|
self.de.next_token()?;
|
||||||
seed.deserialize(self.de)
|
seed.deserialize(self.de)
|
||||||
}
|
}
|
||||||
_ => seed.deserialize(self.de),
|
_ => seed.deserialize(self.de),
|
||||||
@@ -523,26 +511,26 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.de.peek_token() {
|
match self.de.peek_token()? {
|
||||||
Token::TupleVariant { len: enum_len, .. } => {
|
Token::TupleVariant { len: enum_len, .. } => {
|
||||||
let token = self.de.next_token();
|
let token = self.de.next_token()?;
|
||||||
|
|
||||||
if len == enum_len {
|
if len == enum_len {
|
||||||
self.de
|
self.de
|
||||||
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
||||||
} else {
|
} else {
|
||||||
unexpected!(token);
|
Err(unexpected(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token::Seq {
|
Token::Seq {
|
||||||
len: Some(enum_len),
|
len: Some(enum_len),
|
||||||
} => {
|
} => {
|
||||||
let token = self.de.next_token();
|
let token = self.de.next_token()?;
|
||||||
|
|
||||||
if len == enum_len {
|
if len == enum_len {
|
||||||
self.de.visit_seq(Some(len), Token::SeqEnd, visitor)
|
self.de.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||||
} else {
|
} else {
|
||||||
unexpected!(token);
|
Err(unexpected(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||||
@@ -557,27 +545,27 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>,
|
V: Visitor<'de>,
|
||||||
{
|
{
|
||||||
match self.de.peek_token() {
|
match self.de.peek_token()? {
|
||||||
Token::StructVariant { len: enum_len, .. } => {
|
Token::StructVariant { len: enum_len, .. } => {
|
||||||
let token = self.de.next_token();
|
let token = self.de.next_token()?;
|
||||||
|
|
||||||
if fields.len() == enum_len {
|
if fields.len() == enum_len {
|
||||||
self.de
|
self.de
|
||||||
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
|
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
|
||||||
} else {
|
} else {
|
||||||
unexpected!(token);
|
Err(unexpected(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token::Map {
|
Token::Map {
|
||||||
len: Some(enum_len),
|
len: Some(enum_len),
|
||||||
} => {
|
} => {
|
||||||
let token = self.de.next_token();
|
let token = self.de.next_token()?;
|
||||||
|
|
||||||
if fields.len() == enum_len {
|
if fields.len() == enum_len {
|
||||||
self.de
|
self.de
|
||||||
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||||
} else {
|
} else {
|
||||||
unexpected!(token);
|
Err(unexpected(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||||
@@ -622,7 +610,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
|||||||
.deserialize(BytesDeserializer { value: variant })
|
.deserialize(BytesDeserializer { value: variant })
|
||||||
.map(Some),
|
.map(Some),
|
||||||
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||||
Some(other) => unexpected!(other),
|
Some(other) => Err(unexpected(other)),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -641,7 +629,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
|||||||
};
|
};
|
||||||
seed.deserialize(SeqAccessDeserializer::new(visitor))?
|
seed.deserialize(SeqAccessDeserializer::new(visitor))?
|
||||||
};
|
};
|
||||||
assert_next_token!(self.de, Token::TupleVariantEnd);
|
assert_next_token(self.de, Token::TupleVariantEnd)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
EnumFormat::Map => {
|
EnumFormat::Map => {
|
||||||
@@ -653,7 +641,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
|||||||
};
|
};
|
||||||
seed.deserialize(MapAccessDeserializer::new(visitor))?
|
seed.deserialize(MapAccessDeserializer::new(visitor))?
|
||||||
};
|
};
|
||||||
assert_next_token!(self.de, Token::StructVariantEnd);
|
assert_next_token(self.de, Token::StructVariantEnd)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
EnumFormat::Any => seed.deserialize(&mut *self.de),
|
EnumFormat::Any => seed.deserialize(&mut *self.de),
|
||||||
|
|||||||
@@ -140,7 +140,7 @@
|
|||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.154")]
|
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.165")]
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
|
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
|
||||||
@@ -182,7 +182,3 @@ pub use assert::{
|
|||||||
pub use token::Token;
|
pub use token::Token;
|
||||||
|
|
||||||
pub use configure::{Compact, Configure, Readable};
|
pub use configure::{Compact, Configure, Readable};
|
||||||
|
|
||||||
// Not public API.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use de::Deserializer;
|
|
||||||
|
|||||||
@@ -63,14 +63,12 @@ macro_rules! assert_next_token {
|
|||||||
($ser:expr, $actual:expr, $pat:pat, $guard:expr) => {
|
($ser:expr, $actual:expr, $pat:pat, $guard:expr) => {
|
||||||
match $ser.next_token() {
|
match $ser.next_token() {
|
||||||
Some($pat) if $guard => {}
|
Some($pat) if $guard => {}
|
||||||
Some(expected) => {
|
Some(expected) => return Err(ser::Error::custom(
|
||||||
panic!("expected Token::{} but serialized as {}",
|
format!("expected Token::{} but serialized as {}", expected, $actual)
|
||||||
expected, $actual);
|
)),
|
||||||
}
|
None => return Err(ser::Error::custom(
|
||||||
None => {
|
format!("expected end of tokens, but {} was serialized", $actual)
|
||||||
panic!("expected end of tokens, but {} was serialized",
|
)),
|
||||||
$actual);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ unstable = ["serde/unstable"]
|
|||||||
serde = { path = "../serde" }
|
serde = { path = "../serde" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
automod = "1.0"
|
||||||
fnv = "1.0"
|
fnv = "1.0"
|
||||||
rustversion = "1.0"
|
rustversion = "1.0"
|
||||||
serde = { path = "../serde", features = ["rc", "derive"] }
|
serde = { path = "../serde", features = ["rc", "derive"] }
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
mod regression {
|
||||||
|
automod::dir!("tests/regression");
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Nested;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub enum ExternallyTagged {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
string: &'static str,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(tag = "tag")]
|
||||||
|
pub enum InternallyTagged {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
string: &'static str,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(tag = "tag", content = "content")]
|
||||||
|
pub enum AdjacentlyTagged {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
string: &'static str,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum UntaggedWorkaround {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
string: &'static str,
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
macro_rules! bug {
|
||||||
|
($serde_path:literal) => {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(crate = $serde_path)]
|
||||||
|
pub struct Struct;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
bug!("serde");
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde()]
|
||||||
|
pub struct S;
|
||||||
@@ -1533,7 +1533,7 @@ fn test_invalid_length_enum() {
|
|||||||
Token::TupleVariant {
|
Token::TupleVariant {
|
||||||
name: "InvalidLengthEnum",
|
name: "InvalidLengthEnum",
|
||||||
variant: "B",
|
variant: "B",
|
||||||
len: 3,
|
len: 2,
|
||||||
},
|
},
|
||||||
Token::I32(1),
|
Token::I32(1),
|
||||||
Token::TupleVariantEnd,
|
Token::TupleVariantEnd,
|
||||||
@@ -2317,6 +2317,53 @@ fn test_internally_tagged_enum_new_type_with_unit() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_adjacently_tagged_enum_bytes() {
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
#[serde(tag = "t", content = "c")]
|
||||||
|
enum Data {
|
||||||
|
A { a: i32 },
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = Data::A { a: 0 };
|
||||||
|
|
||||||
|
assert_tokens(
|
||||||
|
&data,
|
||||||
|
&[
|
||||||
|
Token::Struct {
|
||||||
|
name: "Data",
|
||||||
|
len: 2,
|
||||||
|
},
|
||||||
|
Token::Str("t"),
|
||||||
|
Token::Str("A"),
|
||||||
|
Token::Str("c"),
|
||||||
|
Token::Struct { name: "A", len: 1 },
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(0),
|
||||||
|
Token::StructEnd,
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_de_tokens(
|
||||||
|
&data,
|
||||||
|
&[
|
||||||
|
Token::Struct {
|
||||||
|
name: "Data",
|
||||||
|
len: 2,
|
||||||
|
},
|
||||||
|
Token::Bytes(b"t"),
|
||||||
|
Token::Str("A"),
|
||||||
|
Token::Bytes(b"c"),
|
||||||
|
Token::Struct { name: "A", len: 1 },
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(0),
|
||||||
|
Token::StructEnd,
|
||||||
|
Token::StructEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_adjacently_tagged_enum_containing_flatten() {
|
fn test_adjacently_tagged_enum_containing_flatten() {
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
@@ -2395,6 +2442,162 @@ fn test_untagged_enum_containing_flatten() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_partially_untagged_enum() {
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
enum Exp {
|
||||||
|
Lambda(u32, Box<Exp>),
|
||||||
|
#[serde(untagged)]
|
||||||
|
App(Box<Exp>, Box<Exp>),
|
||||||
|
#[serde(untagged)]
|
||||||
|
Var(u32),
|
||||||
|
}
|
||||||
|
use Exp::*;
|
||||||
|
|
||||||
|
let data = Lambda(0, Box::new(App(Box::new(Var(0)), Box::new(Var(0)))));
|
||||||
|
assert_tokens(
|
||||||
|
&data,
|
||||||
|
&[
|
||||||
|
Token::TupleVariant {
|
||||||
|
name: "Exp",
|
||||||
|
variant: "Lambda",
|
||||||
|
len: 2,
|
||||||
|
},
|
||||||
|
Token::U32(0),
|
||||||
|
Token::Tuple { len: 2 },
|
||||||
|
Token::U32(0),
|
||||||
|
Token::U32(0),
|
||||||
|
Token::TupleEnd,
|
||||||
|
Token::TupleVariantEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_partially_untagged_enum_generic() {
|
||||||
|
trait Trait<T> {
|
||||||
|
type Assoc;
|
||||||
|
type Assoc2;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
enum E<A, B, C>
|
||||||
|
where
|
||||||
|
A: Trait<C, Assoc2 = B>,
|
||||||
|
{
|
||||||
|
A(A::Assoc),
|
||||||
|
#[serde(untagged)]
|
||||||
|
B(A::Assoc2),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Trait<T> for () {
|
||||||
|
type Assoc = T;
|
||||||
|
type Assoc2 = bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyE = E<(), bool, u32>;
|
||||||
|
use E::*;
|
||||||
|
|
||||||
|
assert_tokens::<MyE>(&B(true), &[Token::Bool(true)]);
|
||||||
|
|
||||||
|
assert_tokens::<MyE>(
|
||||||
|
&A(5),
|
||||||
|
&[
|
||||||
|
Token::NewtypeVariant {
|
||||||
|
name: "E",
|
||||||
|
variant: "A",
|
||||||
|
},
|
||||||
|
Token::U32(5),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_partially_untagged_enum_desugared() {
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
enum Test {
|
||||||
|
A(u32, u32),
|
||||||
|
B(u32),
|
||||||
|
#[serde(untagged)]
|
||||||
|
C(u32),
|
||||||
|
#[serde(untagged)]
|
||||||
|
D(u32, u32),
|
||||||
|
}
|
||||||
|
use Test::*;
|
||||||
|
|
||||||
|
mod desugared {
|
||||||
|
use super::*;
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
pub(super) enum Test {
|
||||||
|
A(u32, u32),
|
||||||
|
B(u32),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
use desugared::Test as TestTagged;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum TestUntagged {
|
||||||
|
Tagged(TestTagged),
|
||||||
|
C(u32),
|
||||||
|
D(u32, u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Test> for TestUntagged {
|
||||||
|
fn from(test: Test) -> Self {
|
||||||
|
match test {
|
||||||
|
A(x, y) => TestUntagged::Tagged(TestTagged::A(x, y)),
|
||||||
|
B(x) => TestUntagged::Tagged(TestTagged::B(x)),
|
||||||
|
C(x) => TestUntagged::C(x),
|
||||||
|
D(x, y) => TestUntagged::D(x, y),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_tokens_desugared(value: Test, tokens: &[Token]) {
|
||||||
|
assert_tokens(&value, tokens);
|
||||||
|
let desugared: TestUntagged = value.into();
|
||||||
|
assert_tokens(&desugared, tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_tokens_desugared(
|
||||||
|
A(0, 1),
|
||||||
|
&[
|
||||||
|
Token::TupleVariant {
|
||||||
|
name: "Test",
|
||||||
|
variant: "A",
|
||||||
|
len: 2,
|
||||||
|
},
|
||||||
|
Token::U32(0),
|
||||||
|
Token::U32(1),
|
||||||
|
Token::TupleVariantEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_tokens_desugared(
|
||||||
|
B(1),
|
||||||
|
&[
|
||||||
|
Token::NewtypeVariant {
|
||||||
|
name: "Test",
|
||||||
|
variant: "B",
|
||||||
|
},
|
||||||
|
Token::U32(1),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_tokens_desugared(C(2), &[Token::U32(2)]);
|
||||||
|
|
||||||
|
assert_tokens_desugared(
|
||||||
|
D(3, 5),
|
||||||
|
&[
|
||||||
|
Token::Tuple { len: 2 },
|
||||||
|
Token::U32(3),
|
||||||
|
Token::U32(5),
|
||||||
|
Token::TupleEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_flatten_untagged_enum() {
|
fn test_flatten_untagged_enum() {
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
clippy::used_underscore_binding
|
clippy::used_underscore_binding
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
use serde::de::value::{BorrowedStrDeserializer, MapDeserializer};
|
||||||
|
use serde::de::IntoDeserializer;
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
|
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
|
||||||
|
|
||||||
@@ -130,20 +132,22 @@ fn test_cow() {
|
|||||||
borrowed: Cow<'b, str>,
|
borrowed: Cow<'b, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let tokens = &[
|
struct BorrowedStr(&'static str);
|
||||||
Token::Struct {
|
|
||||||
name: "Cows",
|
|
||||||
len: 2,
|
|
||||||
},
|
|
||||||
Token::Str("copied"),
|
|
||||||
Token::BorrowedStr("copied"),
|
|
||||||
Token::Str("borrowed"),
|
|
||||||
Token::BorrowedStr("borrowed"),
|
|
||||||
Token::StructEnd,
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut de = serde_test::Deserializer::new(tokens);
|
impl<'de> IntoDeserializer<'de> for BorrowedStr {
|
||||||
let cows = Cows::deserialize(&mut de).unwrap();
|
type Deserializer = BorrowedStrDeserializer<'de, serde::de::value::Error>;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self::Deserializer {
|
||||||
|
BorrowedStrDeserializer::new(self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let de = MapDeserializer::new(IntoIterator::into_iter([
|
||||||
|
("copied", BorrowedStr("copied")),
|
||||||
|
("borrowed", BorrowedStr("borrowed")),
|
||||||
|
]));
|
||||||
|
|
||||||
|
let cows = Cows::deserialize(de).unwrap();
|
||||||
|
|
||||||
match cows.copied {
|
match cows.copied {
|
||||||
Cow::Owned(ref s) if s == "copied" => {}
|
Cow::Owned(ref s) if s == "copied" => {}
|
||||||
|
|||||||
+19
-24
@@ -14,6 +14,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
|||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::ffi::{CStr, CString, OsString};
|
use std::ffi::{CStr, CString, OsString};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
use std::iter;
|
||||||
use std::net;
|
use std::net;
|
||||||
use std::num::{
|
use std::num::{
|
||||||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
||||||
@@ -33,7 +34,7 @@ use std::time::{Duration, UNIX_EPOCH};
|
|||||||
use std::sync::atomic::{AtomicI64, AtomicU64};
|
use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||||
|
|
||||||
use fnv::FnvHasher;
|
use fnv::FnvHasher;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::{DeserializeOwned, IntoDeserializer};
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use serde_test::{assert_de_tokens, Configure, Token};
|
use serde_test::{assert_de_tokens, Configure, Token};
|
||||||
|
|
||||||
@@ -199,12 +200,11 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
|
|||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(ignorable_tokens.iter().copied())
|
.chain(ignorable_tokens.iter().copied())
|
||||||
.chain(vec![Token::MapEnd].into_iter())
|
.chain(iter::once(Token::MapEnd))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut de = serde_test::Deserializer::new(&concated_tokens);
|
let expected = IgnoreBase { a: 1 };
|
||||||
let base = IgnoreBase::deserialize(&mut de).unwrap();
|
assert_de_tokens(&expected, &concated_tokens);
|
||||||
assert_eq!(base, IgnoreBase { a: 1 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@@ -2245,39 +2245,34 @@ fn test_cstr() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_atomics() {
|
fn test_atomics() {
|
||||||
fn test<L, A, T>(load: L, val: T, token: Token)
|
fn test<L, A, T>(load: L, val: T)
|
||||||
where
|
where
|
||||||
L: Fn(&A, Ordering) -> T,
|
L: Fn(&A, Ordering) -> T,
|
||||||
A: DeserializeOwned,
|
A: DeserializeOwned,
|
||||||
T: PartialEq + Debug,
|
T: PartialEq + Debug + Copy + for<'de> IntoDeserializer<'de>,
|
||||||
{
|
{
|
||||||
let tokens = &[token];
|
match A::deserialize(val.into_deserializer()) {
|
||||||
let mut de = serde_test::Deserializer::new(tokens);
|
|
||||||
match A::deserialize(&mut de) {
|
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
let loaded = load(&v, Ordering::Relaxed);
|
let loaded = load(&v, Ordering::Relaxed);
|
||||||
assert_eq!(val, loaded);
|
assert_eq!(val, loaded);
|
||||||
}
|
}
|
||||||
Err(e) => panic!("tokens failed to deserialize: {}", e),
|
Err(e) => panic!("tokens failed to deserialize: {}", e),
|
||||||
};
|
|
||||||
if de.remaining() > 0 {
|
|
||||||
panic!("{} remaining tokens", de.remaining());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test(AtomicBool::load, true, Token::Bool(true));
|
test(AtomicBool::load, true);
|
||||||
test(AtomicI8::load, -127, Token::I8(-127i8));
|
test(AtomicI8::load, -127i8);
|
||||||
test(AtomicI16::load, -510, Token::I16(-510i16));
|
test(AtomicI16::load, -510i16);
|
||||||
test(AtomicI32::load, -131072, Token::I32(-131072i32));
|
test(AtomicI32::load, -131072i32);
|
||||||
test(AtomicIsize::load, -131072isize, Token::I32(-131072));
|
test(AtomicIsize::load, -131072isize);
|
||||||
test(AtomicU8::load, 127, Token::U8(127u8));
|
test(AtomicU8::load, 127u8);
|
||||||
test(AtomicU16::load, 510u16, Token::U16(510u16));
|
test(AtomicU16::load, 510u16);
|
||||||
test(AtomicU32::load, 131072u32, Token::U32(131072u32));
|
test(AtomicU32::load, 131072u32);
|
||||||
test(AtomicUsize::load, 131072usize, Token::U32(131072));
|
test(AtomicUsize::load, 131072usize);
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
{
|
{
|
||||||
test(AtomicI64::load, -8589934592, Token::I64(-8589934592));
|
test(AtomicI64::load, -8589934592i64);
|
||||||
test(AtomicU64::load, 8589934592u64, Token::U64(8589934592));
|
test(AtomicU64::load, 8589934592u64);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -257,6 +257,16 @@ fn test_gen() {
|
|||||||
}
|
}
|
||||||
assert::<VariantWithTraits2<X, X>>();
|
assert::<VariantWithTraits2<X, X>>();
|
||||||
|
|
||||||
|
type PhantomDataAlias<T> = PhantomData<T>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(bound = "")]
|
||||||
|
struct PhantomDataWrapper<T> {
|
||||||
|
#[serde(default)]
|
||||||
|
field: PhantomDataAlias<T>,
|
||||||
|
}
|
||||||
|
assert::<PhantomDataWrapper<X>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct CowStr<'a>(Cow<'a, str>);
|
struct CowStr<'a>(Cow<'a, str>);
|
||||||
assert::<CowStr>();
|
assert::<CowStr>();
|
||||||
@@ -892,53 +902,3 @@ pub struct RemotePackedNonCopyDef {
|
|||||||
impl Drop for RemotePackedNonCopyDef {
|
impl Drop for RemotePackedNonCopyDef {
|
||||||
fn drop(&mut self) {}
|
fn drop(&mut self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/// Regression tests for <https://github.com/serde-rs/serde/issues/2371>
|
|
||||||
#[allow(dead_code)]
|
|
||||||
mod static_and_flatten {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Nested;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
enum ExternallyTagged {
|
|
||||||
Flatten {
|
|
||||||
#[serde(flatten)]
|
|
||||||
nested: Nested,
|
|
||||||
string: &'static str,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(tag = "tag")]
|
|
||||||
enum InternallyTagged {
|
|
||||||
Flatten {
|
|
||||||
#[serde(flatten)]
|
|
||||||
nested: Nested,
|
|
||||||
string: &'static str,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(tag = "tag", content = "content")]
|
|
||||||
enum AdjacentlyTagged {
|
|
||||||
Flatten {
|
|
||||||
#[serde(flatten)]
|
|
||||||
nested: Nested,
|
|
||||||
string: &'static str,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
enum UntaggedWorkaround {
|
|
||||||
Flatten {
|
|
||||||
#[serde(flatten)]
|
|
||||||
nested: Nested,
|
|
||||||
string: &'static str,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
error: duplicate serde attribute `borrow`
|
error: duplicate serde attribute `borrow`
|
||||||
--> tests/ui/borrow/duplicate_variant.rs:8:13
|
--> tests/ui/borrow/duplicate_variant.rs:9:15
|
||||||
|
|
|
|
||||||
8 | #[serde(borrow)]
|
9 | S(#[serde(borrow)] Str<'a>),
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ use serde_derive::Deserialize;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Test<'a> {
|
struct Test<'a> {
|
||||||
#[serde(borrow = "")]
|
#[serde(borrow = "")]
|
||||||
|
r: &'a str,
|
||||||
|
#[serde(borrow = " ")]
|
||||||
s: &'a str,
|
s: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,3 +3,9 @@ error: at least one lifetime must be borrowed
|
|||||||
|
|
|
|
||||||
5 | #[serde(borrow = "")]
|
5 | #[serde(borrow = "")]
|
||||||
| ^^
|
| ^^
|
||||||
|
|
||||||
|
error: at least one lifetime must be borrowed
|
||||||
|
--> tests/ui/borrow/empty_lifetimes.rs:7:22
|
||||||
|
|
|
||||||
|
7 | #[serde(borrow = " ")]
|
||||||
|
| ^^^^
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
enum E {
|
||||||
|
#[serde(untagged)]
|
||||||
|
A(u8),
|
||||||
|
B(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
error: all variants with the #[serde(untagged)] attribute must be placed at the end of the enum
|
||||||
|
--> tests/ui/enum-representation/partially_tagged_wrong_order.rs:6:5
|
||||||
|
|
|
||||||
|
6 | A(u8),
|
||||||
|
| ^
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
error: unexpected end of input, expected literal
|
error: unexpected end of input, expected an expression
|
||||||
--> tests/ui/malformed/cut_off.rs:4:17
|
--> tests/ui/malformed/cut_off.rs:4:17
|
||||||
|
|
|
|
||||||
4 | #[serde(rename =)]
|
4 | #[serde(rename =)]
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
error: expected #[serde(...)]
|
error: expected attribute arguments in parentheses: #[serde(...)]
|
||||||
--> tests/ui/malformed/not_list.rs:4:3
|
--> tests/ui/malformed/not_list.rs:4:3
|
||||||
|
|
|
|
||||||
4 | #[serde]
|
4 | #[serde]
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
||||||
error: expected #[serde(...)]
|
error: expected parentheses: #[serde(...)]
|
||||||
--> tests/ui/malformed/not_list.rs:5:3
|
--> tests/ui/malformed/not_list.rs:5:9
|
||||||
|
|
|
|
||||||
5 | #[serde = "?"]
|
5 | #[serde = "?"]
|
||||||
| ^^^^^^^^^^^
|
| ^
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct S {
|
||||||
|
#[serde(skip_serializing_if, x.is_empty())]
|
||||||
|
x: Vec<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
error: expected `=`
|
||||||
|
--> tests/ui/malformed/trailing_expr.rs:5:32
|
||||||
|
|
|
||||||
|
5 | #[serde(skip_serializing_if, x.is_empty())]
|
||||||
|
| ^
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
error: unexpected literal in serde container attribute
|
error: unexpected literal in nested attribute, expected ident
|
||||||
--> tests/ui/unexpected-literal/container.rs:4:9
|
--> tests/ui/unexpected-literal/container.rs:4:9
|
||||||
|
|
|
|
||||||
4 | #[serde("literal")]
|
4 | #[serde("literal")]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: unexpected literal in serde field attribute
|
error: unexpected literal in nested attribute, expected ident
|
||||||
--> tests/ui/unexpected-literal/field.rs:5:13
|
--> tests/ui/unexpected-literal/field.rs:5:13
|
||||||
|
|
|
|
||||||
5 | #[serde("literal")]
|
5 | #[serde("literal")]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: unexpected literal in serde variant attribute
|
error: unexpected literal in nested attribute, expected ident
|
||||||
--> tests/ui/unexpected-literal/variant.rs:5:13
|
--> tests/ui/unexpected-literal/variant.rs:5:13
|
||||||
|
|
|
|
||||||
5 | #[serde("literal")]
|
5 | #[serde("literal")]
|
||||||
|
|||||||
Reference in New Issue
Block a user