|
144 | 144 | //! to create codecs to encode prefixes when possible instead of using a slice of bytes. |
145 | 145 | //! |
146 | 146 | //! ``` |
147 | | -//! use std::borrow::Cow; |
| 147 | +//! use std::convert::Infallible; |
148 | 148 | //! use std::error::Error; |
149 | 149 | //! use std::fs; |
150 | 150 | //! use std::path::Path; |
151 | 151 | //! |
152 | 152 | //! use heed::types::*; |
153 | 153 | //! use heed::{BoxedError, BytesDecode, BytesEncode, Database, EnvOpenOptions}; |
154 | 154 | //! |
155 | | -//! #[derive(Debug, PartialEq, Eq)] |
| 155 | +//! #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
156 | 156 | //! pub enum Level { |
157 | 157 | //! Debug, |
158 | 158 | //! Warn, |
|
170 | 170 | //! impl<'a> BytesEncode<'a> for LogKeyCodec { |
171 | 171 | //! type EItem = LogKey; |
172 | 172 | //! |
| 173 | +//! type ReturnBytes = [u8; 5]; |
| 174 | +//! |
| 175 | +//! type Error = Infallible; |
| 176 | +//! |
173 | 177 | //! /// Encodes the u32 timestamp in big endian followed by the log level with a single byte. |
174 | | -//! fn bytes_encode(log: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> { |
175 | | -//! let (timestamp_bytes, level_byte) = match log { |
176 | | -//! LogKey { timestamp, level: Level::Debug } => (timestamp.to_be_bytes(), 0), |
177 | | -//! LogKey { timestamp, level: Level::Warn } => (timestamp.to_be_bytes(), 1), |
178 | | -//! LogKey { timestamp, level: Level::Error } => (timestamp.to_be_bytes(), 2), |
179 | | -//! }; |
| 178 | +//! fn bytes_encode(log: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> { |
| 179 | +//! let mut output = [0; 5]; |
180 | 180 | //! |
181 | | -//! let mut output = Vec::new(); |
182 | | -//! output.extend_from_slice(×tamp_bytes); |
183 | | -//! output.push(level_byte); |
184 | | -//! Ok(Cow::Owned(output)) |
| 181 | +//! let [timestamp @ .., level] = &mut output; |
| 182 | +//! |
| 183 | +//! *timestamp = log.timestamp.to_be_bytes(); |
| 184 | +//! *level = log.level as u8; |
| 185 | +//! |
| 186 | +//! Ok(output) |
185 | 187 | //! } |
186 | 188 | //! } |
187 | 189 | //! |
|
216 | 218 | //! impl<'a> BytesEncode<'a> for LogAtHalfTimestampCodec { |
217 | 219 | //! type EItem = u32; |
218 | 220 | //! |
| 221 | +//! type ReturnBytes = [u8; 2]; |
| 222 | +//! |
| 223 | +//! type Error = Infallible; |
| 224 | +//! |
219 | 225 | //! /// This method encodes only the prefix of the keys in this particular case, the timestamp. |
220 | | -//! fn bytes_encode(half_timestamp: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> { |
221 | | -//! Ok(Cow::Owned(half_timestamp.to_be_bytes()[..2].to_vec())) |
| 226 | +//! fn bytes_encode(half_timestamp: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> { |
| 227 | +//! let [bytes @ .., _, _] = half_timestamp.to_be_bytes(); |
| 228 | +//! Ok(bytes) |
222 | 229 | //! } |
223 | 230 | //! } |
224 | 231 | //! |
|
0 commit comments