From 13a33b3c338c0dbce2b93f6fe654f68fa6389d02 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 5 Mar 2025 09:23:29 +0000 Subject: [PATCH 1/4] Bump MSRV to 1.56 This is the first cargo version that actually enforces the rust-version field in Cargo.toml --- .github/workflows/ci.yml | 13 ++------ README.md | 4 +-- serde/Cargo.toml | 2 +- serde/build.rs | 21 ------------- serde/src/de/impls.rs | 27 +++++++---------- serde/src/private/mod.rs | 1 - serde/src/ser/impls.rs | 64 +++++----------------------------------- serde/src/std_error.rs | 2 +- 8 files changed, 24 insertions(+), 110 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50139b6d..446d46b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,7 +84,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [1.31.0, 1.34.0] + rust: [1.56.0, 1.60.0] timeout-minutes: 45 steps: - uses: actions/checkout@v4 @@ -94,6 +94,7 @@ jobs: - run: sed -i '/"test_suite"/d' Cargo.toml - run: cd serde && cargo build --features rc - run: cd serde && cargo build --no-default-features + - run: cd serde && cargo build --no-default-features --features alloc - run: cd serde && cargo build derive: @@ -111,16 +112,6 @@ jobs: - run: cd serde && cargo check - run: cd serde_derive && cargo check - alloc: - name: Rust 1.36.0 - runs-on: ubuntu-latest - timeout-minutes: 45 - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@1.36.0 - - run: sed -i '/"test_suite"/d' Cargo.toml - - run: cd serde && cargo build --no-default-features --features alloc - minimal: name: Minimal versions runs-on: ubuntu-latest diff --git a/README.md b/README.md index e3da0cbc..e62820e7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde msrv]][Rust 1.31] [![serde_derive msrv]][Rust 1.61] +# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde msrv]][Rust 1.56] [![serde_derive msrv]][Rust 1.61] [Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/serde/ci.yml?branch=master [actions]: https://github.com/serde-rs/serde/actions?query=branch%3Amaster @@ -6,7 +6,7 @@ [crates.io]: https://crates.io/crates/serde [serde msrv]: https://img.shields.io/crates/msrv/serde.svg?label=serde%20msrv&color=lightgray [serde_derive msrv]: https://img.shields.io/crates/msrv/serde_derive.svg?label=serde_derive%20msrv&color=lightgray -[Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html +[Rust 1.56]: https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html [Rust 1.61]: https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html **Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.** diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 2a865cfb..8c81e0f1 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["serde", "serialization", "no_std"] license = "MIT OR Apache-2.0" readme = "crates-io.md" repository = "https://github.com/serde-rs/serde" -rust-version = "1.31" +rust-version = "1.56" [dependencies] serde_derive = { version = "1", optional = true, path = "../serde_derive" } diff --git a/serde/build.rs b/serde/build.rs index 24aa0e6d..69fa327e 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -18,38 +18,17 @@ fn main() { println!("cargo:rustc-check-cfg=cfg(no_core_error)"); println!("cargo:rustc-check-cfg=cfg(no_core_net)"); println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); - println!("cargo:rustc-check-cfg=cfg(no_core_try_from)"); println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); println!("cargo:rustc-check-cfg=cfg(no_float_copysign)"); - println!("cargo:rustc-check-cfg=cfg(no_num_nonzero_signed)"); - println!("cargo:rustc-check-cfg=cfg(no_relaxed_trait_bounds)"); println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); - println!("cargo:rustc-check-cfg=cfg(no_systemtime_checked_add)"); println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)"); } let target = env::var("TARGET").unwrap(); let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; - // TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add - // stabilized in Rust 1.34: - // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto - // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations - if minor < 34 { - println!("cargo:rustc-cfg=no_core_try_from"); - println!("cargo:rustc-cfg=no_num_nonzero_signed"); - println!("cargo:rustc-cfg=no_systemtime_checked_add"); - println!("cargo:rustc-cfg=no_relaxed_trait_bounds"); - } - - // f32::copysign and f64::copysign stabilized in Rust 1.35. - // https://blog.rust-lang.org/2019/05/23/Rust-1.35.0.html#copy-the-sign-of-a-floating-point-number-onto-another - if minor < 35 { - println!("cargo:rustc-cfg=no_float_copysign"); - } - // Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60. if minor < 60 { println!("cargo:rustc-cfg=no_target_has_atomic"); diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 2d8c9903..037f0d61 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -80,10 +80,9 @@ impl<'de> Deserialize<'de> for bool { //////////////////////////////////////////////////////////////////////////////// macro_rules! impl_deserialize_num { - ($primitive:ident, $nonzero:ident $(cfg($($cfg:tt)*))*, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { + ($primitive:ident, $nonzero:ident, $deserialize:ident $($method:ident!($($val:ident : $visit:ident)*);)*) => { impl_deserialize_num!($primitive, $deserialize $($method!($($val : $visit)*);)*); - $(#[cfg($($cfg)*)])* impl<'de> Deserialize<'de> for num::$nonzero { fn deserialize(deserializer: D) -> Result where @@ -228,12 +227,12 @@ macro_rules! num_as_copysign_self { where E: Error, { - #[cfg(any(no_float_copysign, not(feature = "std")))] + #[cfg(not(feature = "std"))] { Ok(v as Self::Value) } - #[cfg(all(not(no_float_copysign), feature = "std"))] + #[cfg(feature = "std")] { // Preserve sign of NaN. The `as` produces a nondeterministic sign. let sign = if v.is_sign_positive() { 1.0 } else { -1.0 }; @@ -381,14 +380,14 @@ macro_rules! uint_to_self { } impl_deserialize_num! { - i8, NonZeroI8 cfg(not(no_num_nonzero_signed)), deserialize_i8 + i8, NonZeroI8, deserialize_i8 num_self!(i8:visit_i8); int_to_int!(i16:visit_i16 i32:visit_i32 i64:visit_i64); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { - i16, NonZeroI16 cfg(not(no_num_nonzero_signed)), deserialize_i16 + i16, NonZeroI16, deserialize_i16 num_self!(i16:visit_i16); num_as_self!(i8:visit_i8); int_to_int!(i32:visit_i32 i64:visit_i64); @@ -396,7 +395,7 @@ impl_deserialize_num! { } impl_deserialize_num! { - i32, NonZeroI32 cfg(not(no_num_nonzero_signed)), deserialize_i32 + i32, NonZeroI32, deserialize_i32 num_self!(i32:visit_i32); num_as_self!(i8:visit_i8 i16:visit_i16); int_to_int!(i64:visit_i64); @@ -404,14 +403,14 @@ impl_deserialize_num! { } impl_deserialize_num! { - i64, NonZeroI64 cfg(not(no_num_nonzero_signed)), deserialize_i64 + i64, NonZeroI64, deserialize_i64 num_self!(i64:visit_i64); num_as_self!(i8:visit_i8 i16:visit_i16 i32:visit_i32); uint_to_self!(u8:visit_u8 u16:visit_u16 u32:visit_u32 u64:visit_u64); } impl_deserialize_num! { - isize, NonZeroIsize cfg(not(no_num_nonzero_signed)), deserialize_i64 + isize, NonZeroIsize, 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); @@ -528,7 +527,7 @@ macro_rules! num_128 { } impl_deserialize_num! { - i128, NonZeroI128 cfg(not(no_num_nonzero_signed)), deserialize_i128 + i128, NonZeroI128, 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); @@ -2415,13 +2414,9 @@ impl<'de> Deserialize<'de> for SystemTime { const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"]; let duration = tri!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor)); - #[cfg(not(no_systemtime_checked_add))] - let ret = UNIX_EPOCH + UNIX_EPOCH .checked_add(duration) - .ok_or_else(|| D::Error::custom("overflow deserializing SystemTime")); - #[cfg(no_systemtime_checked_add)] - let ret = Ok(UNIX_EPOCH + duration); - ret + .ok_or_else(|| D::Error::custom("overflow deserializing SystemTime")) } } diff --git a/serde/src/private/mod.rs b/serde/src/private/mod.rs index 177f8501..f1207c8a 100644 --- a/serde/src/private/mod.rs +++ b/serde/src/private/mod.rs @@ -20,7 +20,6 @@ pub use self::string::from_utf8_lossy; #[cfg(any(feature = "alloc", feature = "std"))] pub use crate::lib::{ToString, Vec}; -#[cfg(not(no_core_try_from))] pub use crate::lib::convert::TryFrom; mod string { diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index fb574eae..8bbb244a 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -185,11 +185,10 @@ where } } -#[cfg(not(no_relaxed_trait_bounds))] macro_rules! seq_impl { ( $(#[$attr:meta])* - $ty:ident + $ty:ident ) => { $(#[$attr])* impl Serialize for $ty @@ -207,45 +206,22 @@ macro_rules! seq_impl { } } -#[cfg(no_relaxed_trait_bounds)] -macro_rules! seq_impl { - ( - $(#[$attr:meta])* - $ty:ident - ) => { - $(#[$attr])* - impl Serialize for $ty - where - T: Serialize $(+ $tbound1 $(+ $tbound2)*)*, - $($typaram: $bound,)* - { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.collect_seq(self) - } - } - } +seq_impl! { + #[cfg(any(feature = "std", feature = "alloc"))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] + BinaryHeap } seq_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] - BinaryHeap -} - -seq_impl! { - #[cfg(any(feature = "std", feature = "alloc"))] - #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] - BTreeSet + BTreeSet } seq_impl! { #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] - HashSet + HashSet } seq_impl! { @@ -445,7 +421,6 @@ tuple_impls! { //////////////////////////////////////////////////////////////////////////////// -#[cfg(not(no_relaxed_trait_bounds))] macro_rules! map_impl { ( $(#[$attr:meta])* @@ -468,30 +443,6 @@ macro_rules! map_impl { } } -#[cfg(no_relaxed_trait_bounds)] -macro_rules! map_impl { - ( - $(#[$attr:meta])* - $ty:ident - ) => { - $(#[$attr])* - impl Serialize for $ty - where - K: Serialize $(+ $kbound1 $(+ $kbound2)*)*, - V: Serialize, - $($typaram: $bound,)* - { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.collect_map(self) - } - } - } -} - map_impl! { #[cfg(any(feature = "std", feature = "alloc"))] #[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] @@ -640,7 +591,6 @@ nonzero_integers! { NonZeroUsize, } -#[cfg(not(no_num_nonzero_signed))] nonzero_integers! { NonZeroI8, NonZeroI16, diff --git a/serde/src/std_error.rs b/serde/src/std_error.rs index f15a4d74..e026ace1 100644 --- a/serde/src/std_error.rs +++ b/serde/src/std_error.rs @@ -42,7 +42,7 @@ use crate::lib::{Debug, Display}; /// ``` pub trait Error: Debug + Display { /// The underlying cause of this error, if any. - fn source(&self) -> Option<&(Error + 'static)> { + fn source(&self) -> Option<&(dyn Error + 'static)> { None } } From 6ac8049b9245dd1fdd3641fbd6e965c01595b0bc Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 5 Mar 2025 09:58:12 +0000 Subject: [PATCH 2/4] clippy --- serde/src/de/impls.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 037f0d61..001b8210 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -249,9 +249,7 @@ macro_rules! int_to_int { where E: Error, { - if Self::Value::min_value() as i64 <= v as i64 - && v as i64 <= Self::Value::max_value() as i64 - { + if Self::Value::MIN as i64 <= v as i64 && v as i64 <= Self::Value::MAX as i64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) @@ -264,9 +262,7 @@ macro_rules! int_to_int { where E: Error, { - if $primitive::min_value() as i64 <= v as i64 - && v as i64 <= $primitive::max_value() as i64 - { + if $primitive::MIN as i64 <= v as i64 && v as i64 <= $primitive::MAX as i64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } @@ -298,7 +294,7 @@ macro_rules! int_to_uint { where E: Error, { - if 0 <= v && v as u64 <= Self::Value::max_value() as u64 { + if 0 <= v && v as u64 <= Self::Value::MAX as u64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) @@ -311,7 +307,7 @@ macro_rules! int_to_uint { where E: Error, { - if 0 < v && v as u64 <= $primitive::max_value() as u64 { + if 0 < v && v as u64 <= $primitive::MAX as u64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } @@ -343,7 +339,7 @@ macro_rules! uint_to_self { where E: Error, { - if v as u64 <= Self::Value::max_value() as u64 { + if v as u64 <= Self::Value::MAX as u64 { Ok(v as Self::Value) } else { Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) @@ -356,7 +352,7 @@ macro_rules! uint_to_self { where E: Error, { - if v as u64 <= $primitive::max_value() as u64 { + if v as u64 <= $primitive::MAX as u64 { if let Some(nonzero) = Self::Value::new(v as $primitive) { return Ok(nonzero); } @@ -475,9 +471,7 @@ macro_rules! num_128 { where E: Error, { - if v as i128 >= Self::Value::min_value() as i128 - && v as u128 <= Self::Value::max_value() as u128 - { + if v as i128 >= Self::Value::MIN as i128 && v as u128 <= Self::Value::MAX as u128 { Ok(v as Self::Value) } else { Err(Error::invalid_value( @@ -493,9 +487,7 @@ macro_rules! num_128 { where E: Error, { - if v as i128 >= $primitive::min_value() as i128 - && v as u128 <= $primitive::max_value() as u128 - { + if v as i128 >= $primitive::MIN as i128 && v as u128 <= $primitive::MAX as u128 { if let Some(nonzero) = Self::Value::new(v as $primitive) { Ok(nonzero) } else { From e3a4165363d6bee2d28068dcc5d0a95b78b85192 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 5 Mar 2025 09:51:32 +0000 Subject: [PATCH 3/4] Switch all crates to edition 2021 --- Cargo.toml | 1 + serde/Cargo.toml | 2 +- serde/src/de/mod.rs | 8 ++++---- serde/src/private/de.rs | 4 ++-- serde_derive/Cargo.toml | 2 +- serde_derive_internals/Cargo.toml | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 41c11afa..b09c5f07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "serde_derive_internals", "test_suite", ] +resolver = "2" [patch.crates-io] serde = { path = "serde" } diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 8c81e0f1..ae2855c5 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -6,7 +6,7 @@ build = "build.rs" categories = ["encoding", "no-std", "no-std::no-alloc"] description = "A generic serialization/deserialization framework" documentation = "https://docs.rs/serde" -edition = "2018" +edition = "2021" homepage = "https://serde.rs" keywords = ["serde", "serialization", "no_std"] license = "MIT OR Apache-2.0" diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 540632f4..b2cc64c8 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -206,7 +206,7 @@ macro_rules! declare_error_trait { /// containing an integer, the unexpected type is the integer and the /// expected type is the string. #[cold] - fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self { + fn invalid_type(unexp: Unexpected, exp: &dyn Expected) -> Self { Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp)) } @@ -224,7 +224,7 @@ macro_rules! declare_error_trait { /// that is not valid UTF-8, the unexpected value is the bytes and the /// expected value is a string. #[cold] - fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self { + fn invalid_value(unexp: Unexpected, exp: &dyn Expected) -> Self { Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp)) } @@ -238,7 +238,7 @@ macro_rules! declare_error_trait { /// expected. For example `exp` might say that a tuple of size 6 was /// expected. #[cold] - fn invalid_length(len: usize, exp: &Expected) -> Self { + fn invalid_length(len: usize, exp: &dyn Expected) -> Self { Error::custom(format_args!("invalid length {}, expected {}", len, exp)) } @@ -492,7 +492,7 @@ impl Expected for &str { } } -impl Display for Expected + '_ { +impl Display for dyn Expected + '_ { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { Expected::fmt(self, formatter) } diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 50ae6ed1..af5878bb 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1065,7 +1065,7 @@ mod content { E: de::Error, { #[cold] - fn invalid_type(self, exp: &Expected) -> E { + fn invalid_type(self, exp: &dyn Expected) -> E { de::Error::invalid_type(self.content.unexpected(), exp) } @@ -1654,7 +1654,7 @@ mod content { E: de::Error, { #[cold] - fn invalid_type(self, exp: &Expected) -> E { + fn invalid_type(self, exp: &dyn Expected) -> E { de::Error::invalid_type(self.content.unexpected(), exp) } diff --git a/serde_derive/Cargo.toml b/serde_derive/Cargo.toml index 4242312a..bce32311 100644 --- a/serde_derive/Cargo.toml +++ b/serde_derive/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Erick Tryzelaar ", "David Tolnay ", "David Tolnay "] description = "AST representation used by Serde derive macros. Unstable." documentation = "https://docs.rs/serde_derive_internals" -edition = "2015" +edition = "2021" exclude = ["build.rs"] homepage = "https://serde.rs" keywords = ["serde", "serialization"] From 8b0e95b6ded306475ca4e49a6179b349d02070e2 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 5 Mar 2025 10:07:07 +0000 Subject: [PATCH 4/4] clippy --- serde/src/de/impls.rs | 43 +++++++++++++++++++-------------------- serde/src/de/size_hint.rs | 1 + serde/src/lib.rs | 2 -- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 001b8210..f62b0f16 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -249,11 +249,8 @@ macro_rules! int_to_int { where E: Error, { - if Self::Value::MIN as i64 <= v as i64 && v as i64 <= Self::Value::MAX as i64 { - Ok(v as Self::Value) - } else { - Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) - } + Self::Value::try_from(v as i64) + .map_err(|_| Error::invalid_value(Unexpected::Signed(v as i64), &self)) } }; @@ -262,8 +259,8 @@ macro_rules! int_to_int { where E: Error, { - if $primitive::MIN as i64 <= v as i64 && v as i64 <= $primitive::MAX as i64 { - if let Some(nonzero) = Self::Value::new(v as $primitive) { + if let Ok(v) = $primitive::try_from(v as i64) { + if let Some(nonzero) = Self::Value::new(v) { return Ok(nonzero); } } @@ -294,11 +291,13 @@ macro_rules! int_to_uint { where E: Error, { - if 0 <= v && v as u64 <= Self::Value::MAX as u64 { - Ok(v as Self::Value) - } else { - Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) + if 0 <= v { + #[allow(irrefutable_let_patterns)] + if let Ok(v) = Self::Value::try_from(v as u64) { + return Ok(v as Self::Value); + } } + Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) } }; @@ -307,9 +306,12 @@ macro_rules! int_to_uint { where E: Error, { - if 0 < v && v as u64 <= $primitive::MAX as u64 { - if let Some(nonzero) = Self::Value::new(v as $primitive) { - return Ok(nonzero); + if 0 < v { + #[allow(irrefutable_let_patterns)] + if let Ok(v) = $primitive::try_from(v as u64) { + if let Some(nonzero) = Self::Value::new(v) { + return Ok(nonzero); + } } } Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) @@ -339,11 +341,8 @@ macro_rules! uint_to_self { where E: Error, { - if v as u64 <= Self::Value::MAX as u64 { - Ok(v as Self::Value) - } else { - Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) - } + Self::Value::try_from(v as u64) + .map_err(|_| Error::invalid_value(Unexpected::Unsigned(v as u64), &self)) } }; @@ -352,8 +351,8 @@ macro_rules! uint_to_self { where E: Error, { - if v as u64 <= $primitive::MAX as u64 { - if let Some(nonzero) = Self::Value::new(v as $primitive) { + if let Ok(v) = $primitive::try_from(v as u64) { + if let Some(nonzero) = Self::Value::new(v) { return Ok(nonzero); } } @@ -366,7 +365,7 @@ macro_rules! uint_to_self { where E: Error, { - if v as u64 <= $primitive::MAX as u64 { + if let Ok(v) = $primitive::try_from(v as u64) { Ok(Saturating(v as $primitive)) } else { Ok(Saturating($primitive::MAX)) diff --git a/serde/src/de/size_hint.rs b/serde/src/de/size_hint.rs index 4a4fe25d..783281b4 100644 --- a/serde/src/de/size_hint.rs +++ b/serde/src/de/size_hint.rs @@ -1,3 +1,4 @@ +#[cfg(any(feature = "std", feature = "alloc"))] use crate::lib::*; pub fn from_bounds(iter: &I) -> Option diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 52a427c6..84b5cd7e 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -175,9 +175,7 @@ mod lib { } pub use self::core::{f32, f64}; - pub use self::core::{i16, i32, i64, i8, isize}; pub use self::core::{iter, num, ptr, str}; - pub use self::core::{u16, u32, u64, u8, usize}; #[cfg(any(feature = "std", feature = "alloc"))] pub use self::core::{cmp, mem, slice};