Skip to content

Commit 15ab5e5

Browse files
committed
remove deprecated BytesEncode and add ToBytes::bytes_encode_into_writer
1 parent 8780bf2 commit 15ab5e5

File tree

6 files changed

+13
-62
lines changed

6 files changed

+13
-62
lines changed

heed-traits/src/lib.rs

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,12 @@
99
1010
#![warn(missing_docs)]
1111

12-
use std::borrow::Cow;
1312
use std::cmp::{Ord, Ordering};
1413
use std::error::Error as StdError;
15-
use std::fmt;
1614

1715
/// A boxed `Send + Sync + 'static` error.
1816
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;
1917

20-
/// A trait that represents an encoding structure.
21-
#[deprecated = "replaced by `ToBytes` to allow for more optimization"]
22-
#[allow(deprecated)] // deprecated BoxedErrorWrapper is used in a bound
23-
pub trait BytesEncode<'a>:
24-
// TODO are these bound needed?
25-
ToBytes<'a, SelfType = Self::EItem, ReturnBytes = Cow<'a, [u8]>, Error = BoxedErrorWrapper>
26-
{
27-
/// The type to encode.
28-
type EItem: ?Sized + 'a;
29-
30-
/// Encode the given item as bytes.
31-
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError>;
32-
}
33-
3418
/// A trait that represents an encoding structure.
3519
pub trait ToBytes<'a> {
3620
/// The type to encode to bytes.
@@ -44,45 +28,15 @@ pub trait ToBytes<'a> {
4428

4529
/// Encode the given item as bytes.
4630
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error>;
47-
}
48-
49-
#[allow(deprecated)]
50-
impl<'a, T: BytesEncode<'a>> ToBytes<'a> for T {
51-
type SelfType = <Self as BytesEncode<'a>>::EItem;
52-
53-
type ReturnBytes = Cow<'a, [u8]>;
54-
55-
type Error = BoxedErrorWrapper;
56-
57-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
58-
Self::bytes_encode(item).map_err(BoxedErrorWrapper)
59-
}
60-
}
61-
62-
/// Wraps the [`BoxedError`] type alias because for complicated reasons it does not implement
63-
/// [`Error`][StdError]. This wrapper forwards [`Debug`][fmt::Debug], [`Display`][fmt::Display]
64-
/// and [`Error`][StdError] through the wrapper and the [`Box`].
65-
#[deprecated = "this wrapper was added for backwards compatibility of BytesEncode only"]
66-
pub struct BoxedErrorWrapper(BoxedError);
67-
68-
#[allow(deprecated)]
69-
impl fmt::Debug for BoxedErrorWrapper {
70-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
<BoxedError as fmt::Debug>::fmt(&self.0, f)
72-
}
73-
}
74-
75-
#[allow(deprecated)]
76-
impl fmt::Display for BoxedErrorWrapper {
77-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78-
<BoxedError as fmt::Display>::fmt(&self.0, f)
79-
}
80-
}
8131

82-
#[allow(deprecated)]
83-
impl StdError for BoxedErrorWrapper {
84-
fn source(&self) -> Option<&(dyn StdError + 'static)> {
85-
self.0.source()
32+
/// Encode the given item as bytes and write it into the writer. This function by default
33+
/// forwards to `to_bytes`.
34+
fn bytes_encode_into_writer(
35+
item: &'a Self::SelfType,
36+
writer: &mut Vec<u8>,
37+
) -> Result<(), Self::Error> {
38+
writer.extend_from_slice(Self::to_bytes(item)?.as_ref());
39+
Ok(())
8640
}
8741
}
8842

heed-types/src/serde_bincode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616

1717
type Error = bincode::Error;
1818

19-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
19+
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
2020
bincode::serialize(item)
2121
}
2222
}

heed-types/src/serde_json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616

1717
type Error = serde_json::Error;
1818

19-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
19+
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
2020
serde_json::to_vec(item)
2121
}
2222
}

heed-types/src/serde_rmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616

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

19-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
19+
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
2020
rmp_serde::to_vec(item)
2121
}
2222
}

heed-types/src/unit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl ToBytes<'_> for Unit {
1313

1414
type Error = Infallible;
1515

16-
fn to_bytes(_item: &'_ Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
16+
fn to_bytes(&(): &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
1717
Ok([])
1818
}
1919
}

heed/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ pub use self::mdb::error::Error as MdbError;
9292
use self::mdb::ffi::{from_val, into_val};
9393
pub use self::mdb::flags::{DatabaseFlags, EnvFlags, PutFlags};
9494
pub use self::reserved_space::ReservedSpace;
95-
#[allow(deprecated)] // re-exports BytesEncode
96-
pub use self::traits::{
97-
BoxedError, BytesDecode, BytesEncode, Comparator, LexicographicComparator, ToBytes,
98-
};
95+
pub use self::traits::{BoxedError, BytesDecode, Comparator, LexicographicComparator, ToBytes};
9996
pub use self::txn::{RoTxn, RwTxn};
10097

10198
/// The underlying LMDB library version information.

0 commit comments

Comments
 (0)