Skip to content

Commit 0e947e6

Browse files
authored
Merge pull request #2338 from serde-rs/atomic
Deduplicate atomic_impl implementations and atomic_impl calls from PR 2337
2 parents 37faaf2 + 9249dab commit 0e947e6

2 files changed

Lines changed: 32 additions & 81 deletions

File tree

‎serde/src/de/impls.rs‎

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,10 +2660,11 @@ where
26602660
}
26612661
}
26622662

2663-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
2663+
#[cfg(all(feature = "std", not(no_std_atomic)))]
26642664
macro_rules! atomic_impl {
2665-
($($ty:ident)*) => {
2665+
($($ty:ident $size:expr)*) => {
26662666
$(
2667+
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
26672668
impl<'de> Deserialize<'de> for $ty {
26682669
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
26692670
where
@@ -2676,48 +2677,23 @@ macro_rules! atomic_impl {
26762677
};
26772678
}
26782679

2679-
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
2680-
macro_rules! atomic_impl {
2681-
($($ty:ident $size:expr),*) => {
2682-
$(
2683-
#[cfg(target_has_atomic = $size)]
2684-
impl<'de> Deserialize<'de> for $ty {
2685-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2686-
where
2687-
D: Deserializer<'de>,
2688-
{
2689-
Deserialize::deserialize(deserializer).map(Self::new)
2690-
}
2691-
}
2692-
)*
2693-
};
2694-
}
2695-
2696-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
2680+
#[cfg(all(feature = "std", not(no_std_atomic)))]
26972681
atomic_impl! {
2698-
AtomicBool
2699-
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
2700-
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
2701-
}
2702-
2703-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
2704-
atomic_impl! {
2705-
AtomicI64 AtomicU64
2682+
AtomicBool "8"
2683+
AtomicI8 "8"
2684+
AtomicI16 "16"
2685+
AtomicI32 "32"
2686+
AtomicIsize "ptr"
2687+
AtomicU8 "8"
2688+
AtomicU16 "16"
2689+
AtomicU32 "32"
2690+
AtomicUsize "ptr"
27062691
}
27072692

2708-
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
2693+
#[cfg(all(feature = "std", not(no_std_atomic64)))]
27092694
atomic_impl! {
2710-
AtomicBool "8",
2711-
AtomicI8 "8",
2712-
AtomicI16 "16",
2713-
AtomicI32 "32",
2714-
AtomicI64 "64",
2715-
AtomicIsize "ptr",
2716-
AtomicU8 "8",
2717-
AtomicU16 "16",
2718-
AtomicU32 "32",
2719-
AtomicU64 "64",
2720-
AtomicUsize "ptr"
2695+
AtomicI64 "64"
2696+
AtomicU64 "64"
27212697
}
27222698

27232699
#[cfg(feature = "std")]

‎serde/src/ser/impls.rs‎

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,11 @@ where
945945

946946
////////////////////////////////////////////////////////////////////////////////
947947

948-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
948+
#[cfg(all(feature = "std", not(no_std_atomic)))]
949949
macro_rules! atomic_impl {
950-
($($ty:ident)*) => {
950+
($($ty:ident $size:expr)*) => {
951951
$(
952+
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
952953
impl Serialize for $ty {
953954
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
954955
where
@@ -962,47 +963,21 @@ macro_rules! atomic_impl {
962963
}
963964
}
964965

965-
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
966-
macro_rules! atomic_impl {
967-
($($ty:ident $size:expr),*) => {
968-
$(
969-
#[cfg(target_has_atomic = $size)]
970-
impl Serialize for $ty {
971-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
972-
where
973-
S: Serializer,
974-
{
975-
// Matches the atomic ordering used in libcore for the Debug impl
976-
self.load(Ordering::Relaxed).serialize(serializer)
977-
}
978-
}
979-
)*
980-
}
981-
}
982-
983-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
966+
#[cfg(all(feature = "std", not(no_std_atomic)))]
984967
atomic_impl! {
985-
AtomicBool
986-
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
987-
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
988-
}
989-
990-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
991-
atomic_impl! {
992-
AtomicI64 AtomicU64
968+
AtomicBool "8"
969+
AtomicI8 "8"
970+
AtomicI16 "16"
971+
AtomicI32 "32"
972+
AtomicIsize "ptr"
973+
AtomicU8 "8"
974+
AtomicU16 "16"
975+
AtomicU32 "32"
976+
AtomicUsize "ptr"
993977
}
994978

995-
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
979+
#[cfg(all(feature = "std", not(no_std_atomic64)))]
996980
atomic_impl! {
997-
AtomicBool "8",
998-
AtomicI8 "8",
999-
AtomicI16 "16",
1000-
AtomicI32 "32",
1001-
AtomicI64 "64",
1002-
AtomicIsize "ptr",
1003-
AtomicU8 "8",
1004-
AtomicU16 "16",
1005-
AtomicU32 "32",
1006-
AtomicU64 "64",
1007-
AtomicUsize "ptr"
981+
AtomicI64 "64"
982+
AtomicU64 "64"
1008983
}

0 commit comments

Comments
 (0)