From 0d7930628517468ab3b00bfb7e3b575f0bb5c849 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 11 Dec 2022 16:01:39 -0800 Subject: [PATCH 1/3] Update atomic_impl macros to have same input syntax in all cfgs --- serde/src/de/impls.rs | 39 +++++++++++++++++++++++---------------- serde/src/ser/impls.rs | 39 +++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 1b93f6381..b2311e1dc 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -2662,7 +2662,7 @@ where #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] macro_rules! atomic_impl { - ($($ty:ident)*) => { + ($($ty:ident $size:expr)*) => { $( impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result @@ -2678,7 +2678,7 @@ macro_rules! atomic_impl { #[cfg(all(feature = "std", not(no_target_has_atomic)))] macro_rules! atomic_impl { - ($($ty:ident $size:expr),*) => { + ($($ty:ident $size:expr)*) => { $( #[cfg(target_has_atomic = $size)] impl<'de> Deserialize<'de> for $ty { @@ -2695,28 +2695,35 @@ macro_rules! atomic_impl { #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] atomic_impl! { - AtomicBool - AtomicI8 AtomicI16 AtomicI32 AtomicIsize - AtomicU8 AtomicU16 AtomicU32 AtomicUsize + AtomicBool "8" + AtomicI8 "8" + AtomicI16 "16" + AtomicI32 "32" + AtomicIsize "ptr" + AtomicU8 "8" + AtomicU16 "16" + AtomicU32 "32" + AtomicUsize "ptr" } #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))] atomic_impl! { - AtomicI64 AtomicU64 + AtomicI64 "64" + AtomicU64 "64" } #[cfg(all(feature = "std", not(no_target_has_atomic)))] atomic_impl! { - AtomicBool "8", - AtomicI8 "8", - AtomicI16 "16", - AtomicI32 "32", - AtomicI64 "64", - AtomicIsize "ptr", - AtomicU8 "8", - AtomicU16 "16", - AtomicU32 "32", - AtomicU64 "64", + AtomicBool "8" + AtomicI8 "8" + AtomicI16 "16" + AtomicI32 "32" + AtomicI64 "64" + AtomicIsize "ptr" + AtomicU8 "8" + AtomicU16 "16" + AtomicU32 "32" + AtomicU64 "64" AtomicUsize "ptr" } diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index e3434460f..4698827be 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -947,7 +947,7 @@ where #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] macro_rules! atomic_impl { - ($($ty:ident)*) => { + ($($ty:ident $size:expr)*) => { $( impl Serialize for $ty { fn serialize(&self, serializer: S) -> Result @@ -964,7 +964,7 @@ macro_rules! atomic_impl { #[cfg(all(feature = "std", not(no_target_has_atomic)))] macro_rules! atomic_impl { - ($($ty:ident $size:expr),*) => { + ($($ty:ident $size:expr)*) => { $( #[cfg(target_has_atomic = $size)] impl Serialize for $ty { @@ -982,27 +982,34 @@ macro_rules! atomic_impl { #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] atomic_impl! { - AtomicBool - AtomicI8 AtomicI16 AtomicI32 AtomicIsize - AtomicU8 AtomicU16 AtomicU32 AtomicUsize + AtomicBool "8" + AtomicI8 "8" + AtomicI16 "16" + AtomicI32 "32" + AtomicIsize "ptr" + AtomicU8 "8" + AtomicU16 "16" + AtomicU32 "32" + AtomicUsize "ptr" } #[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))] atomic_impl! { - AtomicI64 AtomicU64 + AtomicI64 "64" + AtomicU64 "64" } #[cfg(all(feature = "std", not(no_target_has_atomic)))] atomic_impl! { - AtomicBool "8", - AtomicI8 "8", - AtomicI16 "16", - AtomicI32 "32", - AtomicI64 "64", - AtomicIsize "ptr", - AtomicU8 "8", - AtomicU16 "16", - AtomicU32 "32", - AtomicU64 "64", + AtomicBool "8" + AtomicI8 "8" + AtomicI16 "16" + AtomicI32 "32" + AtomicI64 "64" + AtomicIsize "ptr" + AtomicU8 "8" + AtomicU16 "16" + AtomicU32 "32" + AtomicU64 "64" AtomicUsize "ptr" } From 7440e56c5383724ff4d24fb77778d7c1a66e07d1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 11 Dec 2022 16:09:26 -0800 Subject: [PATCH 2/3] Deduplicate atomic_impl macro implementations --- serde/src/de/impls.rs | 20 ++------------------ serde/src/ser/impls.rs | 21 ++------------------- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index b2311e1dc..9621dcc43 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -2660,27 +2660,11 @@ where } } -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] -macro_rules! atomic_impl { - ($($ty:ident $size:expr)*) => { - $( - impl<'de> Deserialize<'de> for $ty { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - Deserialize::deserialize(deserializer).map(Self::new) - } - } - )* - }; -} - -#[cfg(all(feature = "std", not(no_target_has_atomic)))] +#[cfg(all(feature = "std", not(no_std_atomic)))] macro_rules! atomic_impl { ($($ty:ident $size:expr)*) => { $( - #[cfg(target_has_atomic = $size)] + #[cfg(any(no_target_has_atomic, target_has_atomic = $size))] impl<'de> Deserialize<'de> for $ty { fn deserialize(deserializer: D) -> Result where diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 4698827be..81c566959 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -945,28 +945,11 @@ where //////////////////////////////////////////////////////////////////////////////// -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] -macro_rules! atomic_impl { - ($($ty:ident $size:expr)*) => { - $( - impl Serialize for $ty { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - // Matches the atomic ordering used in libcore for the Debug impl - self.load(Ordering::Relaxed).serialize(serializer) - } - } - )* - } -} - -#[cfg(all(feature = "std", not(no_target_has_atomic)))] +#[cfg(all(feature = "std", not(no_std_atomic)))] macro_rules! atomic_impl { ($($ty:ident $size:expr)*) => { $( - #[cfg(target_has_atomic = $size)] + #[cfg(any(no_target_has_atomic, target_has_atomic = $size))] impl Serialize for $ty { fn serialize(&self, serializer: S) -> Result where From 9249dab54cc3f497b6ae3494423a92f6a3857599 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 11 Dec 2022 16:11:01 -0800 Subject: [PATCH 3/3] Deduplicate atomic_impl macro calls --- serde/src/de/impls.rs | 19 ++----------------- serde/src/ser/impls.rs | 19 ++----------------- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 9621dcc43..a257d9e2f 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -2677,7 +2677,7 @@ macro_rules! atomic_impl { }; } -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] +#[cfg(all(feature = "std", not(no_std_atomic)))] atomic_impl! { AtomicBool "8" AtomicI8 "8" @@ -2690,27 +2690,12 @@ atomic_impl! { AtomicUsize "ptr" } -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))] +#[cfg(all(feature = "std", not(no_std_atomic64)))] atomic_impl! { AtomicI64 "64" AtomicU64 "64" } -#[cfg(all(feature = "std", not(no_target_has_atomic)))] -atomic_impl! { - AtomicBool "8" - AtomicI8 "8" - AtomicI16 "16" - AtomicI32 "32" - AtomicI64 "64" - AtomicIsize "ptr" - AtomicU8 "8" - AtomicU16 "16" - AtomicU32 "32" - AtomicU64 "64" - AtomicUsize "ptr" -} - #[cfg(feature = "std")] struct FromStrVisitor { expecting: &'static str, diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 81c566959..da2677261 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -963,7 +963,7 @@ macro_rules! atomic_impl { } } -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))] +#[cfg(all(feature = "std", not(no_std_atomic)))] atomic_impl! { AtomicBool "8" AtomicI8 "8" @@ -976,23 +976,8 @@ atomic_impl! { AtomicUsize "ptr" } -#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))] +#[cfg(all(feature = "std", not(no_std_atomic64)))] atomic_impl! { AtomicI64 "64" AtomicU64 "64" } - -#[cfg(all(feature = "std", not(no_target_has_atomic)))] -atomic_impl! { - AtomicBool "8" - AtomicI8 "8" - AtomicI16 "16" - AtomicI32 "32" - AtomicI64 "64" - AtomicIsize "ptr" - AtomicU8 "8" - AtomicU16 "16" - AtomicU32 "32" - AtomicU64 "64" - AtomicUsize "ptr" -}