artful/
lib.rs

1pub mod art;
2mod leaf;
3mod node;
4mod node16;
5mod node256;
6mod node4;
7mod node48;
8mod simd;
9pub use art::Art;
10
11/// A trait some constraints on the key of art.
12///
13/// Artful implements this trait for most of the built-in types. If you want to
14/// customize the type as an artful key, you will need to implement the trait.
15pub trait ArtKey: Default {
16    /// Returns a reference to a byte slice from a particular type.
17    fn get_bytes(&self) -> &[u8];
18
19    /// Returns a mutable reference to a byte slice from a particular type.
20    fn get_mut_bytes(&mut self) -> &mut [u8];
21}
22
23impl ArtKey for i32 {
24    fn get_bytes(&self) -> &[u8] {
25        let ptr = self as *const i32 as *const u8;
26        unsafe { std::slice::from_raw_parts(ptr, 4) }
27    }
28
29    fn get_mut_bytes(&mut self) -> &mut [u8] {
30        let ptr = self as *mut i32 as *mut u8;
31        unsafe { std::slice::from_raw_parts_mut(ptr, 4) }
32    }
33}
34
35impl ArtKey for i64 {
36    fn get_bytes(&self) -> &[u8] {
37        let ptr = self as *const i64 as *const u8;
38        unsafe { std::slice::from_raw_parts(ptr, 8) }
39    }
40
41    fn get_mut_bytes(&mut self) -> &mut [u8] {
42        let ptr = self as *mut i64 as *mut u8;
43        unsafe { std::slice::from_raw_parts_mut(ptr, 8) }
44    }
45}
46
47impl ArtKey for String {
48    fn get_bytes(&self) -> &[u8] {
49        self.as_bytes()
50    }
51
52    fn get_mut_bytes(&mut self) -> &mut [u8] {
53        unsafe { self.as_bytes_mut() }
54    }
55}
56
57#[derive(Debug, Clone, Copy)]
58pub(crate) struct Partial<const MAX_PARTIAL_LEN: usize> {
59    pub(crate) data: [u8; MAX_PARTIAL_LEN],
60    pub(crate) len: u32,
61}
62
63impl<const MAX_PARTIAL_LEN: usize> Default for Partial<MAX_PARTIAL_LEN> {
64    fn default() -> Partial<MAX_PARTIAL_LEN> {
65        Partial {
66            data: [0_u8; MAX_PARTIAL_LEN],
67            len: 0,
68        }
69    }
70}
71
72#[derive(Default, Debug, Clone, Copy)]
73pub(crate) struct Header<const MAX_PARTIAL_LEN: usize> {
74    pub(crate) partial: Partial<MAX_PARTIAL_LEN>,
75    pub(crate) non_null_children: u16,
76}