Skip to content

Commit 8780bf2

Browse files
committed
fix cookbook
1 parent 766d4c4 commit 8780bf2

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

heed/src/cookbook.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@
147147
//!
148148
//! ```
149149
//! use std::borrow::Cow;
150+
//! use std::convert::Infallible;
150151
//! use std::error::Error;
151152
//! use std::fs;
152153
//! use std::path::Path;
153154
//!
154155
//! use heed::types::*;
155-
//! use heed::{BoxedError, BytesDecode, BytesEncode, Database, EnvOpenOptions};
156+
//! use heed::{BoxedError, BytesDecode, ToBytes, Database, EnvOpenOptions};
156157
//!
157-
//! #[derive(Debug, PartialEq, Eq)]
158+
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
158159
//! pub enum Level {
159160
//! Debug,
160161
//! Warn,
@@ -169,21 +170,23 @@
169170
//!
170171
//! pub struct LogKeyCodec;
171172
//!
172-
//! impl<'a> BytesEncode<'a> for LogKeyCodec {
173-
//! type EItem = LogKey;
173+
//! impl<'a> ToBytes<'a> for LogKeyCodec {
174+
//! type SelfType = LogKey;
175+
//!
176+
//! type ReturnBytes = [u8; 5];
177+
//!
178+
//! type Error = Infallible;
174179
//!
175180
//! /// Encodes the u32 timestamp in big endian followed by the log level with a single byte.
176-
//! fn bytes_encode(log: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
177-
//! let (timestamp_bytes, level_byte) = match log {
178-
//! LogKey { timestamp, level: Level::Debug } => (timestamp.to_be_bytes(), 0),
179-
//! LogKey { timestamp, level: Level::Warn } => (timestamp.to_be_bytes(), 1),
180-
//! LogKey { timestamp, level: Level::Error } => (timestamp.to_be_bytes(), 2),
181-
//! };
181+
//! fn to_bytes(log: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
182+
//! let mut output = [0; 5];
182183
//!
183-
//! let mut output = Vec::new();
184-
//! output.extend_from_slice(&timestamp_bytes);
185-
//! output.push(level_byte);
186-
//! Ok(Cow::Owned(output))
184+
//! let [timestamp @ .., level] = &mut output;
185+
//!
186+
//! *timestamp = log.timestamp.to_be_bytes();
187+
//! *level = log.level as u8;
188+
//!
189+
//! Ok(output)
187190
//! }
188191
//! }
189192
//!
@@ -215,12 +218,17 @@
215218
//! /// the logs that appeared during a, rather long, period.
216219
//! pub struct LogAtHalfTimestampCodec;
217220
//!
218-
//! impl<'a> BytesEncode<'a> for LogAtHalfTimestampCodec {
219-
//! type EItem = u32;
221+
//! impl<'a> ToBytes<'a> for LogAtHalfTimestampCodec {
222+
//! type SelfType = u32;
223+
//!
224+
//! type ReturnBytes = [u8; 2];
225+
//!
226+
//! type Error = Infallible;
220227
//!
221228
//! /// This method encodes only the prefix of the keys in this particular case, the timestamp.
222-
//! fn bytes_encode(half_timestamp: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
223-
//! Ok(Cow::Owned(half_timestamp.to_be_bytes()[..2].to_vec()))
229+
//! fn to_bytes(half_timestamp: &'a Self::SelfType) -> Result<Self::ReturnBytes, Self::Error> {
230+
//! let [bytes @ .., _, _] = half_timestamp.to_be_bytes();
231+
//! Ok(bytes)
224232
//! }
225233
//! }
226234
//!
@@ -449,4 +457,4 @@
449457
// To let cargo generate doc links
450458
#![allow(unused_imports)]
451459

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

0 commit comments

Comments
 (0)