Skip to content

Commit f4dcc5c

Browse files
authored
Merge pull request #2374 from safarir/master
Enable CStr and CString in no-std enviroment
2 parents 8b1887c + f763642 commit f4dcc5c

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

serde/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ fn main() {
114114
println!("cargo:rustc-cfg=no_std_atomic");
115115
}
116116
}
117+
118+
// Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64.
119+
if minor < 64 {
120+
println!("cargo:rustc-cfg=no_core_cstr");
121+
}
117122
}
118123

119124
fn rustc_minor_version() -> Option<u32> {

serde/src/de/impls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,10 @@ impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {
666666

667667
////////////////////////////////////////////////////////////////////////////////
668668

669-
#[cfg(feature = "std")]
669+
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
670670
struct CStringVisitor;
671671

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

@@ -720,7 +720,7 @@ impl<'de> Visitor<'de> for CStringVisitor {
720720
}
721721
}
722722

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

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

753753
#[cfg(not(no_core_reverse))]

serde/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,23 @@ mod lib {
219219
#[cfg(feature = "std")]
220220
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
221221

222+
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
223+
pub use core::ffi::CStr;
224+
#[cfg(feature = "std")]
225+
pub use std::ffi::CStr;
226+
227+
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
228+
pub use alloc::ffi::{CString};
229+
#[cfg(feature = "std")]
230+
pub use std::ffi::CString;
231+
222232
#[cfg(feature = "std")]
223233
pub use std::{error, net};
224234

225235
#[cfg(feature = "std")]
226236
pub use std::collections::{HashMap, HashSet};
227237
#[cfg(feature = "std")]
228-
pub use std::ffi::{CStr, CString, OsStr, OsString};
238+
pub use std::ffi::{OsStr, OsString};
229239
#[cfg(feature = "std")]
230240
pub use std::hash::{BuildHasher, Hash};
231241
#[cfg(feature = "std")]

serde/src/ser/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a> Serialize for fmt::Arguments<'a> {
7272

7373
////////////////////////////////////////////////////////////////////////////////
7474

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

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

0 commit comments

Comments
 (0)