Skip to content

Commit

Permalink
Add check for rust version
Browse files Browse the repository at this point in the history
  • Loading branch information
safarir committed Feb 17, 2023
1 parent bd4a098 commit f763642
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions serde/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ fn main() {
println!("cargo:rustc-cfg=no_std_atomic");
}
}

// Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64.
if minor < 64 {
println!("cargo:rustc-cfg=no_core_cstr");
}
}

fn rustc_minor_version() -> Option<u32> {
Expand Down
8 changes: 4 additions & 4 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,10 @@ impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {

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

#[cfg(feature = "std")]
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
struct CStringVisitor;

#[cfg(feature = "std")]
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
impl<'de> Visitor<'de> for CStringVisitor {
type Value = CString;

Expand Down Expand Up @@ -720,7 +720,7 @@ impl<'de> Visitor<'de> for CStringVisitor {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
impl<'de> Deserialize<'de> for CString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -747,7 +747,7 @@ macro_rules! forwarded_impl {
}
}

#[cfg(all(feature = "std", not(no_de_boxed_c_str)))]
#[cfg(all(any(feature = "std", all(not(no_core_cstr), feature = "alloc")), not(no_de_boxed_c_str)))]
forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);

#[cfg(not(no_core_reverse))]
Expand Down
10 changes: 7 additions & 3 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ mod lib {
pub use self::core::clone::{self, Clone};
pub use self::core::convert::{self, From, Into};
pub use self::core::default::{self, Default};
pub use self::core::ffi::CStr;
pub use self::core::fmt::{self, Debug, Display};
pub use self::core::marker::{self, PhantomData};
pub use self::core::num::Wrapping;
Expand Down Expand Up @@ -219,10 +218,15 @@ mod lib {
#[cfg(feature = "std")]
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};

#[cfg(all(feature = "alloc", not(feature = "std")))]
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
pub use core::ffi::CStr;
#[cfg(feature = "std")]
pub use std::ffi::CStr;

#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
pub use alloc::ffi::{CString};
#[cfg(feature = "std")]
pub use std::ffi::{CString};
pub use std::ffi::CString;

#[cfg(feature = "std")]
pub use std::{error, net};
Expand Down
4 changes: 2 additions & 2 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a> Serialize for fmt::Arguments<'a> {

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

#[cfg(feature = "std")]
#[cfg(any(feature = "std", not(no_core_cstr)))]
impl Serialize for CStr {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -83,7 +83,7 @@ impl Serialize for CStr {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
impl Serialize for CString {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down

0 comments on commit f763642

Please sign in to comment.