Skip to content

Commit 9c5ea67

Browse files
authored
feature: impl no_std feature build called alloc (#8)
2 parents 1383bae + 441640e commit 9c5ea67

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

bintensors/Cargo.toml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ license = "MIT"
66
rust-version = "1.85"
77
authors = ["Luca Vivona"]
88
readme = "README.md"
9-
keywords = [
10-
"bintensors",
11-
"Tensors",
12-
"Pytorch",
13-
"Numpy",
14-
"bincode",
15-
]
9+
keywords = ["bintensors", "Tensors", "Pytorch", "Numpy", "bincode"]
1610
repository = "https://github.com/GnosisFoundation/bintensors"
1711
description = """
1812
bintensors is a high-performance binary tensor serialization
@@ -30,9 +24,12 @@ making it ideal for machine learning, scientific computing, and other high-perfo
3024
exclude = ["target/*", "Cargo.lock"]
3125

3226
[dependencies]
33-
bincode = { version = "2.0.1", default-features = false, features = ["derive"] }
27+
bincode = { version = "2.0.1", default-features = false, features = ["derive", "serde"] }
28+
hashbrown = { version = "0.15.2", features = ["serde"], optional = true}
29+
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
3430
digest = "0.10.7"
3531

32+
3633
[dev-dependencies]
3734
criterion = "0.5"
3835
memmap2 = "0.9"
@@ -44,11 +41,12 @@ sha1 = "0.10.6"
4441

4542
[features]
4643
default = ["std"]
47-
std = ["bincode/default"]
44+
std = ["bincode/std", "serde/std"]
45+
alloc = ["bincode/alloc", "serde/alloc", "hashbrown"]
4846

4947
[[bench]]
5048
name = "benchmark"
5149
harness = false
5250

5351
[package.metadata.docs.rs]
54-
rustdoc-args = ["--html-in-header", "./docs/katex.html"]
52+
rustdoc-args = ["--html-in-header", "./docs/katex.html"]

bintensors/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,28 @@ pub use tensor::serialize_to_file;
1212
pub use tensor::{serialize, serialize_with_checksum, BinTensorError, BinTensors, Dtype, View};
1313

1414
// TODO: uncomment when all of no_std is ready
15-
// #[cfg(feature = "alloc")]
16-
// #[macro_use]
17-
// extern crate alloc;
15+
#[cfg(feature = "alloc")]
16+
#[macro_use]
17+
extern crate alloc;
1818

19-
#[cfg(not(feature = "std"))]
20-
compile_error!("must have the `std` feature");
19+
#[cfg(all(feature = "std", feature = "alloc"))]
20+
compile_error!("must choose either the `std` or `alloc` feature, but not both.");
21+
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
22+
compile_error!("must choose either the `std` or `alloc` feature");
2123

2224
/// A facade around all the types we need from the `std`, `core`, and `alloc`
2325
/// crates. This avoids elaborate import wrangling having to happen in every
2426
/// module.
2527
mod lib {
2628
// TODO: uncomment when add non-std
27-
// #[cfg(not(feature = "std"))]
28-
// mod no_stds {
29-
// pub use alloc::borrow::Cow;
30-
// pub use alloc::string::{String, ToString};
31-
// pub use alloc::vec::Vec;
32-
// pub use hashbrown::HashMap;
33-
// }
29+
#[cfg(not(feature = "std"))]
30+
mod no_stds {
31+
pub use alloc::borrow::Cow;
32+
pub use alloc::string::{String, ToString};
33+
pub use alloc::vec::Vec;
34+
pub use hashbrown::HashMap;
35+
}
36+
3437
#[cfg(feature = "std")]
3538
mod stds {
3639
pub use std::borrow::Cow;
@@ -40,9 +43,8 @@ mod lib {
4043
}
4144

4245
/// choose std or no_std to export by feature flag
43-
// TODO: uncomment when add non-std
44-
// #[cfg(not(feature = "std"))]
45-
// pub use no_stds::*;
46+
#[cfg(not(feature = "std"))]
47+
pub use no_stds::*;
4648
#[cfg(feature = "std")]
4749
pub use stds::*;
4850
}

bintensors/src/tensor.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,13 @@ impl<'data> BinTensors<'data> {
542542

543543
/// The stuct representing the header of bintensor files which allow
544544
/// indexing into the raw byte-buffer array and how to interpret it.
545-
#[derive(Debug, Clone, Encode, Decode)]
545+
#[derive(Debug, Encode, Decode)]
546+
#[cfg_attr(feature = "std", derive(Clone))]
546547
pub struct Metadata {
548+
#[bincode(with_serde)]
547549
metadata: Option<HashMap<String, String>>,
548550
tensors: Vec<TensorInfo>,
551+
#[bincode(with_serde)]
549552
index_map: HashMap<String, usize>,
550553
}
551554

@@ -717,7 +720,8 @@ impl<'data> TensorView<'data> {
717720
/// A single tensor information.
718721
/// Endianness is assumed to be little endian
719722
/// Ordering is assumed to be 'C'.
720-
#[derive(Debug, Clone, Encode, Decode)]
723+
#[derive(Debug, Encode, Decode)]
724+
#[cfg_attr(feature = "std", derive(Clone))]
721725
pub struct TensorInfo {
722726
/// The type of each element of the tensor
723727
pub dtype: Dtype,
@@ -728,7 +732,8 @@ pub struct TensorInfo {
728732
}
729733

730734
/// The various available dtypes. They MUST be in increasing alignment order
731-
#[derive(Debug, Encode, Decode, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
735+
736+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Encode, Decode)]
732737
#[non_exhaustive]
733738
pub enum Dtype {
734739
/// Boolan type
@@ -1151,7 +1156,7 @@ mod tests {
11511156
match BinTensors::deserialize(&reloaded) {
11521157
Err(BinTensorError::InvalidOffset(_)) => {
11531158
// Yes we have the correct error
1154-
// std::fs::remove_file(filename).unwrap();
1159+
std::fs::remove_file(filename).unwrap();
11551160
}
11561161
Err(err) => panic!("Unexpected error {err:?}"),
11571162
Ok(_) => panic!("This should not be able to be deserialized"),

0 commit comments

Comments
 (0)