Skip to content

Commit 08ca164

Browse files
committed
rename the new trait and associated function back
1 parent 15ab5e5 commit 08ca164

File tree

14 files changed

+259
-249
lines changed

14 files changed

+259
-249
lines changed

heed-traits/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::error::Error as StdError;
1616
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;
1717

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

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

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

3232
/// Encode the given item as bytes and write it into the writer. This function by default
33-
/// forwards to `to_bytes`.
33+
/// forwards to `bytes_encode`.
3434
fn bytes_encode_into_writer(
35-
item: &'a Self::SelfType,
35+
item: &'a Self::EItem,
3636
writer: &mut Vec<u8>,
3737
) -> Result<(), Self::Error> {
38-
writer.extend_from_slice(Self::to_bytes(item)?.as_ref());
38+
writer.extend_from_slice(Self::bytes_encode(item)?.as_ref());
3939
Ok(())
4040
}
4141
}

heed-types/src/bytes.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
use std::convert::Infallible;
22

3-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
3+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
44

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

11-
impl<'a> ToBytes<'a> for Bytes {
12-
type SelfType = [u8];
11+
impl<'a> BytesEncode<'a> for Bytes {
12+
type EItem = [u8];
1313

1414
type ReturnBytes = &'a [u8];
1515

1616
type Error = Infallible;
1717

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

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

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

3939
type Error = Infallible;
4040

41-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
41+
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
4242
Ok(*item)
4343
}
4444
}

heed-types/src/integer.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ use std::marker::PhantomData;
33
use std::mem::size_of;
44

55
use byteorder::{ByteOrder, ReadBytesExt};
6-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
6+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
77

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

11-
impl ToBytes<'_> for U8 {
12-
type SelfType = u8;
11+
impl BytesEncode<'_> for U8 {
12+
type EItem = u8;
1313

1414
type ReturnBytes = [u8; 1];
1515

1616
type Error = Infallible;
1717

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

34-
impl ToBytes<'_> for I8 {
35-
type SelfType = i8;
34+
impl BytesEncode<'_> for I8 {
35+
type EItem = i8;
3636

3737
type ReturnBytes = [u8; 1];
3838

3939
type Error = Infallible;
4040

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

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

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

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

6767
type Error = Infallible;
6868

69-
fn to_bytes(item: &Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
69+
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
7070
let mut buf = [0; size_of::<$native>()];
7171
O::$write_method(&mut buf, *item);
7272
Ok(buf)

heed-types/src/serde_bincode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
1+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
22
use serde::{Deserialize, Serialize};
33

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

9-
impl<'a, T: 'a> ToBytes<'a> for SerdeBincode<T>
9+
impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode<T>
1010
where
1111
T: Serialize,
1212
{
13-
type SelfType = T;
13+
type EItem = T;
1414

1515
type ReturnBytes = Vec<u8>;
1616

1717
type Error = bincode::Error;
1818

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

heed-types/src/serde_json.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
1+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
22
use serde::{Deserialize, Serialize};
33

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

9-
impl<'a, T: 'a> ToBytes<'a> for SerdeJson<T>
9+
impl<'a, T: 'a> BytesEncode<'a> for SerdeJson<T>
1010
where
1111
T: Serialize,
1212
{
13-
type SelfType = T;
13+
type EItem = T;
1414

1515
type ReturnBytes = Vec<u8>;
1616

1717
type Error = serde_json::Error;
1818

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

heed-types/src/serde_rmp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
1+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
22
use serde::{Deserialize, Serialize};
33

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

9-
impl<'a, T: 'a> ToBytes<'a> for SerdeRmp<T>
9+
impl<'a, T: 'a> BytesEncode<'a> for SerdeRmp<T>
1010
where
1111
T: Serialize,
1212
{
13-
type SelfType = T;
13+
type EItem = T;
1414

1515
type ReturnBytes = Vec<u8>;
1616

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

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

heed-types/src/str.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use std::convert::Infallible;
22

3-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
3+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
44

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

8-
impl<'a> ToBytes<'a> for Str {
9-
type SelfType = str;
8+
impl<'a> BytesEncode<'a> for Str {
9+
type EItem = str;
1010

1111
type ReturnBytes = &'a [u8];
1212

1313
type Error = Infallible;
1414

15-
fn to_bytes(item: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
15+
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
1616
Ok(item.as_bytes())
1717
}
1818
}

heed-types/src/unit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use std::convert::Infallible;
22
use std::{error, fmt};
33

4-
use heed_traits::{BoxedError, BytesDecode, ToBytes};
4+
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
55

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

9-
impl ToBytes<'_> for Unit {
10-
type SelfType = ();
9+
impl BytesEncode<'_> for Unit {
10+
type EItem = ();
1111

1212
type ReturnBytes = [u8; 0];
1313

1414
type Error = Infallible;
1515

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

heed/src/cookbook.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@
146146
//! to create codecs to encode prefixes when possible instead of using a slice of bytes.
147147
//!
148148
//! ```
149-
//! use std::borrow::Cow;
150149
//! use std::convert::Infallible;
151150
//! use std::error::Error;
152151
//! use std::fs;
153152
//! use std::path::Path;
154153
//!
155154
//! use heed::types::*;
156-
//! use heed::{BoxedError, BytesDecode, ToBytes, Database, EnvOpenOptions};
155+
//! use heed::{BoxedError, BytesDecode, BytesEncode, Database, EnvOpenOptions};
157156
//!
158157
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
159158
//! pub enum Level {
@@ -170,15 +169,15 @@
170169
//!
171170
//! pub struct LogKeyCodec;
172171
//!
173-
//! impl<'a> ToBytes<'a> for LogKeyCodec {
174-
//! type SelfType = LogKey;
172+
//! impl<'a> BytesEncode<'a> for LogKeyCodec {
173+
//! type EItem = LogKey;
175174
//!
176175
//! type ReturnBytes = [u8; 5];
177176
//!
178177
//! type Error = Infallible;
179178
//!
180179
//! /// Encodes the u32 timestamp in big endian followed by the log level with a single byte.
181-
//! fn to_bytes(log: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
180+
//! fn bytes_encode(log: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
182181
//! let mut output = [0; 5];
183182
//!
184183
//! let [timestamp @ .., level] = &mut output;
@@ -218,15 +217,15 @@
218217
//! /// the logs that appeared during a, rather long, period.
219218
//! pub struct LogAtHalfTimestampCodec;
220219
//!
221-
//! impl<'a> ToBytes<'a> for LogAtHalfTimestampCodec {
222-
//! type SelfType = u32;
220+
//! impl<'a> BytesEncode<'a> for LogAtHalfTimestampCodec {
221+
//! type EItem = u32;
223222
//!
224223
//! type ReturnBytes = [u8; 2];
225224
//!
226225
//! type Error = Infallible;
227226
//!
228227
//! /// This method encodes only the prefix of the keys in this particular case, the timestamp.
229-
//! fn to_bytes(half_timestamp: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
228+
//! fn bytes_encode(half_timestamp: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
230229
//! let [bytes @ .., _, _] = half_timestamp.to_be_bytes();
231230
//! Ok(bytes)
232231
//! }
@@ -457,4 +456,4 @@
457456
// To let cargo generate doc links
458457
#![allow(unused_imports)]
459458

460-
use crate::{BytesDecode, Database, EnvOpenOptions, ToBytes};
459+
use crate::{BytesDecode, BytesEncode, Database, EnvOpenOptions};

0 commit comments

Comments
 (0)