Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate atomic_impl implementations and atomic_impl calls from PR 2337 #2338

Merged
merged 3 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}