#![doc(
html_favicon_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets//heed-pigeon.ico?raw=true"
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png?raw=true"
)]
#![warn(missing_docs)]
use std::borrow::Cow;
use std::cmp::{Ord, Ordering};
use std::error::Error as StdError;
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;
pub trait BytesEncode<'a> {
type EItem: ?Sized + 'a;
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError>;
}
pub trait BytesDecode<'a> {
type DItem: 'a;
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>;
}
pub trait Comparator {
fn compare(a: &[u8], b: &[u8]) -> Ordering;
}
pub trait LexicographicComparator: Comparator {
fn compare_elem(a: u8, b: u8) -> Ordering;
fn successor(elem: u8) -> Option<u8>;
fn predecessor(elem: u8) -> Option<u8>;
fn max_elem() -> u8;
fn min_elem() -> u8;
}
impl<C: LexicographicComparator> Comparator for C {
fn compare(a: &[u8], b: &[u8]) -> Ordering {
for idx in 0..std::cmp::min(a.len(), b.len()) {
if a[idx] != b[idx] {
return C::compare_elem(a[idx], b[idx]);
}
}
Ord::cmp(&a.len(), &b.len())
}
}