| 1 | #![doc( |
| 2 | html_favicon_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets//heed-pigeon.ico?raw=true" |
| 3 | )] |
| 4 | #![doc( |
| 5 | html_logo_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png?raw=true" |
| 6 | )] |
| 7 | |
| 8 | //! Contains the traits used to encode and decode database content. |
| 9 | |
| 10 | #![warn(missing_docs)] |
| 11 | |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#}; |
| 14 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 15 | |
| 16 | /// A boxed `Send + Sync + 'static` error. |
| 17 | pub type BoxedErrorrust-analyzer cargo heed-traits 0.20.0 BoxedError# = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + 'static>; |
| 18 | |
| 19 | /// A trait that represents an encoding structure. |
| 20 | pub trait BytesEncoderust-analyzer cargo heed-traits 0.20.0 BytesEncode#<'a> { |
| 21 | /// The type to encode. |
| 22 | type EItemrust-analyzer cargo heed-traits 0.20.0 BytesEncode#EItem#: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + 'a; |
| 23 | |
| 24 | /// Encode the given item as bytes. |
| 25 | fn bytes_encoderust-analyzer cargo heed-traits 0.20.0 BytesEncode#bytes_encode().(item: &'a Self::EItemrust-analyzer cargo heed-traits 0.20.0 BytesEncode#EItem#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, [u8]>, BoxedErrorrust-analyzer cargo heed-traits 0.20.0 BoxedError#>; |
| 26 | } |
| 27 | |
| 28 | /// A trait that represents a decoding structure. |
| 29 | pub trait BytesDecoderust-analyzer cargo heed-traits 0.20.0 BytesDecode#<'a> { |
| 30 | /// The type to decode. |
| 31 | type DItemrust-analyzer cargo heed-traits 0.20.0 BytesDecode#DItem#: 'a; |
| 32 | |
| 33 | /// Decode the given bytes as `DItem`. |
| 34 | fn bytes_decoderust-analyzer cargo heed-traits 0.20.0 BytesDecode#bytes_decode().(bytes: &'a [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::DItemrust-analyzer cargo heed-traits 0.20.0 BytesDecode#DItem#, BoxedErrorrust-analyzer cargo heed-traits 0.20.0 BoxedError#>; |
| 35 | } |
| 36 | |
| 37 | /// Define a custom key comparison function for a database. |
| 38 | /// |
| 39 | /// The comparison function is called whenever it is necessary to compare a key specified |
| 40 | /// by the application with a key currently stored in the database. If no comparison function |
| 41 | /// is specified, and no special key flags were specified, the keys are compared lexically, |
| 42 | /// with shorter keys collating before longer keys. |
| 43 | pub trait Comparatorrust-analyzer cargo heed-traits 0.20.0 Comparator# { |
| 44 | /// Compares the raw bytes representation of two keys. |
| 45 | /// |
| 46 | /// # Safety |
| 47 | /// |
| 48 | /// This function must never crash, this is the reason why it takes raw bytes as parameter, |
| 49 | /// to let you define the recovery method you want in case of a decoding error. |
| 50 | fn comparerust-analyzer cargo heed-traits 0.20.0 Comparator#compare().(a: &[u8], b: &[u8]) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#; |
| 51 | } |
| 52 | |
| 53 | /// Define a lexicographic comparator, which is a special case of [`Comparator`]. |
| 54 | /// |
| 55 | /// Types that implements [`LexicographicComparator`] will automatically have [`Comparator`] |
| 56 | /// implemented as well, where the [`Comparator::compare`] is implemented per the definition |
| 57 | /// of lexicographic ordering with [`LexicographicComparator::compare_elem`]. |
| 58 | /// |
| 59 | /// This trait is introduced to support prefix iterators, which implicit assumes that the |
| 60 | /// underlying key comparator is lexicographic. |
| 61 | pub trait LexicographicComparatorrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#: Comparatorrust-analyzer cargo heed-traits 0.20.0 Comparator# { |
| 62 | /// Compare a single byte; this function is used to implement [`Comparator::compare`] |
| 63 | /// by definition of lexicographic ordering. |
| 64 | /// |
| 65 | /// # Safety |
| 66 | /// |
| 67 | /// This function must never crash. |
| 68 | fn compare_elemrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#compare_elem().(a: u8, b: u8) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#; |
| 69 | |
| 70 | /// Advances the given `elem` to its immediate lexicographic successor, if possible. |
| 71 | /// Returns `None` if `elem` is already at its maximum value with respect to the |
| 72 | /// lexicographic order defined by this comparator. |
| 73 | fn successorrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#successor().(elem: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>; |
| 74 | |
| 75 | /// Moves the given `elem` to its immediate lexicographic predecessor, if possible. |
| 76 | /// Returns `None` if `elem` is already at its minimum value with respect to the |
| 77 | /// lexicographic order defined by this comparator. |
| 78 | fn predecessorrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#predecessor().(elem: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>; |
| 79 | |
| 80 | /// Returns the maximum byte value per the comparator's lexicographic order. |
| 81 | fn max_elemrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#max_elem().() -> u8; |
| 82 | |
| 83 | /// Returns the minimum byte value per the comparator's lexicographic order. |
| 84 | fn min_elemrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#min_elem().() -> u8; |
| 85 | } |
| 86 | |
| 87 | impl<C: LexicographicComparatorrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#> Comparatorrust-analyzer cargo heed-traits 0.20.0 Comparator# for C { |
| 88 | fn comparerust-analyzer cargo heed-traits 0.20.0 impl#[C][Comparator]compare().(a: &[u8], b: &[u8]) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering# { |
| 89 | for idx in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(a.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), b.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) { |
| 90 | if a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().b[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). { |
| 91 | return C::compare_elemrust-analyzer cargo heed-traits 0.20.0 LexicographicComparator#compare_elem().(a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., b[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 92 | } |
| 93 | } |
| 94 | Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][Ord]cmp().(&a.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), &b.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) |
| 95 | } |
| 96 | } |