Skip to content

Commit fc1ba48

Browse files
committed
feature: utilize bincode serde feature for both no_std/std
1 parent e8a5a93 commit fc1ba48

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

bintensors/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ making it ideal for machine learning, scientific computing, and other high-perfo
2424
exclude = ["target/*", "Cargo.lock"]
2525

2626
[dependencies]
27-
bincode = { version = "2.0.1", default-features = false, features = ["derive"] }
27+
bincode = { version = "2.0.1", default-features = false, features = ["derive", "serde"] }
2828
hashbrown = { version = "0.15.2", features = ["serde"], optional = true}
2929
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
3030
digest = "0.10.7"
@@ -40,9 +40,9 @@ sha1 = "0.10.6"
4040

4141

4242
[features]
43-
default = ["std"]
43+
default = ["alloc"]
4444
std = ["bincode/std", "serde/std"]
45-
alloc = ["bincode/alloc", "bincode/serde", "serde/alloc", "hashbrown"]
45+
alloc = ["bincode/alloc", "serde/alloc", "hashbrown"]
4646

4747
[[bench]]
4848
name = "benchmark"

bintensors/src/tensor.rs

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::lib::{Cow, HashMap, String, ToString, Vec};
33
use crate::slice::{InvalidSlice, SliceIterator, TensorIndexer};
44
use bincode::{Decode, Encode};
55
use digest::Digest;
6-
#[cfg(not(feature = "std"))]
7-
use serde::{Deserialize, Serialize};
86
#[cfg(feature = "std")]
97
use std::io::Write;
108

@@ -544,33 +542,16 @@ impl<'data> BinTensors<'data> {
544542

545543
/// The stuct representing the header of bintensor files which allow
546544
/// indexing into the raw byte-buffer array and how to interpret it.
547-
#[cfg_attr(feature = "std", derive(Debug, Clone, Encode, Decode))]
548-
#[cfg_attr(not(feature = "std"), derive(Debug, Serialize, Deserialize))]
545+
#[derive(Debug, Encode, Decode)]
546+
#[cfg_attr(feature = "std", derive(Clone))]
549547
pub struct Metadata {
548+
#[bincode(with_serde)]
550549
metadata: Option<HashMap<String, String>>,
551550
tensors: Vec<TensorInfo>,
551+
#[bincode(with_serde)]
552552
index_map: HashMap<String, usize>,
553553
}
554554

555-
#[cfg(not(feature = "std"))]
556-
impl bincode::Encode for Metadata {
557-
fn encode<E: bincode::enc::Encoder>(
558-
&self,
559-
encoder: &mut E,
560-
) -> Result<(), bincode::error::EncodeError> {
561-
bincode::serde::encode_into_writer(self, encoder.writer(), bincode::config::standard())
562-
}
563-
}
564-
565-
#[cfg(not(feature = "std"))]
566-
impl<Context> bincode::Decode<Context> for Metadata {
567-
fn decode<D: bincode::de::Decoder<Context = Context>>(
568-
decoder: &mut D,
569-
) -> core::result::Result<Self, bincode::error::DecodeError> {
570-
bincode::serde::decode_from_reader(decoder.reader(), bincode::config::standard())
571-
}
572-
}
573-
574555
impl Metadata {
575556
fn new(
576557
metadata: Option<HashMap<String, String>>,
@@ -739,8 +720,8 @@ impl<'data> TensorView<'data> {
739720
/// A single tensor information.
740721
/// Endianness is assumed to be little endian
741722
/// Ordering is assumed to be 'C'.
742-
#[cfg_attr(feature = "std", derive(Debug, Clone, Encode, Decode))]
743-
#[cfg_attr(not(feature = "std"), derive(Debug, Serialize, Deserialize))]
723+
#[derive(Debug, Encode, Decode)]
724+
#[cfg_attr(feature = "std", derive(Clone))]
744725
pub struct TensorInfo {
745726
/// The type of each element of the tensor
746727
pub dtype: Dtype,
@@ -751,24 +732,8 @@ pub struct TensorInfo {
751732
}
752733

753734
/// The various available dtypes. They MUST be in increasing alignment order
754-
#[cfg_attr(
755-
feature = "std",
756-
derive(Debug, Encode, Decode, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)
757-
)]
758-
#[cfg_attr(
759-
not(feature = "std"),
760-
derive(
761-
Debug,
762-
Clone,
763-
Copy,
764-
PartialEq,
765-
Eq,
766-
Ord,
767-
PartialOrd,
768-
Serialize,
769-
Deserialize
770-
)
771-
)]
735+
736+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Encode, Decode)]
772737
#[non_exhaustive]
773738
pub enum Dtype {
774739
/// Boolan type

0 commit comments

Comments
 (0)