]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Fix no-std feature configuration to auto-fallback from std to alloc (#651)
authorDilshod Tadjibaev <939125+antimora@users.noreply.github.com>
Mon, 8 Sep 2025 08:24:14 +0000 (03:24 -0500)
committerGitHub <noreply@github.com>
Mon, 8 Sep 2025 08:24:14 +0000 (10:24 +0200)
* 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.

.github/workflows/rust.yml
safetensors/Cargo.toml
safetensors/src/lib.rs

index a1eb3e60347f7ae943c3d0af202afe65ab9b12e5..e38cc47acfe73cc5565a9da7bf894242876eb26c 100644 (file)
@@ -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
index 839d06e2f64d32167b3861af48f1b45398e344ec..e9d25ae9d62342af32f6b2dfc5d3af63b2c11ce9 100644 (file)
@@ -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"
index 4020c9dc21a1e1570d5624ba88c9faa73a590e12..f9ab8b76e389b50855e9a4dc79c91cb11ce4bfe1 100644 (file)
@@ -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.