redb_bincode/
sort.rs

1use std::borrow::Borrow;
2use std::fmt;
3
4pub trait SortOrder {
5    fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering;
6}
7
8#[derive(Debug)]
9pub struct Lexicographical;
10
11impl SortOrder for Lexicographical {
12    fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
13        data1.cmp(data2)
14    }
15}
16
17#[derive(Debug)]
18pub struct SortKey<T>(pub T);
19
20impl<T> Borrow<T> for SortKey<T> {
21    fn borrow(&self) -> &T {
22        &self.0
23    }
24}
25
26impl<T> redb::Value for SortKey<T>
27where
28    T: SortOrder + fmt::Debug,
29{
30    type SelfType<'a>
31        = &'a [u8]
32    where
33        Self: 'a;
34
35    type AsBytes<'a>
36        = &'a [u8]
37    where
38        Self: 'a;
39
40    fn fixed_width() -> Option<usize> {
41        None
42    }
43
44    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
45    where
46        Self: 'a,
47    {
48        data
49    }
50
51    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
52    where
53        Self: 'a,
54        Self: 'b,
55    {
56        value
57    }
58
59    fn type_name() -> redb::TypeName {
60        <&[u8] as redb::Value>::type_name()
61    }
62}
63
64impl<T> redb::Key for SortKey<T>
65where
66    T: SortOrder + fmt::Debug,
67{
68    fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
69        <T as SortOrder>::compare(data1, data2)
70    }
71}