Skip to content

Commit

Permalink
rename the new trait and associated function back
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Aug 18, 2024
1 parent 7b5d7d5 commit b1a1918
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 249 deletions.
14 changes: 7 additions & 7 deletions heed-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use std::error::Error as StdError;
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;

/// A trait that represents an encoding structure.
pub trait ToBytes<'a> {
/// The type to encode to bytes.
type SelfType: ?Sized + 'a;
pub trait BytesEncode<'a> {
/// The type to encode.
type EItem: ?Sized + 'a;

/// The type containing the encoded bytes.
type ReturnBytes: Into<Vec<u8>> + AsRef<[u8]> + 'a;
Expand All @@ -27,15 +27,15 @@ pub trait ToBytes<'a> {
type Error: StdError + Send + Sync + 'static;

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

/// Encode the given item as bytes and write it into the writer. This function by default
/// forwards to `to_bytes`.
/// forwards to `bytes_encode`.
fn bytes_encode_into_writer(
item: &'a Self::SelfType,
item: &'a Self::EItem,
writer: &mut Vec<u8>,
) -> Result<(), Self::Error> {
writer.extend_from_slice(Self::to_bytes(item)?.as_ref());
writer.extend_from_slice(Self::bytes_encode(item)?.as_ref());
Ok(())
}
}
Expand Down
14 changes: 7 additions & 7 deletions heed-types/src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::convert::Infallible;

use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};

/// Describes a byte slice `[u8]` that is totally borrowed and doesn't depend on
/// any [memory alignment].
///
/// [memory alignment]: std::mem::align_of()
pub enum Bytes {}

impl<'a> ToBytes<'a> for Bytes {
type SelfType = [u8];
impl<'a> BytesEncode<'a> for Bytes {
type EItem = [u8];

type ReturnBytes = &'a [u8];

type Error = Infallible;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok(item)
}
}
Expand All @@ -31,14 +31,14 @@ impl<'a> BytesDecode<'a> for Bytes {
/// Like [`Bytes`], but always contains exactly `N` (the generic parameter) bytes.
pub enum FixedSizeBytes<const N: usize> {}

impl<'a, const N: usize> ToBytes<'a> for FixedSizeBytes<N> {
type SelfType = [u8; N];
impl<'a, const N: usize> BytesEncode<'a> for FixedSizeBytes<N> {
type EItem = [u8; N];

type ReturnBytes = [u8; N]; // TODO &'a [u8; N] or [u8; N]

type Error = Infallible;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok(*item)
}
}
Expand Down
20 changes: 10 additions & 10 deletions heed-types/src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use std::marker::PhantomData;
use std::mem::size_of;

use byteorder::{ByteOrder, ReadBytesExt};
use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};

/// Encodable version of [`u8`].
pub struct U8;

impl ToBytes<'_> for U8 {
type SelfType = u8;
impl BytesEncode<'_> for U8 {
type EItem = u8;

type ReturnBytes = [u8; 1];

type Error = Infallible;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok([*item])
}
}
Expand All @@ -31,14 +31,14 @@ impl BytesDecode<'_> for U8 {
/// Encodable version of [`i8`].
pub struct I8;

impl ToBytes<'_> for I8 {
type SelfType = i8;
impl BytesEncode<'_> for I8 {
type EItem = i8;

type ReturnBytes = [u8; 1];

type Error = Infallible;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok([*item as u8])
}
}
Expand All @@ -59,14 +59,14 @@ macro_rules! define_type {

pub struct $name<O>(PhantomData<O>);

impl<O: ByteOrder> ToBytes<'_> for $name<O> {
type SelfType = $native;
impl<O: ByteOrder> BytesEncode<'_> for $name<O> {
type EItem = $native;

type ReturnBytes = [u8; size_of::<$native>()];

type Error = Infallible;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
let mut buf = [0; size_of::<$native>()];
O::$write_method(&mut buf, *item);
Ok(buf)
Expand Down
8 changes: 4 additions & 4 deletions heed-types/src/serde_bincode.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
use serde::{Deserialize, Serialize};

/// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so.
///
/// It can borrow bytes from the original slice.
pub struct SerdeBincode<T>(std::marker::PhantomData<T>);

impl<'a, T: 'a> ToBytes<'a> for SerdeBincode<T>
impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode<T>
where
T: Serialize,
{
type SelfType = T;
type EItem = T;

type ReturnBytes = Vec<u8>;

type Error = bincode::Error;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
bincode::serialize(item)
}
}
Expand Down
8 changes: 4 additions & 4 deletions heed-types/src/serde_json.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
use serde::{Deserialize, Serialize};

/// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `serde_json` to do so.
///
/// It can borrow bytes from the original slice.
pub struct SerdeJson<T>(std::marker::PhantomData<T>);

impl<'a, T: 'a> ToBytes<'a> for SerdeJson<T>
impl<'a, T: 'a> BytesEncode<'a> for SerdeJson<T>
where
T: Serialize,
{
type SelfType = T;
type EItem = T;

type ReturnBytes = Vec<u8>;

type Error = serde_json::Error;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
serde_json::to_vec(item)
}
}
Expand Down
8 changes: 4 additions & 4 deletions heed-types/src/serde_rmp.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
use serde::{Deserialize, Serialize};

/// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `rmp_serde` to do so.
///
/// It can borrow bytes from the original slice.
pub struct SerdeRmp<T>(std::marker::PhantomData<T>);

impl<'a, T: 'a> ToBytes<'a> for SerdeRmp<T>
impl<'a, T: 'a> BytesEncode<'a> for SerdeRmp<T>
where
T: Serialize,
{
type SelfType = T;
type EItem = T;

type ReturnBytes = Vec<u8>;

type Error = rmp_serde::encode::Error;

fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
rmp_serde::to_vec(item)
}
}
Expand Down
8 changes: 4 additions & 4 deletions heed-types/src/str.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::convert::Infallible;

use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};

/// Describes a [`str`].
pub enum Str {}

impl<'a> ToBytes<'a> for Str {
type SelfType = str;
impl<'a> BytesEncode<'a> for Str {
type EItem = str;

type ReturnBytes = &'a [u8];

type Error = Infallible;

fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok(item.as_bytes())
}
}
Expand Down
8 changes: 4 additions & 4 deletions heed-types/src/unit.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::convert::Infallible;
use std::{error, fmt};

use heed_traits::{BoxedError, BytesDecode, ToBytes};
use heed_traits::{BoxedError, BytesDecode, BytesEncode};

/// Describes the unit `()` type.
pub enum Unit {}

impl ToBytes<'_> for Unit {
type SelfType = ();
impl BytesEncode<'_> for Unit {
type EItem = ();

type ReturnBytes = [u8; 0];

type Error = Infallible;

fn to_bytes(&(): &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
fn bytes_encode(&(): &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok([])
}
}
Expand Down
17 changes: 8 additions & 9 deletions heed/src/cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,13 @@
//! to create codecs to encode prefixes when possible instead of using a slice of bytes.
//!
//! ```
//! use std::borrow::Cow;
//! use std::convert::Infallible;
//! use std::error::Error;
//! use std::fs;
//! use std::path::Path;
//!
//! use heed::types::*;
//! use heed::{BoxedError, BytesDecode, ToBytes, Database, EnvOpenOptions};
//! use heed::{BoxedError, BytesDecode, BytesEncode, Database, EnvOpenOptions};
//!
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
//! pub enum Level {
Expand All @@ -170,15 +169,15 @@
//!
//! pub struct LogKeyCodec;
//!
//! impl<'a> ToBytes<'a> for LogKeyCodec {
//! type SelfType = LogKey;
//! impl<'a> BytesEncode<'a> for LogKeyCodec {
//! type EItem = LogKey;
//!
//! type ReturnBytes = [u8; 5];
//!
//! type Error = Infallible;
//!
//! /// Encodes the u32 timestamp in big endian followed by the log level with a single byte.
//! fn to_bytes(log: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
//! fn bytes_encode(log: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
//! let mut output = [0; 5];
//!
//! let [timestamp @ .., level] = &mut output;
Expand Down Expand Up @@ -218,15 +217,15 @@
//! /// the logs that appeared during a, rather long, period.
//! pub struct LogAtHalfTimestampCodec;
//!
//! impl<'a> ToBytes<'a> for LogAtHalfTimestampCodec {
//! type SelfType = u32;
//! impl<'a> BytesEncode<'a> for LogAtHalfTimestampCodec {
//! type EItem = u32;
//!
//! type ReturnBytes = [u8; 2];
//!
//! type Error = Infallible;
//!
//! /// This method encodes only the prefix of the keys in this particular case, the timestamp.
//! fn to_bytes(half_timestamp: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
//! fn bytes_encode(half_timestamp: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
//! let [bytes @ .., _, _] = half_timestamp.to_be_bytes();
//! Ok(bytes)
//! }
Expand Down Expand Up @@ -457,4 +456,4 @@
// To let cargo generate doc links
#![allow(unused_imports)]

use crate::{BytesDecode, Database, EnvOpenOptions, ToBytes};
use crate::{BytesDecode, BytesEncode, Database, EnvOpenOptions};
Loading

0 comments on commit b1a1918

Please sign in to comment.