Fix wasm32v1-none target compilation with Cargo feature unification

When building for wasm32v1-none with Cargo, the 'std' feature gets
unified across the dependency graph, meaning serde_core sees
feature="std" even on a no_std target. This caused compilation failures
because the crate tried to use std:: imports.

This commit fixes the issue by checking target_os = "none" in addition
to the std feature flag:
- lib.rs: Force no_std when target_os = "none"
- crate_root.rs: Use core/alloc instead of std on target_os = "none"
- All std-only cfg blocks now include not(target_os = "none")
- Add explicit prelude imports for wasm32v1-none compatibility
This commit is contained in:
2026-01-09 15:36:07 +03:00
parent d99c876120
commit 679af0fb4b
10 changed files with 116 additions and 110 deletions
+1 -2
View File
@@ -1,8 +1,7 @@
use crate::lib::*;
// Explicit prelude import for wasm32v1-none target compatibility
// Explicit prelude import for wasm32v1-none and other no_std targets
#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use ::core::prelude::rust_2021::*;
use crate::ser::{Error, Impossible, Serialize, Serializer};
+15 -15
View File
@@ -1,9 +1,9 @@
use crate::lib::*;
// Explicit prelude import for wasm32v1-none target compatibility
// These may appear unused but are required for ?Sized bounds on wasm32v1-none
// Explicit prelude import for wasm32v1-none and other no_std targets
// Even when "std" feature is enabled (due to Cargo feature unification),
// the prelude may not be injected on wasm32v1-none target
#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use ::core::prelude::rust_2021::*;
use crate::ser::{Error, Serialize, SerializeTuple, Serializer};
@@ -225,7 +225,7 @@ seq_impl! {
}
seq_impl! {
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
HashSet<T, H: BuildHasher>
}
@@ -456,7 +456,7 @@ map_impl! {
}
map_impl! {
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
HashMap<K: Eq + Hash, V, H: BuildHasher>
}
@@ -630,7 +630,7 @@ where
}
}
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> Serialize for Mutex<T>
where
@@ -647,7 +647,7 @@ where
}
}
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> Serialize for RwLock<T>
where
@@ -703,7 +703,7 @@ impl Serialize for Duration {
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Serialize for SystemTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -909,7 +909,7 @@ impl Serialize for net::SocketAddrV6 {
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Serialize for Path {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -923,7 +923,7 @@ impl Serialize for Path {
}
}
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Serialize for PathBuf {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -934,7 +934,7 @@ impl Serialize for PathBuf {
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
#[cfg(all(feature = "std", not(target_os = "none"), any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))]
impl Serialize for OsStr {
#[cfg(unix)]
@@ -957,7 +957,7 @@ impl Serialize for OsStr {
}
}
#[cfg(all(feature = "std", any(unix, windows)))]
#[cfg(all(feature = "std", not(target_os = "none"), any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))]
impl Serialize for OsString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -1012,7 +1012,7 @@ where
////////////////////////////////////////////////////////////////////////////////
#[cfg(all(feature = "std", not(no_std_atomic)))]
#[cfg(all(feature = "std", not(target_os = "none"), not(no_std_atomic)))]
macro_rules! atomic_impl {
($($ty:ident $size:expr)*) => {
$(
@@ -1031,7 +1031,7 @@ macro_rules! atomic_impl {
}
}
#[cfg(all(feature = "std", not(no_std_atomic)))]
#[cfg(all(feature = "std", not(target_os = "none"), not(no_std_atomic)))]
atomic_impl! {
AtomicBool "8"
AtomicI8 "8"
@@ -1044,7 +1044,7 @@ atomic_impl! {
AtomicUsize "ptr"
}
#[cfg(all(feature = "std", not(no_std_atomic64)))]
#[cfg(all(feature = "std", not(target_os = "none"), not(no_std_atomic64)))]
atomic_impl! {
AtomicI64 "64"
AtomicU64 "64"
+1 -2
View File
@@ -2,9 +2,8 @@
use crate::lib::*;
// Explicit prelude import for wasm32v1-none target compatibility
// Explicit prelude import for wasm32v1-none and other no_std targets
#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use ::core::prelude::rust_2021::*;
use crate::ser::{
+6 -7
View File
@@ -109,9 +109,8 @@
use crate::lib::*;
// Explicit prelude import for wasm32v1-none target compatibility
// Explicit prelude import for wasm32v1-none and other no_std targets
#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use ::core::prelude::rust_2021::*;
mod fmt;
@@ -120,13 +119,13 @@ mod impossible;
pub use self::impossible::Impossible;
#[cfg(all(not(feature = "std"), no_core_error))]
#[cfg(all(any(not(feature = "std"), target_os = "none"), no_core_error))]
#[doc(no_inline)]
pub use crate::std_error::Error as StdError;
#[cfg(not(any(feature = "std", no_core_error)))]
#[cfg(all(any(not(feature = "std"), target_os = "none"), not(no_core_error)))]
#[doc(no_inline)]
pub use core::error::Error as StdError;
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
#[doc(no_inline)]
pub use std::error::Error as StdError;
@@ -193,10 +192,10 @@ macro_rules! declare_error_trait {
}
}
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_os = "none")))]
declare_error_trait!(Error: Sized + StdError);
#[cfg(not(feature = "std"))]
#[cfg(any(not(feature = "std"), target_os = "none"))]
declare_error_trait!(Error: Sized + Debug + Display);
////////////////////////////////////////////////////////////////////////////////