Function encode_path_leaf
pub fn encode_path_leaf(nibbles: &Nibbles, is_leaf: bool) -> SmallVec<[u8; 36]>
Expand description
Re-export Encodes a given path leaf as a compact array of bytes.
In resulted array, each byte represents two “nibbles” (half-bytes or 4 bits) of the original hex data, along with additional information about the leaf itself.
The method takes the following input:
is_leaf
: A boolean value indicating whether the current node is a leaf node or not.
The first byte of the encoded vector is set based on the is_leaf
flag and the parity of
the hex data length (even or odd number of nibbles).
- If the node is an extension with even length, the header byte is
0x00
. - If the node is an extension with odd length, the header byte is
0x10 + <first nibble>
. - If the node is a leaf with even length, the header byte is
0x20
. - If the node is a leaf with odd length, the header byte is
0x30 + <first nibble>
.
If there is an odd number of nibbles, store the first nibble in the lower 4 bits of the first byte of encoded.
§Returns
A vector containing the compact byte representation of the nibble sequence, including the header byte.
This vector’s length is self.len() / 2 + 1
. For stack-allocated nibbles, this is at most
33 bytes, so 36 was chosen as the stack capacity to round up to the next usize-aligned
size.
§Examples
// Extension node with an even path length:
let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
assert_eq!(nibbles.encode_path_leaf(false)[..], [0x00, 0xAB, 0xCD]);
// Extension node with an odd path length:
let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]);
assert_eq!(nibbles.encode_path_leaf(false)[..], [0x1A, 0xBC]);
// Leaf node with an even path length:
let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
assert_eq!(nibbles.encode_path_leaf(true)[..], [0x20, 0xAB, 0xCD]);
// Leaf node with an odd path length:
let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]);
assert_eq!(nibbles.encode_path_leaf(true)[..], [0x3A, 0xBC]);