Skip to content

Commit 0816a1a

Browse files
authored
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.
1 parent 0e01511 commit 0816a1a

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
run: cargo test --verbose
4747

4848
- name: Run No-STD Tests
49-
run: cargo test --no-default-features --features alloc --verbose
49+
run: cargo test --no-default-features --verbose
5050

5151
- name: Run Audit
5252
# RUSTSEC-2021-0145 is criterion so only within benchmarks

safetensors/Cargo.toml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ The format is 8 bytes which is an unsized int, being the size of a JSON header,
1616
the JSON header refers the `dtype` the `shape` and `data_offsets` which are the offsets
1717
for the values in the rest of the file.
1818
"""
19-
exclude = [ "rust-toolchain", "target/*", "Cargo.lock"]
19+
exclude = ["rust-toolchain", "target/*", "Cargo.lock"]
2020

2121

2222
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2323

2424
[dependencies]
25-
hashbrown = { version = "0.15.4", features = ["serde"], optional = true }
26-
serde = { version = "1.0", default-features = false, features = ["derive"] }
27-
serde_json = { version = "1.0", default-features = false }
25+
serde = { version = "1.0", default-features = false, features = [
26+
"derive",
27+
"alloc",
28+
] }
29+
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
30+
hashbrown = { version = "0.15.4", features = ["serde"] }
2831

2932
[dev-dependencies]
3033
criterion = "0.6"
@@ -34,7 +37,8 @@ proptest = "1.7"
3437
[features]
3538
default = ["std"]
3639
std = ["serde/default", "serde_json/default"]
37-
alloc = ["serde/alloc", "serde_json/alloc", "hashbrown"]
40+
# Kept for backward compatibility - no-op since alloc is always available
41+
alloc = []
3842

3943
[[bench]]
4044
name = "benchmark"

safetensors/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ pub mod tensor;
88
pub use tensor::serialize_to_file;
99
pub use tensor::{serialize, Dtype, SafeTensorError, SafeTensors, View};
1010

11-
#[cfg(feature = "alloc")]
11+
#[cfg(not(feature = "std"))]
1212
#[macro_use]
1313
extern crate alloc;
1414

15-
#[cfg(all(feature = "std", feature = "alloc"))]
16-
compile_error!("must choose either the `std` or `alloc` feature, but not both.");
17-
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
18-
compile_error!("must choose either the `std` or `alloc` feature");
19-
2015
/// A facade around all the types we need from the `std`, `core`, and `alloc`
2116
/// crates. This avoids elaborate import wrangling having to happen in every
2217
/// module.

0 commit comments

Comments
 (0)