Skip to content

Commit 42725fc

Browse files
committed
hint to using writers for serde serialization types, remove requirement of returning written bytes amount
1 parent 1c0fae6 commit 42725fc

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Diff for: heed-traits/src/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ pub trait BytesEncode<'a> {
4747
/// Encode the given item as bytes.
4848
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error>;
4949

50-
/// Encode the given item as bytes and write it into the writer. Returns the amount of bytes
51-
/// that were written.
50+
/// Encode the given item as bytes and write it into the writer.
5251
///
5352
/// When implementing this, also take a look at [`zero_copy`][BytesEncode::zero_copy]'s
5453
/// documentation.
@@ -57,13 +56,12 @@ pub trait BytesEncode<'a> {
5756
fn bytes_encode_into_writer<W: io::Write>(
5857
item: &'a Self::EItem,
5958
writer: &mut W,
60-
) -> Result<usize, BoxedError> {
59+
) -> Result<(), BoxedError> {
6160
let bytes = Self::bytes_encode(item)?;
62-
let bytes = bytes.as_ref();
6361

64-
writer.write_all(bytes)?;
62+
writer.write_all(bytes.as_ref())?;
6563

66-
Ok(bytes.len())
64+
Ok(())
6765
}
6866
}
6967

Diff for: heed-types/src/serde_bincode.rs

+12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ where
1616

1717
type Error = bincode::Error;
1818

19+
fn zero_copy(_item: &Self::EItem) -> bool {
20+
false
21+
}
22+
1923
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
2024
bincode::serialize(item)
2125
}
26+
27+
fn bytes_encode_into_writer<W: std::io::Write>(
28+
item: &'a Self::EItem,
29+
writer: &mut W,
30+
) -> Result<(), BoxedError> {
31+
bincode::serialize_into(writer, item)?;
32+
Ok(())
33+
}
2234
}
2335

2436
impl<'a, T: 'a> BytesDecode<'a> for SerdeBincode<T>

Diff for: heed-types/src/serde_json.rs

+12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ where
1616

1717
type Error = serde_json::Error;
1818

19+
fn zero_copy(_item: &Self::EItem) -> bool {
20+
false
21+
}
22+
1923
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
2024
serde_json::to_vec(item)
2125
}
26+
27+
fn bytes_encode_into_writer<W: std::io::Write>(
28+
item: &'a Self::EItem,
29+
writer: &mut W,
30+
) -> Result<(), BoxedError> {
31+
serde_json::to_writer(writer, item)?;
32+
Ok(())
33+
}
2234
}
2335

2436
impl<'a, T: 'a> BytesDecode<'a> for SerdeJson<T>

Diff for: heed-types/src/serde_rmp.rs

+12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ where
1616

1717
type Error = rmp_serde::encode::Error;
1818

19+
fn zero_copy(_item: &Self::EItem) -> bool {
20+
false
21+
}
22+
1923
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
2024
rmp_serde::to_vec(item)
2125
}
26+
27+
fn bytes_encode_into_writer<W: std::io::Write>(
28+
item: &'a Self::EItem,
29+
writer: &mut W,
30+
) -> Result<(), BoxedError> {
31+
rmp_serde::encode::write(writer, item)?;
32+
Ok(())
33+
}
2234
}
2335

2436
impl<'a, T: 'a> BytesDecode<'a> for SerdeRmp<T>

0 commit comments

Comments
 (0)