Merge pull request #2374 from safarir/master

Enable CStr and CString in no-std enviroment
This commit is contained in:
David Tolnay 2023-03-11 12:55:49 -08:00 committed by GitHub
commit f4dcc5c918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View File

@ -114,6 +114,11 @@ fn main() {
println!("cargo:rustc-cfg=no_std_atomic"); 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> { fn rustc_minor_version() -> Option<u32> {

View File

@ -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; struct CStringVisitor;
#[cfg(feature = "std")] #[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
impl<'de> Visitor<'de> for CStringVisitor { impl<'de> Visitor<'de> for CStringVisitor {
type Value = CString; 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 { impl<'de> Deserialize<'de> for CString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where 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<CStr>, CString::into_boxed_c_str); forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
#[cfg(not(no_core_reverse))] #[cfg(not(no_core_reverse))]

View File

@ -219,13 +219,23 @@ mod lib {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
#[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;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::{error, net}; pub use std::{error, net};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::collections::{HashMap, HashSet}; pub use std::collections::{HashMap, HashSet};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::ffi::{CStr, CString, OsStr, OsString}; pub use std::ffi::{OsStr, OsString};
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std::hash::{BuildHasher, Hash}; pub use std::hash::{BuildHasher, Hash};
#[cfg(feature = "std")] #[cfg(feature = "std")]

View File

@ -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 { impl Serialize for CStr {
#[inline] #[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -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 { impl Serialize for CString {
#[inline] #[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>