diff options
author | Ivan Lozano <ivanlozano@google.com> | 2021-02-17 17:33:33 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-02-17 17:33:33 +0000 |
commit | a53bbc62fdfde18149e1b15b14e4b0a8694db1cf (patch) | |
tree | 9daccb425d4f9dbfad2a3f9e0fd1a05ed8f20e99 | |
parent | ccf8cefd6f6ffbd1a30294e0da418cc41855de53 (diff) | |
parent | 35944af006e33e16ef8320eae8712131ed4cb2ce (diff) | |
download | serde-a53bbc62fdfde18149e1b15b14e4b0a8694db1cf.tar.gz |
Merge "Upgrade rust/crates/serde to 1.0.123" am: 35944af006
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/serde/+/1581972
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: I9d044d752765254e28c0592020fb7fe4f9d14032
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 5 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | Cargo.toml.orig | 4 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | TEST_MAPPING | 8 | ||||
-rw-r--r-- | build.rs | 5 | ||||
-rw-r--r-- | src/de/from_primitive.rs | 260 | ||||
-rw-r--r-- | src/de/ignored_any.rs | 16 | ||||
-rw-r--r-- | src/de/impls.rs | 444 | ||||
-rw-r--r-- | src/de/mod.rs | 3 | ||||
-rw-r--r-- | src/de/seed.rs | 19 | ||||
-rw-r--r-- | src/de/value.rs | 192 | ||||
-rw-r--r-- | src/integer128.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/private/de.rs | 164 | ||||
-rw-r--r-- | src/private/doc.rs (renamed from src/private/macros.rs) | 45 | ||||
-rw-r--r-- | src/private/mod.rs | 9 | ||||
-rw-r--r-- | src/private/ser.rs | 28 | ||||
-rw-r--r-- | src/private/size_hint.rs | 21 | ||||
-rw-r--r-- | src/ser/impossible.rs | 2 | ||||
-rw-r--r-- | src/ser/mod.rs | 2 |
22 files changed, 611 insertions, 637 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index f5c327c..71f38ef 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "4c89cf89fdb84970c8d4e09b5f29507dc88c5547" + "sha1": "3d6c4149b177e9cadfb948ebc6d1e55b33861792" } } @@ -61,6 +61,7 @@ rust_library { "--cfg num_nonzero_signed", "--cfg ops_bound", "--cfg range_inclusive", + "--cfg serde_derive", "--cfg std_atomic", // "--cfg std_atomic64", // not for arm/arm64 ], @@ -70,6 +71,6 @@ rust_library { // dependent_library ["feature_list"] // proc-macro2-1.0.24 "default,proc-macro" // quote-1.0.8 "default,proc-macro" -// serde_derive-1.0.119 "default" -// syn-1.0.58 "clone-impls,default,derive,parsing,printing,proc-macro,quote,visit" +// serde_derive-1.0.123 "default" +// syn-1.0.60 "clone-impls,default,derive,parsing,printing,proc-macro,quote" // unicode-xid-0.2.1 "default" @@ -12,7 +12,7 @@ [package] name = "serde" -version = "1.0.119" +version = "1.0.123" authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"] build = "build.rs" include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] @@ -30,7 +30,7 @@ targets = ["x86_64-unknown-linux-gnu"] [package.metadata.playground] features = ["derive", "rc"] [dependencies.serde_derive] -version = "=1.0.119" +version = "=1.0.123" optional = true [dev-dependencies.serde_derive] version = "1.0" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 6ce9b1d..b777712 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "serde" -version = "1.0.119" # remember to update html_root_url and serde_derive dependency +version = "1.0.123" # remember to update html_root_url and serde_derive dependency authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"] license = "MIT OR Apache-2.0" description = "A generic serialization/deserialization framework" @@ -14,7 +14,7 @@ include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APAC build = "build.rs" [dependencies] -serde_derive = { version = "=1.0.119", optional = true, path = "../serde_derive" } +serde_derive = { version = "=1.0.123", optional = true, path = "../serde_derive" } [dev-dependencies] serde_derive = { version = "1.0", path = "../serde_derive" } @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/serde/serde-1.0.119.crate" + value: "https://static.crates.io/crates/serde/serde-1.0.123.crate" } - version: "1.0.119" + version: "1.0.123" license_type: NOTICE last_upgrade_date { year: 2021 - month: 1 - day: 11 + month: 2 + day: 9 } } diff --git a/TEST_MAPPING b/TEST_MAPPING index 270998f..bb118af 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,14 +1,14 @@ -// Generated by cargo2android.py for tests that depend on this crate. +// Generated by update_crate_tests.py for tests that depend on this crate. { "presubmit": [ { - "name": "unicode-bidi_device_test_src_lib" - }, - { "name": "serde_test_device_test_src_lib" }, { "name": "url_device_test_src_lib" + }, + { + "name": "unicode-bidi_device_test_src_lib" } ] } @@ -71,6 +71,11 @@ fn main() { println!("cargo:rustc-cfg=num_nonzero"); } + // Current minimum supported version of serde_derive crate is Rust 1.31. + if minor >= 31 { + println!("cargo:rustc-cfg=serde_derive"); + } + // TryFrom, Atomic types, and non-zero signed integers stabilized in Rust 1.34: // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations diff --git a/src/de/from_primitive.rs b/src/de/from_primitive.rs deleted file mode 100644 index 544f3f1..0000000 --- a/src/de/from_primitive.rs +++ /dev/null @@ -1,260 +0,0 @@ -use lib::*; - -macro_rules! int_to_int { - ($dst:ident, $n:ident) => { - if $dst::min_value() as i64 <= $n as i64 && $n as i64 <= $dst::max_value() as i64 { - Some($n as $dst) - } else { - None - } - }; -} - -macro_rules! int_to_uint { - ($dst:ident, $n:ident) => { - if 0 <= $n && $n as u64 <= $dst::max_value() as u64 { - Some($n as $dst) - } else { - None - } - }; -} - -macro_rules! uint_to { - ($dst:ident, $n:ident) => { - if $n as u64 <= $dst::max_value() as u64 { - Some($n as $dst) - } else { - None - } - }; -} - -pub trait FromPrimitive: Sized { - fn from_i8(n: i8) -> Option<Self>; - fn from_i16(n: i16) -> Option<Self>; - fn from_i32(n: i32) -> Option<Self>; - fn from_i64(n: i64) -> Option<Self>; - fn from_u8(n: u8) -> Option<Self>; - fn from_u16(n: u16) -> Option<Self>; - fn from_u32(n: u32) -> Option<Self>; - fn from_u64(n: u64) -> Option<Self>; -} - -macro_rules! impl_from_primitive_for_int { - ($t:ident) => { - impl FromPrimitive for $t { - #[inline] - fn from_i8(n: i8) -> Option<Self> { - int_to_int!($t, n) - } - #[inline] - fn from_i16(n: i16) -> Option<Self> { - int_to_int!($t, n) - } - #[inline] - fn from_i32(n: i32) -> Option<Self> { - int_to_int!($t, n) - } - #[inline] - fn from_i64(n: i64) -> Option<Self> { - int_to_int!($t, n) - } - #[inline] - fn from_u8(n: u8) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u16(n: u16) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u32(n: u32) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u64(n: u64) -> Option<Self> { - uint_to!($t, n) - } - } - }; -} - -macro_rules! impl_from_primitive_for_uint { - ($t:ident) => { - impl FromPrimitive for $t { - #[inline] - fn from_i8(n: i8) -> Option<Self> { - int_to_uint!($t, n) - } - #[inline] - fn from_i16(n: i16) -> Option<Self> { - int_to_uint!($t, n) - } - #[inline] - fn from_i32(n: i32) -> Option<Self> { - int_to_uint!($t, n) - } - #[inline] - fn from_i64(n: i64) -> Option<Self> { - int_to_uint!($t, n) - } - #[inline] - fn from_u8(n: u8) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u16(n: u16) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u32(n: u32) -> Option<Self> { - uint_to!($t, n) - } - #[inline] - fn from_u64(n: u64) -> Option<Self> { - uint_to!($t, n) - } - } - }; -} - -macro_rules! impl_from_primitive_for_float { - ($t:ident) => { - impl FromPrimitive for $t { - #[inline] - fn from_i8(n: i8) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_i16(n: i16) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_i32(n: i32) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_i64(n: i64) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_u8(n: u8) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_u16(n: u16) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_u32(n: u32) -> Option<Self> { - Some(n as Self) - } - #[inline] - fn from_u64(n: u64) -> Option<Self> { - Some(n as Self) - } - } - }; -} - -impl_from_primitive_for_int!(isize); -impl_from_primitive_for_int!(i8); -impl_from_primitive_for_int!(i16); -impl_from_primitive_for_int!(i32); -impl_from_primitive_for_int!(i64); -impl_from_primitive_for_uint!(usize); -impl_from_primitive_for_uint!(u8); -impl_from_primitive_for_uint!(u16); -impl_from_primitive_for_uint!(u32); -impl_from_primitive_for_uint!(u64); -impl_from_primitive_for_float!(f32); -impl_from_primitive_for_float!(f64); - -serde_if_integer128! { - impl FromPrimitive for i128 { - #[inline] - fn from_i8(n: i8) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_i16(n: i16) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_i32(n: i32) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_i64(n: i64) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_u8(n: u8) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_u16(n: u16) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_u32(n: u32) -> Option<Self> { - Some(n as i128) - } - #[inline] - fn from_u64(n: u64) -> Option<Self> { - Some(n as i128) - } - } - - impl FromPrimitive for u128 { - #[inline] - fn from_i8(n: i8) -> Option<Self> { - if n >= 0 { - Some(n as u128) - } else { - None - } - } - #[inline] - fn from_i16(n: i16) -> Option<Self> { - if n >= 0 { - Some(n as u128) - } else { - None - } - } - #[inline] - fn from_i32(n: i32) -> Option<Self> { - if n >= 0 { - Some(n as u128) - } else { - None - } - } - #[inline] - fn from_i64(n: i64) -> Option<Self> { - if n >= 0 { - Some(n as u128) - } else { - None - } - } - #[inline] - fn from_u8(n: u8) -> Option<Self> { - Some(n as u128) - } - #[inline] - fn from_u16(n: u16) -> Option<Self> { - Some(n as u128) - } - #[inline] - fn from_u32(n: u32) -> Option<Self> { - Some(n as u128) - } - #[inline] - fn from_u64(n: u64) -> Option<Self> { - Some(n as u128) - } - } -} diff --git a/src/de/ignored_any.rs b/src/de/ignored_any.rs index 68a644e..1d50f5e 100644 --- a/src/de/ignored_any.rs +++ b/src/de/ignored_any.rs @@ -130,12 +130,28 @@ impl<'de> Visitor<'de> for IgnoredAny { Ok(IgnoredAny) } + serde_if_integer128! { + #[inline] + fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> { + let _ = x; + Ok(IgnoredAny) + } + } + #[inline] fn visit_u64<E>(self, x: u64) -> Result<Self::Value, E> { let _ = x; Ok(IgnoredAny) } + serde_if_integer128! { + #[inline] + fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> { + let _ = x; + Ok(IgnoredAny) + } + } + #[inline] fn visit_f64<E>(self, x: f64) -> Result<Self::Value, E> { let _ = x; diff --git a/src/de/impls.rs b/src/de/impls.rs index 4d100d6..409f6cb 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -7,11 +7,10 @@ use de::{ #[cfg(any(core_duration, feature = "std", feature = "alloc"))] use de::MapAccess; -use __private::de::InPlaceSeed; -use de::from_primitive::FromPrimitive; +use seed::InPlaceSeed; #[cfg(any(feature = "std", feature = "alloc"))] -use __private::de::size_hint; +use __private::size_hint; //////////////////////////////////////////////////////////////////////////////// @@ -81,38 +80,8 @@ impl<'de> Deserialize<'de> for bool { //////////////////////////////////////////////////////////////////////////////// -macro_rules! visit_integer_method { - ($src_ty:ident, $method:ident, $from_method:ident, $group:ident, $group_ty:ident) => { - #[inline] - fn $method<E>(self, v: $src_ty) -> Result<Self::Value, E> - where - E: Error, - { - match FromPrimitive::$from_method(v) { - Some(v) => Ok(v), - None => Err(Error::invalid_value( - Unexpected::$group(v as $group_ty), - &self, - )), - } - } - }; -} - -macro_rules! visit_float_method { - ($src_ty:ident, $method:ident) => { - #[inline] - fn $method<E>(self, v: $src_ty) -> Result<Self::Value, E> - where - E: Error, - { - Ok(v as Self::Value) - } - }; -} - macro_rules! impl_deserialize_num { - ($ty:ident, $method:ident, $($visit:ident),*) => { + ($ty:ident, $deserialize:ident $($methods:tt)*) => { impl<'de> Deserialize<'de> for $ty { #[inline] fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> @@ -128,131 +97,221 @@ macro_rules! impl_deserialize_num { formatter.write_str(stringify!($ty)) } - $( - impl_deserialize_num!($visit $ty); - )* + $($methods)* } - deserializer.$method(PrimitiveVisitor) + deserializer.$deserialize(PrimitiveVisitor) } } }; +} - (integer $ty:ident) => { - visit_integer_method!(i8, visit_i8, from_i8, Signed, i64); - visit_integer_method!(i16, visit_i16, from_i16, Signed, i64); - visit_integer_method!(i32, visit_i32, from_i32, Signed, i64); - visit_integer_method!(i64, visit_i64, from_i64, Signed, i64); +macro_rules! num_self { + ($ty:ident : $visit:ident) => { + #[inline] + fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v) + } + }; +} - visit_integer_method!(u8, visit_u8, from_u8, Unsigned, u64); - visit_integer_method!(u16, visit_u16, from_u16, Unsigned, u64); - visit_integer_method!(u32, visit_u32, from_u32, Unsigned, u64); - visit_integer_method!(u64, visit_u64, from_u64, Unsigned, u64); +macro_rules! num_as_self { + ($($ty:ident : $visit:ident)*) => { + $( + #[inline] + fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v as Self::Value) + } + )* }; +} - (float $ty:ident) => { - visit_float_method!(f32, visit_f32); - visit_float_method!(f64, visit_f64); +macro_rules! int_to_int { + ($($ty:ident : $visit:ident)*) => { + $( + #[inline] + fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> + where + E: Error, + { + if Self::Value::min_value() as i64 <= v as i64 && v as i64 <= Self::Value::max_value() as i64 { + Ok(v as Self::Value) + } else { + Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) + } + } + )* }; } -impl_deserialize_num!(i8, deserialize_i8, integer); -impl_deserialize_num!(i16, deserialize_i16, integer); -impl_deserialize_num!(i32, deserialize_i32, integer); -impl_deserialize_num!(i64, deserialize_i64, integer); -impl_deserialize_num!(isize, deserialize_i64, integer); +macro_rules! int_to_uint { + ($($ty:ident : $visit:ident)*) => { + $( + #[inline] + fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> + where + E: Error, + { + if 0 <= v && v as u64 <= Self::Value::max_value() as u64 { + Ok(v as Self::Value) + } else { + Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) + } + } + )* + }; +} -impl_deserialize_num!(u8, deserialize_u8, integer); -impl_deserialize_num!(u16, deserialize_u16, integer); -impl_deserialize_num!(u32, deserialize_u32, integer); -impl_deserialize_num!(u64, deserialize_u64, integer); -impl_deserialize_num!(usize, deserialize_u64, integer); +macro_rules! uint_to_self { + ($($ty:ident : $visit:ident)*) => { + $( + #[inline] + fn $visit<E>(self, v: $ty) -> Result<Self::Value, E> + where + E: Error, + { + if v as u64 <= Self::Value::max_value() as u64 { + Ok(v as Self::Value) + } else { + Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) + } + } + )* + }; +} -impl_deserialize_num!(f32, deserialize_f32, integer, float); -impl_deserialize_num!(f64, deserialize_f64, integer, float); +impl_deserialize_num! { + i8, deserialize_i8 + num_self!(i8:visit_i8); + int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64); + uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} -serde_if_integer128! { - impl<'de> Deserialize<'de> for i128 { - #[inline] - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: Deserializer<'de>, - { - struct PrimitiveVisitor; +impl_deserialize_num! { + i16, deserialize_i16 + num_self!(i16:visit_i16); + num_as_self!(i8:visit_i8); + int_to_int!(i32:visit_i32 i64:visit_i64); + uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - impl<'de> Visitor<'de> for PrimitiveVisitor { - type Value = i128; +impl_deserialize_num! { + i32, deserialize_i32 + num_self!(i32:visit_i32); + num_as_self!(i8:visit_i8 i16:visit_i16); + int_to_int!(i64:visit_i64); + uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("i128") - } +impl_deserialize_num! { + i64, deserialize_i64 + num_self!(i64:visit_i64); + num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32); + uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - impl_deserialize_num!(integer i128); +impl_deserialize_num! { + isize, deserialize_i64 + num_as_self!(i8:visit_i8 i16:visit_i16); + int_to_int!(i32:visit_i32 i64:visit_i64); + uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - #[inline] - fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> - where - E: Error, - { - Ok(v) - } +impl_deserialize_num! { + u8, deserialize_u8 + num_self!(u8:visit_u8); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + uint_to_self!(u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - #[inline] - fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> - where - E: Error, - { - if v <= i128::max_value() as u128 { - Ok(v as i128) - } else { - Err(Error::invalid_value(Unexpected::Other("u128"), &self)) - } - } - } +impl_deserialize_num! { + u16, deserialize_u16 + num_self!(u16:visit_u16); + num_as_self!(u8:visit_u8); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + uint_to_self!(u32:visit_u32 u64:visit_u64); +} - deserializer.deserialize_i128(PrimitiveVisitor) - } - } +impl_deserialize_num! { + u32, deserialize_u32 + num_self!(u32:visit_u32); + num_as_self!(u8:visit_u8 u16:visit_u16); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + uint_to_self!(u64:visit_u64); +} - impl<'de> Deserialize<'de> for u128 { - #[inline] - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: Deserializer<'de>, - { - struct PrimitiveVisitor; +impl_deserialize_num! { + u64, deserialize_u64 + num_self!(u64:visit_u64); + num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); +} - impl<'de> Visitor<'de> for PrimitiveVisitor { - type Value = u128; +impl_deserialize_num! { + usize, deserialize_u64 + num_as_self!(u8:visit_u8 u16:visit_u16); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + uint_to_self!(u32:visit_u32 u64:visit_u64); +} - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("u128") - } +impl_deserialize_num! { + f32, deserialize_f32 + num_self!(f32:visit_f32); + num_as_self!(f64:visit_f64); + num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - impl_deserialize_num!(integer u128); +impl_deserialize_num! { + f64, deserialize_f64 + num_self!(f64:visit_f64); + num_as_self!(f32:visit_f32); + num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); +} - #[inline] - fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> - where - E: Error, - { - if v >= 0 { - Ok(v as u128) - } else { - Err(Error::invalid_value(Unexpected::Other("i128"), &self)) - } - } +serde_if_integer128! { + impl_deserialize_num! { + i128, deserialize_i128 + num_self!(i128:visit_i128); + num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); + num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); - #[inline] - fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> - where - E: Error, - { - Ok(v) - } + #[inline] + fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> + where + E: Error, + { + if v <= i128::max_value() as u128 { + Ok(v as i128) + } else { + Err(Error::invalid_value(Unexpected::Other("u128"), &self)) } + } + } + + impl_deserialize_num! { + u128, deserialize_u128 + num_self!(u128:visit_u128); + num_as_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); + int_to_uint!(i8:visit_i8 i16:visit_i16 i32:visit_i32 i64:visit_i64); - deserializer.deserialize_u128(PrimitiveVisitor) + #[inline] + fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> + where + E: Error, + { + if 0 <= v { + Ok(v as u128) + } else { + Err(Error::invalid_value(Unexpected::Other("i128"), &self)) + } } } } @@ -1260,24 +1319,7 @@ macro_rules! parse_ip_impl { D: Deserializer<'de>, { if deserializer.is_human_readable() { - struct IpAddrVisitor; - - impl<'de> Visitor<'de> for IpAddrVisitor { - type Value = $ty; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str($expecting) - } - - fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> - where - E: Error, - { - s.parse().map_err(Error::custom) - } - } - - deserializer.deserialize_str(IpAddrVisitor) + deserializer.deserialize_str(FromStrVisitor::new($expecting)) } else { <[u8; $size]>::deserialize(deserializer).map(<$ty>::from) } @@ -1405,24 +1447,7 @@ impl<'de> Deserialize<'de> for net::IpAddr { D: Deserializer<'de>, { if deserializer.is_human_readable() { - struct IpAddrVisitor; - - impl<'de> Visitor<'de> for IpAddrVisitor { - type Value = net::IpAddr; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("IP address") - } - - fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> - where - E: Error, - { - s.parse().map_err(Error::custom) - } - } - - deserializer.deserialize_str(IpAddrVisitor) + deserializer.deserialize_str(FromStrVisitor::new("IP address")) } else { use lib::net::IpAddr; deserialize_enum! { @@ -1449,24 +1474,7 @@ macro_rules! parse_socket_impl { D: Deserializer<'de>, { if deserializer.is_human_readable() { - struct SocketAddrVisitor; - - impl<'de> Visitor<'de> for SocketAddrVisitor { - type Value = $ty; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str($expecting) - } - - fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> - where - E: Error, - { - s.parse().map_err(Error::custom) - } - } - - deserializer.deserialize_str(SocketAddrVisitor) + deserializer.deserialize_str(FromStrVisitor::new($expecting)) } else { <(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port)) } @@ -1482,24 +1490,7 @@ impl<'de> Deserialize<'de> for net::SocketAddr { D: Deserializer<'de>, { if deserializer.is_human_readable() { - struct SocketAddrVisitor; - - impl<'de> Visitor<'de> for SocketAddrVisitor { - type Value = net::SocketAddr; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("socket address") - } - - fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> - where - E: Error, - { - s.parse().map_err(Error::custom) - } - } - - deserializer.deserialize_str(SocketAddrVisitor) + deserializer.deserialize_str(FromStrVisitor::new("socket address")) } else { use lib::net::SocketAddr; deserialize_enum! { @@ -1917,6 +1908,17 @@ impl<'de> Deserialize<'de> for Duration { } } + fn check_overflow<E>(secs: u64, nanos: u32) -> Result<(), E> + where + E: Error, + { + static NANOS_PER_SEC: u32 = 1_000_000_000; + match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { + Some(_) => Ok(()), + None => Err(E::custom("overflow deserializing Duration")), + } + } + struct DurationVisitor; impl<'de> Visitor<'de> for DurationVisitor { @@ -1942,6 +1944,7 @@ impl<'de> Deserialize<'de> for Duration { return Err(Error::invalid_length(1, &self)); } }; + try!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } @@ -1975,6 +1978,7 @@ impl<'de> Deserialize<'de> for Duration { Some(nanos) => nanos, None => return Err(<A::Error as Error>::missing_field("nanos")), }; + try!(check_overflow(secs, nanos)); Ok(Duration::new(secs, nanos)) } } @@ -2603,3 +2607,39 @@ atomic_impl! { atomic_impl! { AtomicI64 AtomicU64 } + +#[cfg(feature = "std")] +struct FromStrVisitor<T> { + expecting: &'static str, + ty: PhantomData<T>, +} + +#[cfg(feature = "std")] +impl<T> FromStrVisitor<T> { + fn new(expecting: &'static str) -> Self { + FromStrVisitor { + expecting: expecting, + ty: PhantomData, + } + } +} + +#[cfg(feature = "std")] +impl<'de, T> Visitor<'de> for FromStrVisitor<T> +where + T: str::FromStr, + T::Err: fmt::Display, +{ + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str(self.expecting) + } + + fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> + where + E: Error, + { + s.parse().map_err(Error::custom) + } +} diff --git a/src/de/mod.rs b/src/de/mod.rs index 6d39473..1ed7b4f 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -118,7 +118,6 @@ use lib::*; pub mod value; -mod from_primitive; mod ignored_any; mod impls; mod utf8; @@ -393,7 +392,7 @@ pub enum Unexpected<'a> { } impl<'a> fmt::Display for Unexpected<'a> { - fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { use self::Unexpected::*; match *self { Bool(b) => write!(formatter, "boolean `{}`", b), diff --git a/src/de/seed.rs b/src/de/seed.rs new file mode 100644 index 0000000..13b7ea4 --- /dev/null +++ b/src/de/seed.rs @@ -0,0 +1,19 @@ +use de::{Deserialize, DeserializeSeed, Deserializer}; + +/// A DeserializeSeed helper for implementing deserialize_in_place Visitors. +/// +/// Wraps a mutable reference and calls deserialize_in_place on it. +pub struct InPlaceSeed<'a, T: 'a>(pub &'a mut T); + +impl<'a, 'de, T> DeserializeSeed<'de> for InPlaceSeed<'a, T> +where + T: Deserialize<'de>, +{ + type Value = (); + fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: Deserializer<'de>, + { + T::deserialize_in_place(deserializer, self.0) + } +} diff --git a/src/de/value.rs b/src/de/value.rs index 96f2a90..1b154c3 100644 --- a/src/de/value.rs +++ b/src/de/value.rs @@ -24,8 +24,8 @@ use lib::*; use self::private::{First, Second}; -use __private::de::size_hint; -use de::{self, Expected, IntoDeserializer, SeqAccess}; +use __private::size_hint; +use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor}; use ser; //////////////////////////////////////////////////////////////////////////////// @@ -48,7 +48,7 @@ macro_rules! impl_copy_clone { /// A minimal representation of all possible errors that can occur using the /// `IntoDeserializer` trait. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, PartialEq)] pub struct Error { err: ErrorImpl, } @@ -93,16 +93,25 @@ impl ser::Error for Error { impl Display for Error { #[cfg(any(feature = "std", feature = "alloc"))] - fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str(&self.err) } #[cfg(not(any(feature = "std", feature = "alloc")))] - fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("Serde deserialization error") } } +impl Debug for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let mut debug = formatter.debug_tuple("Error"); + #[cfg(any(feature = "std", feature = "alloc"))] + debug.field(&self.err); + debug.finish() + } +} + #[cfg(feature = "std")] impl error::Error for Error { fn description(&self) -> &str { @@ -126,7 +135,6 @@ where } /// A deserializer holding a `()`. -#[derive(Debug)] pub struct UnitDeserializer<E> { marker: PhantomData<E>, } @@ -160,6 +168,12 @@ where } } +impl<E> Debug for UnitDeserializer<E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.debug_struct("UnitDeserializer").finish() + } +} + //////////////////////////////////////////////////////////////////////////////// /// A deserializer that cannot be instantiated. @@ -208,7 +222,6 @@ macro_rules! primitive_deserializer { ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => { #[doc = "A deserializer holding"] #[doc = $doc] - #[derive(Debug)] pub struct $name<E> { value: $ty, marker: PhantomData<E> @@ -249,6 +262,15 @@ macro_rules! primitive_deserializer { visitor.$method(self.value $($cast)*) } } + + impl<E> Debug for $name<E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct(stringify!($name)) + .field("value", &self.value) + .finish() + } + } } } @@ -272,7 +294,6 @@ serde_if_integer128! { } /// A deserializer holding a `u32`. -#[derive(Debug)] pub struct U32Deserializer<E> { value: u32, marker: PhantomData<E>, @@ -343,10 +364,18 @@ where } } +impl<E> Debug for U32Deserializer<E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("U32Deserializer") + .field("value", &self.value) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `&str`. -#[derive(Debug)] pub struct StrDeserializer<'a, E> { value: &'a str, marker: PhantomData<E>, @@ -417,11 +446,19 @@ where } } +impl<'a, E> Debug for StrDeserializer<'a, E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("StrDeserializer") + .field("value", &self.value) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `&str` with a lifetime tied to another /// deserializer. -#[derive(Debug)] pub struct BorrowedStrDeserializer<'de, E> { value: &'de str, marker: PhantomData<E>, @@ -488,11 +525,19 @@ where } } +impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("BorrowedStrDeserializer") + .field("value", &self.value) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `String`. #[cfg(any(feature = "std", feature = "alloc"))] -#[derive(Debug)] pub struct StringDeserializer<E> { value: String, marker: PhantomData<E>, @@ -574,11 +619,20 @@ where } } +#[cfg(any(feature = "std", feature = "alloc"))] +impl<E> Debug for StringDeserializer<E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("StringDeserializer") + .field("value", &self.value) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// /// A deserializer holding a `Cow<str>`. #[cfg(any(feature = "std", feature = "alloc"))] -#[derive(Debug)] pub struct CowStrDeserializer<'a, E> { value: Cow<'a, str>, marker: PhantomData<E>, @@ -663,21 +717,86 @@ where } } +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'a, E> Debug for CowStrDeserializer<'a, E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("CowStrDeserializer") + .field("value", &self.value) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// +/// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`]. +pub struct BytesDeserializer<'a, E> { + value: &'a [u8], + marker: PhantomData<E>, +} + +impl<'a, E> BytesDeserializer<'a, E> { + /// Create a new deserializer from the given bytes. + pub fn new(value: &'a [u8]) -> Self { + BytesDeserializer { + value: value, + marker: PhantomData, + } + } +} + +impl_copy_clone!(BytesDeserializer<'a>); + +impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8] +where + E: de::Error, +{ + type Deserializer = BytesDeserializer<'a, E>; + + fn into_deserializer(self) -> BytesDeserializer<'a, E> { + BytesDeserializer::new(self) + } +} + +impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> +where + E: de::Error, +{ + type Error = E; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_bytes(self.value) + } + + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct seq tuple + tuple_struct map struct enum identifier ignored_any + } +} + +impl<'a, E> Debug for BytesDeserializer<'a, E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("BytesDeserializer") + .field("value", &self.value) + .finish() + } +} + /// A deserializer holding a `&[u8]` with a lifetime tied to another -/// deserializer. -#[derive(Debug)] +/// deserializer. Always calls [`Visitor::visit_borrowed_bytes`]. pub struct BorrowedBytesDeserializer<'de, E> { value: &'de [u8], marker: PhantomData<E>, } -impl_copy_clone!(BorrowedBytesDeserializer<'de>); - impl<'de, E> BorrowedBytesDeserializer<'de, E> { - /// Create a new borrowed deserializer from the given byte slice. - pub fn new(value: &'de [u8]) -> BorrowedBytesDeserializer<'de, E> { + /// Create a new borrowed deserializer from the given borrowed bytes. + pub fn new(value: &'de [u8]) -> Self { BorrowedBytesDeserializer { value: value, marker: PhantomData, @@ -685,7 +804,9 @@ impl<'de, E> BorrowedBytesDeserializer<'de, E> { } } -impl<'de, E> de::Deserializer<'de> for BorrowedBytesDeserializer<'de, E> +impl_copy_clone!(BorrowedBytesDeserializer<'de>); + +impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> where E: de::Error, { @@ -693,7 +814,7 @@ where fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where - V: de::Visitor<'de>, + V: Visitor<'de>, { visitor.visit_borrowed_bytes(self.value) } @@ -701,14 +822,23 @@ where forward_to_deserialize_any! { bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bytes byte_buf option unit unit_struct newtype_struct seq tuple - tuple_struct map struct identifier ignored_any enum + tuple_struct map struct enum identifier ignored_any + } +} + +impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("BorrowedBytesDeserializer") + .field("value", &self.value) + .finish() } } //////////////////////////////////////////////////////////////////////////////// /// A deserializer that iterates over a sequence. -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct SeqDeserializer<I, E> { iter: iter::Fuse<I>, count: usize, @@ -813,6 +943,19 @@ impl Expected for ExpectedInSeq { } } +impl<I, E> Debug for SeqDeserializer<I, E> +where + I: Debug, +{ + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter + .debug_struct("SeqDeserializer") + .field("iter", &self.iter) + .field("count", &self.count) + .finish() + } +} + //////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] @@ -1108,7 +1251,6 @@ where } } -// Cannot #[derive(Debug)] because of the bound `Second<I::Item>: Debug`. impl<'de, I, E> Debug for MapDeserializer<'de, I, E> where I: Iterator + Debug, @@ -1121,8 +1263,6 @@ where .field("iter", &self.iter) .field("value", &self.value) .field("count", &self.count) - .field("lifetime", &self.lifetime) - .field("error", &self.error) .finish() } } @@ -1331,7 +1471,6 @@ mod private { use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor}; - #[derive(Clone, Debug)] pub struct UnitOnly<E> { marker: PhantomData<E>, } @@ -1390,7 +1529,6 @@ mod private { } } - #[derive(Clone, Debug)] pub struct MapAsEnum<A> { map: A, } diff --git a/src/integer128.rs b/src/integer128.rs index 3028c0a..0ee05bd 100644 --- a/src/integer128.rs +++ b/src/integer128.rs @@ -10,7 +10,7 @@ /// bother with this macro and may assume support for 128-bit integers. /// /// ```edition2018 -/// # use serde::__private::ser::Error; +/// # use serde::__private::doc::Error; /// # /// # struct MySerializer; /// # @@ -84,7 +84,7 @@ //////////////////////////////////////////////////////////////////////////////// // Serde types in rustdoc of other crates get linked to here. -#![doc(html_root_url = "https://docs.rs/serde/1.0.119")] +#![doc(html_root_url = "https://docs.rs/serde/1.0.123")] // Support using Serde without the standard library! #![cfg_attr(not(feature = "std"), no_std)] // Unstable functionality only if the user asks for it. For tracking and @@ -276,6 +276,9 @@ use self::__private as export; #[allow(unused_imports)] use self::__private as private; +#[path = "de/seed.rs"] +mod seed; + #[cfg(not(feature = "std"))] mod std_error; diff --git a/src/private/de.rs b/src/private/de.rs index bcb964a..0c2f3b8 100644 --- a/src/private/de.rs +++ b/src/private/de.rs @@ -1,9 +1,10 @@ use lib::*; -use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor}; +use de::value::{BorrowedBytesDeserializer, BytesDeserializer}; +use de::{Deserialize, Deserializer, Error, IntoDeserializer, Visitor}; #[cfg(any(feature = "std", feature = "alloc"))] -use de::{MapAccess, Unexpected}; +use de::{DeserializeSeed, MapAccess, Unexpected}; #[cfg(any(feature = "std", feature = "alloc"))] pub use self::content::{ @@ -12,6 +13,8 @@ pub use self::content::{ TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor, }; +pub use seed::InPlaceSeed; + /// If the missing field is of type `Option<T>` then treat is as `None`, /// otherwise it is an error. pub fn missing_field<'de, V, E>(field: &'static str) -> Result<V, E> @@ -188,29 +191,6 @@ where .map(From::from) } -pub mod size_hint { - use lib::*; - - pub fn from_bounds<I>(iter: &I) -> Option<usize> - where - I: Iterator, - { - helper(iter.size_hint()) - } - - #[inline] - pub fn cautious(hint: Option<usize>) -> usize { - cmp::min(hint.unwrap_or(0), 4096) - } - - fn helper(bounds: (usize, Option<usize>)) -> Option<usize> { - match bounds { - (lower, Some(upper)) if lower == upper => Some(upper), - _ => None, - } - } -} - #[cfg(any(feature = "std", feature = "alloc"))] mod content { // This module is private and nothing here should be used outside of @@ -225,7 +205,7 @@ mod content { use lib::*; - use super::size_hint; + use __private::size_hint; use de::{ self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor, @@ -824,15 +804,17 @@ mod content { /// Not public API. pub struct TaggedContentVisitor<'de, T> { tag_name: &'static str, + expecting: &'static str, value: PhantomData<TaggedContent<'de, T>>, } impl<'de, T> TaggedContentVisitor<'de, T> { /// Visitor for the content of an internally tagged enum with the given /// tag name. - pub fn new(name: &'static str) -> Self { + pub fn new(name: &'static str, expecting: &'static str) -> Self { TaggedContentVisitor { tag_name: name, + expecting: expecting, value: PhantomData, } } @@ -861,7 +843,7 @@ mod content { type Value = TaggedContent<'de, T>; fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.write_str("internally tagged enum") + fmt.write_str(self.expecting) } fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error> @@ -1040,6 +1022,25 @@ mod content { _ => Err(self.invalid_type(&visitor)), } } + + fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E> + where + V: Visitor<'de>, + { + match self.content { + Content::F32(v) => visitor.visit_f32(v), + Content::F64(v) => visitor.visit_f64(v), + Content::U8(v) => visitor.visit_u8(v), + Content::U16(v) => visitor.visit_u16(v), + Content::U32(v) => visitor.visit_u32(v), + Content::U64(v) => visitor.visit_u64(v), + Content::I8(v) => visitor.visit_i8(v), + Content::I16(v) => visitor.visit_i16(v), + Content::I32(v) => visitor.visit_i32(v), + Content::I64(v) => visitor.visit_i64(v), + _ => Err(self.invalid_type(&visitor)), + } + } } fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E> @@ -1179,25 +1180,14 @@ mod content { where V: Visitor<'de>, { - match self.content { - Content::F32(v) => visitor.visit_f32(v), - Content::F64(v) => visitor.visit_f64(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } + self.deserialize_float(visitor) } fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>, { - match self.content { - Content::F64(v) => visitor.visit_f64(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } + self.deserialize_float(visitor) } fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> @@ -2542,11 +2532,13 @@ pub trait IdentifierDeserializer<'de, E: Error> { fn from(self) -> Self::Deserializer; } -impl<'de, E> IdentifierDeserializer<'de, E> for u32 +pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T); + +impl<'de, E> IdentifierDeserializer<'de, E> for u64 where E: Error, { - type Deserializer = <u32 as IntoDeserializer<'de, E>>::Deserializer; + type Deserializer = <u64 as IntoDeserializer<'de, E>>::Deserializer; fn from(self) -> Self::Deserializer { self.into_deserializer() @@ -2558,21 +2550,32 @@ pub struct StrDeserializer<'a, E> { marker: PhantomData<E>, } -impl<'a, E> IdentifierDeserializer<'a, E> for &'a str +impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E> where E: Error, { - type Deserializer = StrDeserializer<'a, E>; + type Error = E; - fn from(self) -> Self::Deserializer { - StrDeserializer { - value: self, - marker: PhantomData, - } + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_str(self.value) + } + + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct seq tuple + tuple_struct map struct enum identifier ignored_any } } -impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E> +pub struct BorrowedStrDeserializer<'de, E> { + value: &'de str, + marker: PhantomData<E>, +} + +impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E> where E: Error, { @@ -2582,7 +2585,7 @@ where where V: Visitor<'de>, { - visitor.visit_str(self.value) + visitor.visit_borrowed_str(self.value) } forward_to_deserialize_any! { @@ -2592,60 +2595,53 @@ where } } -pub struct BytesDeserializer<'a, E> { - value: &'a [u8], - marker: PhantomData<E>, -} - -impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8] +impl<'a, E> IdentifierDeserializer<'a, E> for &'a str where E: Error, { - type Deserializer = BytesDeserializer<'a, E>; + type Deserializer = StrDeserializer<'a, E>; fn from(self) -> Self::Deserializer { - BytesDeserializer { + StrDeserializer { value: self, marker: PhantomData, } } } -impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> +impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str> where E: Error, { - type Error = E; + type Deserializer = BorrowedStrDeserializer<'de, E>; - fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where - V: Visitor<'de>, - { - visitor.visit_bytes(self.value) + fn from(self) -> Self::Deserializer { + BorrowedStrDeserializer { + value: self.0, + marker: PhantomData, + } } +} - forward_to_deserialize_any! { - bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string - bytes byte_buf option unit unit_struct newtype_struct seq tuple - tuple_struct map struct enum identifier ignored_any +impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8] +where + E: Error, +{ + type Deserializer = BytesDeserializer<'a, E>; + + fn from(self) -> Self::Deserializer { + BytesDeserializer::new(self) } } -/// A DeserializeSeed helper for implementing deserialize_in_place Visitors. -/// -/// Wraps a mutable reference and calls deserialize_in_place on it. -pub struct InPlaceSeed<'a, T: 'a>(pub &'a mut T); - -impl<'a, 'de, T> DeserializeSeed<'de> for InPlaceSeed<'a, T> +impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]> where - T: Deserialize<'de>, + E: Error, { - type Value = (); - fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> - where - D: Deserializer<'de>, - { - T::deserialize_in_place(deserializer, self.0) + type Deserializer = BorrowedBytesDeserializer<'de, E>; + + fn from(self) -> Self::Deserializer { + BorrowedBytesDeserializer::new(self.0) } } diff --git a/src/private/macros.rs b/src/private/doc.rs index 39d66f6..f597af8 100644 --- a/src/private/macros.rs +++ b/src/private/doc.rs @@ -1,3 +1,35 @@ +// Used only by Serde doc tests. Not public API. + +use lib::*; + +use ser; + +#[doc(hidden)] +#[derive(Debug)] +pub struct Error; + +impl ser::Error for Error { + fn custom<T>(_: T) -> Self + where + T: Display, + { + unimplemented!() + } +} + +#[cfg(feature = "std")] +impl error::Error for Error { + fn description(&self) -> &str { + unimplemented!() + } +} + +impl Display for Error { + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { + unimplemented!() + } +} + #[doc(hidden)] #[macro_export] macro_rules! __private_serialize { @@ -11,19 +43,6 @@ macro_rules! __private_serialize { } #[doc(hidden)] -#[macro_export] -macro_rules! __private_deserialize { - () => { - trait Deserialize<'de>: Sized { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: $crate::Deserializer<'de>; - } - }; -} - -/// Used only by Serde doc tests. Not public API. -#[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! __serialize_unimplemented { ($($func:ident)*) => { diff --git a/src/private/mod.rs b/src/private/mod.rs index 73355e4..24ea84b 100644 --- a/src/private/mod.rs +++ b/src/private/mod.rs @@ -1,8 +1,13 @@ -mod macros; - +#[cfg(serde_derive)] pub mod de; +#[cfg(serde_derive)] pub mod ser; +pub mod size_hint; + +// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed. +pub mod doc; + pub use lib::clone::Clone; pub use lib::convert::{From, Into}; pub use lib::default::Default; diff --git a/src/private/ser.rs b/src/private/ser.rs index eb8cfc9..6ee9993 100644 --- a/src/private/ser.rs +++ b/src/private/ser.rs @@ -335,33 +335,6 @@ where } } -/// Used only by Serde doc tests. Not public API. -#[doc(hidden)] -#[derive(Debug)] -pub struct Error; - -impl ser::Error for Error { - fn custom<T>(_: T) -> Self - where - T: Display, - { - unimplemented!() - } -} - -#[cfg(feature = "std")] -impl error::Error for Error { - fn description(&self) -> &str { - unimplemented!() - } -} - -impl Display for Error { - fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { - unimplemented!() - } -} - #[cfg(any(feature = "std", feature = "alloc"))] mod content { use lib::*; @@ -452,7 +425,6 @@ mod content { } } - #[derive(Debug)] pub enum Content { Bool(bool), diff --git a/src/private/size_hint.rs b/src/private/size_hint.rs new file mode 100644 index 0000000..ca71e61 --- /dev/null +++ b/src/private/size_hint.rs @@ -0,0 +1,21 @@ +use lib::*; + +pub fn from_bounds<I>(iter: &I) -> Option<usize> +where + I: Iterator, +{ + helper(iter.size_hint()) +} + +#[cfg(any(feature = "std", feature = "alloc"))] +#[inline] +pub fn cautious(hint: Option<usize>) -> usize { + cmp::min(hint.unwrap_or(0), 4096) +} + +fn helper(bounds: (usize, Option<usize>)) -> Option<usize> { + match bounds { + (lower, Some(upper)) if lower == upper => Some(upper), + _ => None, + } +} diff --git a/src/ser/impossible.rs b/src/ser/impossible.rs index 8a6501e..e8df9ca 100644 --- a/src/ser/impossible.rs +++ b/src/ser/impossible.rs @@ -17,7 +17,7 @@ use ser::{ /// /// ```edition2018 /// # use serde::ser::{Serializer, Impossible}; -/// # use serde::__private::ser::Error; +/// # use serde::__private::doc::Error; /// # /// # struct MySerializer; /// # diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 1e7ef8a..d686c5a 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -711,7 +711,7 @@ pub trait Serializer: Sized { /// /// ```edition2018 /// # use serde::ser::{Serializer, SerializeSeq}; - /// # use serde::__private::ser::Error; + /// # use serde::__private::doc::Error; /// # /// # struct MySerializer; /// # |