2023-05-02 15:34:28 +08:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2023-04-26 23:09:59 +08:00
|
|
|
use fst::Set;
|
2023-11-23 01:21:19 +08:00
|
|
|
use heed::{BoxedError, BytesDecode, BytesEncode};
|
2023-05-02 15:34:28 +08:00
|
|
|
|
|
|
|
/// A codec for values of type `Set<&[u8]>`.
|
|
|
|
pub struct FstSetCodec;
|
|
|
|
|
|
|
|
impl<'a> BytesEncode<'a> for FstSetCodec {
|
|
|
|
type EItem = Set<Vec<u8>>;
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError> {
|
|
|
|
Ok(Cow::Borrowed(item.as_fst().as_bytes()))
|
2023-05-02 15:34:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BytesDecode<'a> for FstSetCodec {
|
|
|
|
type DItem = Set<&'a [u8]>;
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
|
|
|
Set::new(bytes).map_err(Into::into)
|
2023-05-02 15:34:28 +08:00
|
|
|
}
|
|
|
|
}
|