Skip to content

Commit

Permalink
Merge pull request #2338 from serde-rs/atomic
Browse files Browse the repository at this point in the history
Deduplicate atomic_impl implementations and atomic_impl calls from PR 2337
  • Loading branch information
dtolnay committed Dec 12, 2022
2 parents 37faaf2 + 9249dab commit 0e947e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 81 deletions.
56 changes: 16 additions & 40 deletions serde/src/de/impls.rs
Expand Up @@ -2660,10 +2660,11 @@ where
}
}

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

#[cfg(all(feature = "std", not(no_target_has_atomic)))]
macro_rules! atomic_impl {
($($ty:ident $size:expr),*) => {
$(
#[cfg(target_has_atomic = $size)]
impl<'de> Deserialize<'de> for $ty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(deserializer).map(Self::new)
}
}
)*
};
}

#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
#[cfg(all(feature = "std", not(no_std_atomic)))]
atomic_impl! {
AtomicBool
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
}

#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
atomic_impl! {
AtomicI64 AtomicU64
AtomicBool "8"
AtomicI8 "8"
AtomicI16 "16"
AtomicI32 "32"
AtomicIsize "ptr"
AtomicU8 "8"
AtomicU16 "16"
AtomicU32 "32"
AtomicUsize "ptr"
}

#[cfg(all(feature = "std", not(no_target_has_atomic)))]
#[cfg(all(feature = "std", not(no_std_atomic64)))]
atomic_impl! {
AtomicBool "8",
AtomicI8 "8",
AtomicI16 "16",
AtomicI32 "32",
AtomicI64 "64",
AtomicIsize "ptr",
AtomicU8 "8",
AtomicU16 "16",
AtomicU32 "32",
AtomicU64 "64",
AtomicUsize "ptr"
AtomicI64 "64"
AtomicU64 "64"
}

#[cfg(feature = "std")]
Expand Down
57 changes: 16 additions & 41 deletions serde/src/ser/impls.rs
Expand Up @@ -945,10 +945,11 @@ where

////////////////////////////////////////////////////////////////////////////////

#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
#[cfg(all(feature = "std", not(no_std_atomic)))]
macro_rules! atomic_impl {
($($ty:ident)*) => {
($($ty:ident $size:expr)*) => {
$(
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
impl Serialize for $ty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -962,47 +963,21 @@ macro_rules! atomic_impl {
}
}

#[cfg(all(feature = "std", not(no_target_has_atomic)))]
macro_rules! atomic_impl {
($($ty:ident $size:expr),*) => {
$(
#[cfg(target_has_atomic = $size)]
impl Serialize for $ty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Matches the atomic ordering used in libcore for the Debug impl
self.load(Ordering::Relaxed).serialize(serializer)
}
}
)*
}
}

#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
#[cfg(all(feature = "std", not(no_std_atomic)))]
atomic_impl! {
AtomicBool
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
}

#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
atomic_impl! {
AtomicI64 AtomicU64
AtomicBool "8"
AtomicI8 "8"
AtomicI16 "16"
AtomicI32 "32"
AtomicIsize "ptr"
AtomicU8 "8"
AtomicU16 "16"
AtomicU32 "32"
AtomicUsize "ptr"
}

#[cfg(all(feature = "std", not(no_target_has_atomic)))]
#[cfg(all(feature = "std", not(no_std_atomic64)))]
atomic_impl! {
AtomicBool "8",
AtomicI8 "8",
AtomicI16 "16",
AtomicI32 "32",
AtomicI64 "64",
AtomicIsize "ptr",
AtomicU8 "8",
AtomicU16 "16",
AtomicU32 "32",
AtomicU64 "64",
AtomicUsize "ptr"
AtomicI64 "64"
AtomicU64 "64"
}

0 comments on commit 0e947e6

Please sign in to comment.