|
147 | 147 | //!
|
148 | 148 | //! ```
|
149 | 149 | //! use std::borrow::Cow;
|
| 150 | +//! use std::convert::Infallible; |
150 | 151 | //! use std::error::Error;
|
151 | 152 | //! use std::fs;
|
152 | 153 | //! use std::path::Path;
|
153 | 154 | //!
|
154 | 155 | //! use heed::types::*;
|
155 |
| -//! use heed::{BoxedError, BytesDecode, BytesEncode, Database, EnvOpenOptions}; |
| 156 | +//! use heed::{BoxedError, BytesDecode, ToBytes, Database, EnvOpenOptions}; |
156 | 157 | //!
|
157 |
| -//! #[derive(Debug, PartialEq, Eq)] |
| 158 | +//! #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
158 | 159 | //! pub enum Level {
|
159 | 160 | //! Debug,
|
160 | 161 | //! Warn,
|
|
169 | 170 | //!
|
170 | 171 | //! pub struct LogKeyCodec;
|
171 | 172 | //!
|
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; |
174 | 179 | //!
|
175 | 180 | //! /// 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]; |
182 | 183 | //!
|
183 |
| -//! let mut output = Vec::new(); |
184 |
| -//! output.extend_from_slice(×tamp_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) |
187 | 190 | //! }
|
188 | 191 | //! }
|
189 | 192 | //!
|
|
215 | 218 | //! /// the logs that appeared during a, rather long, period.
|
216 | 219 | //! pub struct LogAtHalfTimestampCodec;
|
217 | 220 | //!
|
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; |
220 | 227 | //!
|
221 | 228 | //! /// 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) |
224 | 232 | //! }
|
225 | 233 | //! }
|
226 | 234 | //!
|
|
449 | 457 | // To let cargo generate doc links
|
450 | 458 | #![allow(unused_imports)]
|
451 | 459 |
|
452 |
| -use crate::{BytesDecode, BytesEncode, Database, EnvOpenOptions}; |
| 460 | +use crate::{BytesDecode, Database, EnvOpenOptions, ToBytes}; |
0 commit comments