1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Database key abstraction; `trait Key`.

//---------------------------------------------------------------------------------------------------- Import
use std::cmp::Ordering;

use crate::storable::Storable;

//---------------------------------------------------------------------------------------------------- Table
/// Database [`Table`](crate::table::Table) key metadata.
///
/// Purely compile time information for database table keys.
//
// FIXME: this doesn't need to exist right now but
// may be used if we implement getting values using ranges.
// <https://github.com/Cuprate/cuprate/pull/117#discussion_r1589378104>
pub trait Key: Storable + Sized {
    /// The primary key type.
    type Primary: Storable;

    /// Compare 2 [`Key`]'s against each other.
    ///
    /// By default, this does a straight _byte_ comparison,
    /// not a comparison of the key's value.
    ///
    /// ```rust
    /// # use cuprate_database::*;
    /// assert_eq!(
    ///     <u64 as Key>::compare([0].as_slice(), [1].as_slice()),
    ///     std::cmp::Ordering::Less,
    /// );
    /// assert_eq!(
    ///     <u64 as Key>::compare([1].as_slice(), [1].as_slice()),
    ///     std::cmp::Ordering::Equal,
    /// );
    /// assert_eq!(
    ///     <u64 as Key>::compare([2].as_slice(), [1].as_slice()),
    ///     std::cmp::Ordering::Greater,
    /// );
    /// ```
    #[inline]
    fn compare(left: &[u8], right: &[u8]) -> Ordering {
        left.cmp(right)
    }
}

//---------------------------------------------------------------------------------------------------- Impl
impl<T> Key for T
where
    T: Storable + Sized,
{
    type Primary = Self;
}

//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
    // use super::*;
}