From bd4a0981ba4875d3960f2cc90c379213275373ed Mon Sep 17 00:00:00 2001 From: Charles-Xavier Roy Date: Fri, 10 Feb 2023 09:05:24 -0500 Subject: [PATCH 1/2] Enable CStr and CString in no-std enviroment --- serde/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serde/src/lib.rs b/serde/src/lib.rs index e37afe010..cfc78b5dd 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -176,6 +176,7 @@ 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; @@ -218,13 +219,18 @@ mod lib { #[cfg(feature = "std")] pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; + #[cfg(all(feature = "alloc", not(feature = "std")))] + pub use alloc::ffi::{CString}; + #[cfg(feature = "std")] + pub use std::ffi::{CString}; + #[cfg(feature = "std")] pub use std::{error, net}; #[cfg(feature = "std")] pub use std::collections::{HashMap, HashSet}; #[cfg(feature = "std")] - pub use std::ffi::{CStr, CString, OsStr, OsString}; + pub use std::ffi::{OsStr, OsString}; #[cfg(feature = "std")] pub use std::hash::{BuildHasher, Hash}; #[cfg(feature = "std")] From f7636428ed9a93c52fb6c37473afd5e8ce860c8a Mon Sep 17 00:00:00 2001 From: Charles-Xavier Roy Date: Fri, 17 Feb 2023 16:25:16 -0500 Subject: [PATCH 2/2] Add check for rust version --- serde/build.rs | 5 +++++ serde/src/de/impls.rs | 8 ++++---- serde/src/lib.rs | 10 +++++++--- serde/src/ser/impls.rs | 4 ++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/serde/build.rs b/serde/build.rs index ccbddd273..3fd65dd46 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -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 { diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index a257d9e2f..b366e891e 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -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; @@ -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(deserializer: D) -> Result where @@ -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, CString::into_boxed_c_str); #[cfg(not(no_core_reverse))] diff --git a/serde/src/lib.rs b/serde/src/lib.rs index cfc78b5dd..dd9505118 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -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; @@ -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}; diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index da2677261..a79326e5c 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -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(&self, serializer: S) -> Result @@ -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(&self, serializer: S) -> Result