1use std::fmt::{Debug, Display};
2
3use ::u24::U24;
4use num::{
5 cast::AsPrimitive,
6 traits::{ConstOne, ConstZero},
7};
8use u24::u24;
9use zerocopy::{BE, FromBytes, Immutable, IntoBytes, KnownLayout, U16, U32, Unaligned};
10
11use crate::{
12 codec::{Encodable, partition_ref::PartitionRef},
13 never::Never,
14 partition::Partition,
15 segment::SplitSegment,
16 traits::{Cut, Merge, Optimizable, PartitionRead, PartitionWrite, TruncateFrom},
17};
18
19#[doc(hidden)]
20pub trait Level: Sized {
21 const DEBUG_NAME: &'static str;
22
23 type LevelDown: Level;
24
25 type Down: PartitionRead<Self::LevelDown>
26 + PartitionWrite<Self::LevelDown>
27 + Optimizable
28 + Encodable
29 + Default
30 + Debug
31 + Clone
32 + PartialEq
33 + for<'a> PartialEq<PartitionRef<'a, Self::LevelDown>>
34 + for<'a> Merge<PartitionRef<'a, Self::LevelDown>>
35 + for<'a> Cut<PartitionRef<'a, Self::LevelDown>, Out = Self::Down>
36 + Eq
37 + Cut<Out = Self::Down>
38 + Merge;
39
40 type Value: num::PrimInt
41 + AsPrimitive<usize>
42 + SplitSegment<Rest = <Self::LevelDown as Level>::Value>
43 + TruncateFrom<usize>
44 + ConstZero
45 + ConstOne
46 + Debug
47 + Display
48 + Clone
49 + range_set_blaze::Integer;
50
51 type ValueUnaligned: IntoBytes
52 + FromBytes
53 + Unaligned
54 + Immutable
55 + KnownLayout
56 + Into<Self::Value>
57 + From<Self::Value>
58 + Ord
59 + Debug
60 + Display
61 + Copy;
62
63 const BITS: usize;
64 const MAX_LEN: usize = 1 << Self::BITS;
65 const TREE_MIN: usize = 32;
66 const PREFER_TREE: bool = Self::BITS > 8;
67}
68
69#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
72pub struct High;
73
74impl Level for High {
75 const DEBUG_NAME: &'static str = "High";
76
77 type LevelDown = Mid;
78 type Down = Partition<Self::LevelDown>;
79 type Value = u32;
80 type ValueUnaligned = U32<BE>;
81
82 const BITS: usize = 32;
83}
84
85#[doc(hidden)]
86#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
87pub struct Mid;
88
89impl Level for Mid {
90 const DEBUG_NAME: &'static str = "Mid";
91
92 type LevelDown = Low;
93 type Down = Partition<Self::LevelDown>;
94 type Value = u24;
95 type ValueUnaligned = U24<BE>;
96
97 const BITS: usize = 24;
98}
99
100#[doc(hidden)]
101#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
102pub struct Low;
103
104impl Level for Low {
105 const DEBUG_NAME: &'static str = "Low";
106
107 type LevelDown = Block;
108 type Down = Partition<Self::LevelDown>;
109 type Value = u16;
110 type ValueUnaligned = U16<BE>;
111
112 const BITS: usize = 16;
113}
114
115#[doc(hidden)]
116#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
117pub struct Block;
118
119impl Level for Block {
120 const DEBUG_NAME: &'static str = "Block";
121
122 type LevelDown = Never;
123 type Down = Never;
124 type Value = u8;
125 type ValueUnaligned = u8;
126
127 const BITS: usize = 8;
128}