2020-09-22 18:38:12 +08:00
|
|
|
use std::borrow::Cow;
|
2023-11-28 17:11:17 +08:00
|
|
|
use std::ffi::CStr;
|
2020-09-22 18:38:12 +08:00
|
|
|
use std::str;
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
use heed::BoxedError;
|
|
|
|
|
2023-11-28 17:11:17 +08:00
|
|
|
use super::SliceTooShortError;
|
|
|
|
|
2022-09-14 20:01:53 +08:00
|
|
|
pub struct U8StrStrCodec;
|
2020-09-22 18:38:12 +08:00
|
|
|
|
2022-09-14 20:01:53 +08:00
|
|
|
impl<'a> heed::BytesDecode<'a> for U8StrStrCodec {
|
|
|
|
type DItem = (u8, &'a str, &'a str);
|
2020-09-22 18:38:12 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2023-11-28 17:11:17 +08:00
|
|
|
let (n, bytes) = bytes.split_first().ok_or(SliceTooShortError)?;
|
|
|
|
let cstr = CStr::from_bytes_until_nul(bytes)?;
|
|
|
|
let s1 = cstr.to_str()?;
|
|
|
|
// skip '\0' byte between the two strings.
|
|
|
|
let s2 = str::from_utf8(&bytes[s1.len() + 1..])?;
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok((*n, s1, s2))
|
2020-09-22 18:38:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:01:53 +08:00
|
|
|
impl<'a> heed::BytesEncode<'a> for U8StrStrCodec {
|
|
|
|
type EItem = (u8, &'a str, &'a str);
|
2020-09-22 18:38:12 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_encode((n, s1, s2): &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
|
2022-09-14 19:54:12 +08:00
|
|
|
let mut bytes = Vec::with_capacity(s1.len() + s2.len() + 1);
|
|
|
|
bytes.push(*n);
|
2020-09-22 18:38:12 +08:00
|
|
|
bytes.extend_from_slice(s1.as_bytes());
|
|
|
|
bytes.push(0);
|
|
|
|
bytes.extend_from_slice(s2.as_bytes());
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok(Cow::Owned(bytes))
|
2022-07-07 00:20:15 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-14 20:01:53 +08:00
|
|
|
pub struct UncheckedU8StrStrCodec;
|
2022-07-07 00:20:15 +08:00
|
|
|
|
2022-09-14 20:01:53 +08:00
|
|
|
impl<'a> heed::BytesDecode<'a> for UncheckedU8StrStrCodec {
|
|
|
|
type DItem = (u8, &'a [u8], &'a [u8]);
|
2022-07-07 00:20:15 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2023-11-28 17:11:17 +08:00
|
|
|
let (n, bytes) = bytes.split_first().ok_or(SliceTooShortError)?;
|
|
|
|
let cstr = CStr::from_bytes_until_nul(bytes)?;
|
|
|
|
let s1_bytes = cstr.to_bytes();
|
|
|
|
// skip '\0' byte between the two strings.
|
|
|
|
let s2_bytes = &bytes[s1_bytes.len() + 1..];
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok((*n, s1_bytes, s2_bytes))
|
2022-07-07 00:20:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:01:53 +08:00
|
|
|
impl<'a> heed::BytesEncode<'a> for UncheckedU8StrStrCodec {
|
|
|
|
type EItem = (u8, &'a [u8], &'a [u8]);
|
2022-07-07 00:20:15 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_encode((n, s1, s2): &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
|
2022-09-14 19:54:12 +08:00
|
|
|
let mut bytes = Vec::with_capacity(s1.len() + s2.len() + 1);
|
|
|
|
bytes.push(*n);
|
2022-07-07 00:20:15 +08:00
|
|
|
bytes.extend_from_slice(s1);
|
|
|
|
bytes.push(0);
|
|
|
|
bytes.extend_from_slice(s2);
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok(Cow::Owned(bytes))
|
2020-09-22 18:38:12 +08:00
|
|
|
}
|
|
|
|
}
|