From: Dilshod Tadjibaev <939125+antimora@users.noreply.github.com> Date: Mon, 8 Sep 2025 08:24:14 +0000 (-0500) Subject: Fix no-std feature configuration to auto-fallback from std to alloc (#651) X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=0816a1ae1d6b731cefd67f061d80d1cadd0dd7bb;p=safetensors.git Fix no-std feature configuration to auto-fallback from std to alloc (#651) * Refactor feature flags for alloc and std support Updated Cargo.toml to always enable alloc support and removed the alloc feature from dependencies. The alloc feature is now a no-op for backward compatibility. Adjusted lib.rs to use alloc when std is not enabled and removed compile-time errors enforcing feature exclusivity. * Update no-std test command in CI workflow Removes the explicit 'alloc' feature from the no-std test command in the GitHub Actions workflow, running tests with only --no-default-features. This may be to ensure tests run in a stricter no-std environment or to match updated feature requirements. --- diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a1eb3e6..e38cc47 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -46,7 +46,7 @@ jobs: run: cargo test --verbose - name: Run No-STD Tests - run: cargo test --no-default-features --features alloc --verbose + run: cargo test --no-default-features --verbose - name: Run Audit # RUSTSEC-2021-0145 is criterion so only within benchmarks diff --git a/safetensors/Cargo.toml b/safetensors/Cargo.toml index 839d06e..e9d25ae 100644 --- a/safetensors/Cargo.toml +++ b/safetensors/Cargo.toml @@ -16,15 +16,18 @@ The format is 8 bytes which is an unsized int, being the size of a JSON header, the JSON header refers the `dtype` the `shape` and `data_offsets` which are the offsets for the values in the rest of the file. """ -exclude = [ "rust-toolchain", "target/*", "Cargo.lock"] +exclude = ["rust-toolchain", "target/*", "Cargo.lock"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -hashbrown = { version = "0.15.4", features = ["serde"], optional = true } -serde = { version = "1.0", default-features = false, features = ["derive"] } -serde_json = { version = "1.0", default-features = false } +serde = { version = "1.0", default-features = false, features = [ + "derive", + "alloc", +] } +serde_json = { version = "1.0", default-features = false, features = ["alloc"] } +hashbrown = { version = "0.15.4", features = ["serde"] } [dev-dependencies] criterion = "0.6" @@ -34,7 +37,8 @@ proptest = "1.7" [features] default = ["std"] std = ["serde/default", "serde_json/default"] -alloc = ["serde/alloc", "serde_json/alloc", "hashbrown"] +# Kept for backward compatibility - no-op since alloc is always available +alloc = [] [[bench]] name = "benchmark" diff --git a/safetensors/src/lib.rs b/safetensors/src/lib.rs index 4020c9d..f9ab8b7 100644 --- a/safetensors/src/lib.rs +++ b/safetensors/src/lib.rs @@ -8,15 +8,10 @@ pub mod tensor; pub use tensor::serialize_to_file; pub use tensor::{serialize, Dtype, SafeTensorError, SafeTensors, View}; -#[cfg(feature = "alloc")] +#[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; -#[cfg(all(feature = "std", feature = "alloc"))] -compile_error!("must choose either the `std` or `alloc` feature, but not both."); -#[cfg(all(not(feature = "std"), not(feature = "alloc")))] -compile_error!("must choose either the `std` or `alloc` feature"); - /// A facade around all the types we need from the `std`, `core`, and `alloc` /// crates. This avoids elaborate import wrangling having to happen in every /// module.