fuel_merkle/sparse/primitive.rs
1use crate::common::{
2 Bytes32,
3 Prefix,
4 PrefixError,
5};
6
7/// **Leaf buffer:**
8///
9/// | Allocation | Data |
10/// |------------|----------------------------|
11/// | `00 - 04` | Height (4 bytes) |
12/// | `04 - 05` | Prefix (1 byte, `0x00`) |
13/// | `05 - 37` | hash(Key) (32 bytes) |
14/// | `37 - 69` | hash(Data) (32 bytes) |
15///
16/// **Node buffer:**
17///
18/// | Allocation | Data |
19/// |------------|----------------------------|
20/// | `00 - 04` | Height (4 bytes) |
21/// | `04 - 05` | Prefix (1 byte, `0x01`) |
22/// | `05 - 37` | Left child key (32 bytes) |
23/// | `37 - 69` | Right child key (32 bytes) |
24pub type Primitive = (u32, u8, Bytes32, Bytes32);
25
26pub trait PrimitiveView {
27 fn height(&self) -> u32;
28 fn prefix(&self) -> Result<Prefix, PrefixError>;
29 fn bytes_lo(&self) -> &Bytes32;
30 fn bytes_hi(&self) -> &Bytes32;
31}
32
33impl PrimitiveView for Primitive {
34 fn height(&self) -> u32 {
35 self.0
36 }
37
38 fn prefix(&self) -> Result<Prefix, PrefixError> {
39 Prefix::try_from(self.1)
40 }
41
42 fn bytes_lo(&self) -> &Bytes32 {
43 &self.2
44 }
45
46 fn bytes_hi(&self) -> &Bytes32 {
47 &self.3
48 }
49}