Skip to content

Commit

Permalink
remove deprecated BytesEncode and add ToBytes::bytes_encode_into_writer
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Aug 17, 2024
1 parent c1c116d commit ac28681
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 62 deletions.
62 changes: 8 additions & 54 deletions heed-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,12 @@
#![warn(missing_docs)]

use std::borrow::Cow;
use std::cmp::{Ord, Ordering};
use std::error::Error as StdError;
use std::fmt;

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

/// A trait that represents an encoding structure.
#[deprecated = "replaced by `ToBytes` to allow for more optimization"]
#[allow(deprecated)] // deprecated BoxedErrorWrapper is used in a bound
pub trait BytesEncode<'a>:
// TODO are these bound needed?
ToBytes<'a, SelfType = Self::EItem, ReturnBytes = Cow<'a, [u8]>, Error = BoxedErrorWrapper>
{
/// The type to encode.
type EItem: ?Sized + 'a;

/// Encode the given item as bytes.
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError>;
}

/// A trait that represents an encoding structure.
pub trait ToBytes<'a> {
/// The type to encode to bytes.
Expand All @@ -44,45 +28,15 @@ pub trait ToBytes<'a> {

/// Encode the given item as bytes.
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error>;
}

#[allow(deprecated)]
impl<'a, T: BytesEncode<'a>> ToBytes<'a> for T {
type SelfType = <Self as BytesEncode<'a>>::EItem;

type ReturnBytes = Cow<'a, [u8]>;

type Error = BoxedErrorWrapper;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
Self::bytes_encode(item).map_err(BoxedErrorWrapper)
}
}

/// Wraps the [`BoxedError`] type alias because for complicated reasons it does not implement
/// [`Error`][StdError]. This wrapper forwards [`Debug`][fmt::Debug], [`Display`][fmt::Display]
/// and [`Error`][StdError] through the wrapper and the [`Box`].
#[deprecated = "this wrapper was added for backwards compatibility of BytesEncode only"]
pub struct BoxedErrorWrapper(BoxedError);

#[allow(deprecated)]
impl fmt::Debug for BoxedErrorWrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<BoxedError as fmt::Debug>::fmt(&self.0, f)
}
}

#[allow(deprecated)]
impl fmt::Display for BoxedErrorWrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<BoxedError as fmt::Display>::fmt(&self.0, f)
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion heed-types/src/serde_bincode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where

type Error = bincode::Error;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
bincode::serialize(item)
}
}
Expand Down
2 changes: 1 addition & 1 deletion heed-types/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where

type Error = serde_json::Error;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
serde_json::to_vec(item)
}
}
Expand Down
2 changes: 1 addition & 1 deletion heed-types/src/serde_rmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where

type Error = rmp_serde::encode::Error;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
rmp_serde::to_vec(item)
}
}
Expand Down
2 changes: 1 addition & 1 deletion heed-types/src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl ToBytes<'_> for Unit {

type Error = Infallible;

fn to_bytes(_item: &'_ Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn to_bytes(&(): &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
Ok([])
}
}
Expand Down
5 changes: 1 addition & 4 deletions heed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ pub use self::mdb::error::Error as MdbError;
use self::mdb::ffi::{from_val, into_val};
pub use self::mdb::flags::{DatabaseFlags, EnvFlags, PutFlags};
pub use self::reserved_space::ReservedSpace;
#[allow(deprecated)] // re-exports BytesEncode
pub use self::traits::{
BoxedError, BytesDecode, BytesEncode, Comparator, LexicographicComparator, ToBytes,
};
pub use self::traits::{BoxedError, BytesDecode, Comparator, LexicographicComparator, ToBytes};
pub use self::txn::{RoTxn, RwTxn};

/// The underlying LMDB library version information.
Expand Down

0 comments on commit ac28681

Please sign in to comment.