mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 02:21:01 +00:00
Merge pull request #2337 from badboy/use-target_has_atomic-when-available
Use `target_has_atomic` on Rust 1.60+ to enable atomic (de)serialization
This commit is contained in:
+17
-13
@@ -95,19 +95,23 @@ fn main() {
|
|||||||
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
||||||
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
||||||
// Instead this is based on rustc's compiler/rustc_target/src/spec/*.rs.
|
// Instead this is based on rustc's compiler/rustc_target/src/spec/*.rs.
|
||||||
let has_atomic64 = target.starts_with("x86_64")
|
if minor >= 60 {
|
||||||
|| target.starts_with("i686")
|
println!("cargo:rustc-cfg=use_target_has_atomic");
|
||||||
|| target.starts_with("aarch64")
|
} else {
|
||||||
|| target.starts_with("powerpc64")
|
let has_atomic64 = target.starts_with("x86_64")
|
||||||
|| target.starts_with("sparc64")
|
|| target.starts_with("i686")
|
||||||
|| target.starts_with("mips64el")
|
|| target.starts_with("aarch64")
|
||||||
|| target.starts_with("riscv64");
|
|| target.starts_with("powerpc64")
|
||||||
let has_atomic32 = has_atomic64 || emscripten;
|
|| target.starts_with("sparc64")
|
||||||
if minor < 34 || !has_atomic64 {
|
|| target.starts_with("mips64el")
|
||||||
println!("cargo:rustc-cfg=no_std_atomic64");
|
|| target.starts_with("riscv64");
|
||||||
}
|
let has_atomic32 = has_atomic64 || emscripten;
|
||||||
if minor < 34 || !has_atomic32 {
|
if minor < 34 || !has_atomic64 {
|
||||||
println!("cargo:rustc-cfg=no_std_atomic");
|
println!("cargo:rustc-cfg=no_std_atomic64");
|
||||||
|
}
|
||||||
|
if minor < 34 || !has_atomic32 {
|
||||||
|
println!("cargo:rustc-cfg=no_std_atomic");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+35
-3
@@ -2660,7 +2660,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic)))]
|
||||||
macro_rules! atomic_impl {
|
macro_rules! atomic_impl {
|
||||||
($($ty:ident)*) => {
|
($($ty:ident)*) => {
|
||||||
$(
|
$(
|
||||||
@@ -2676,18 +2676,50 @@ macro_rules! atomic_impl {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
#[cfg(all(feature = "std", use_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", not(use_target_has_atomic), not(no_std_atomic)))]
|
||||||
atomic_impl! {
|
atomic_impl! {
|
||||||
AtomicBool
|
AtomicBool
|
||||||
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
||||||
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic64)))]
|
||||||
atomic_impl! {
|
atomic_impl! {
|
||||||
AtomicI64 AtomicU64
|
AtomicI64 AtomicU64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", use_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")]
|
#[cfg(feature = "std")]
|
||||||
struct FromStrVisitor<T> {
|
struct FromStrVisitor<T> {
|
||||||
expecting: &'static str,
|
expecting: &'static str,
|
||||||
|
|||||||
+15
-2
@@ -236,14 +236,27 @@ mod lib {
|
|||||||
#[cfg(not(no_range_inclusive))]
|
#[cfg(not(no_range_inclusive))]
|
||||||
pub use self::core::ops::RangeInclusive;
|
pub use self::core::ops::RangeInclusive;
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic)))]
|
||||||
pub use std::sync::atomic::{
|
pub use std::sync::atomic::{
|
||||||
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
|
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
|
||||||
AtomicUsize, Ordering,
|
AtomicUsize, Ordering,
|
||||||
};
|
};
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic64)))]
|
||||||
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic))]
|
||||||
|
pub use std::sync::atomic::Ordering;
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic, target_has_atomic = "8"))]
|
||||||
|
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic, target_has_atomic = "16"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI16, AtomicU16};
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic, target_has_atomic = "32"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI32, AtomicU32};
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic, target_has_atomic = "64"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||||
|
#[cfg(all(feature = "std", use_target_has_atomic, target_has_atomic = "ptr"))]
|
||||||
|
pub use std::sync::atomic::{AtomicUsize, AtomicIsize};
|
||||||
|
|
||||||
#[cfg(any(feature = "std", not(no_core_duration)))]
|
#[cfg(any(feature = "std", not(no_core_duration)))]
|
||||||
pub use self::core::time::Duration;
|
pub use self::core::time::Duration;
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-3
@@ -945,7 +945,7 @@ where
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic)))]
|
||||||
macro_rules! atomic_impl {
|
macro_rules! atomic_impl {
|
||||||
($($ty:ident)*) => {
|
($($ty:ident)*) => {
|
||||||
$(
|
$(
|
||||||
@@ -962,14 +962,47 @@ macro_rules! atomic_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
#[cfg(all(feature = "std", use_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", not(use_target_has_atomic), not(no_std_atomic)))]
|
||||||
atomic_impl! {
|
atomic_impl! {
|
||||||
AtomicBool
|
AtomicBool
|
||||||
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
||||||
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
#[cfg(all(feature = "std", not(use_target_has_atomic), not(no_std_atomic64)))]
|
||||||
atomic_impl! {
|
atomic_impl! {
|
||||||
AtomicI64 AtomicU64
|
AtomicI64 AtomicU64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", use_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"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user