Struct trie_db::NibbleSlice
source · pub struct NibbleSlice<'a> { /* private fields */ }
Expand description
Nibble-orientated view onto byte-slice, allowing nibble-precision offsets.
This is an immutable struct. No operations actually change it.
Example
use patricia_trie::nibbleslice::NibbleSlice;
fn main() {
let d1 = &[0x01u8, 0x23, 0x45];
let d2 = &[0x34u8, 0x50, 0x12];
let d3 = &[0x00u8, 0x12];
let n1 = NibbleSlice::new(d1); // 0,1,2,3,4,5
let n2 = NibbleSlice::new(d2); // 3,4,5,0,1,2
let n3 = NibbleSlice::new_offset(d3, 1); // 0,1,2
assert!(n1 > n3); // 0,1,2,... > 0,1,2
assert!(n1 < n2); // 0,... < 3,...
assert!(n2.mid(3) == n3); // 0,1,2 == 0,1,2
assert!(n1.starts_with(&n3));
assert_eq!(n1.common_prefix(&n3), 3);
assert_eq!(n2.mid(3).common_prefix(&n1), 3);
}
Implementations§
source§impl<'a> NibbleSlice<'a>
impl<'a> NibbleSlice<'a>
sourcepub fn new_offset(data: &'a [u8], offset: usize) -> Self
pub fn new_offset(data: &'a [u8], offset: usize) -> Self
Create a new nibble slice with the given byte-slice with a nibble offset.
sourcepub fn from_stored(i: &NodeKey) -> NibbleSlice<'_>
pub fn from_stored(i: &NodeKey) -> NibbleSlice<'_>
Get nibble slice from a NodeKey
.
sourcepub fn to_stored(&self) -> NodeKey
pub fn to_stored(&self) -> NodeKey
Helper function to create a owned NodeKey
from this NibbleSlice
.
sourcepub fn to_stored_range(&self, nb: usize) -> NodeKey
pub fn to_stored_range(&self, nb: usize) -> NodeKey
Helper function to create a owned NodeKey
from this NibbleSlice
,
and for a given number of nibble.
Warning this method can be slow (number of nibble does not align the
original padding).
sourcepub fn mid(&self, i: usize) -> NibbleSlice<'a>
pub fn mid(&self, i: usize) -> NibbleSlice<'a>
Return object which represents a view on to this slice (further) offset by i
nibbles.
sourcepub fn back(&self, i: usize) -> NibbleSlice<'a>
pub fn back(&self, i: usize) -> NibbleSlice<'a>
Move back to a previously valid fix offset position.
sourcepub fn starts_with(&self, them: &Self) -> bool
pub fn starts_with(&self, them: &Self) -> bool
Do we start with the same nibbles as the whole of them
?
sourcepub fn common_prefix(&self, them: &Self) -> usize
pub fn common_prefix(&self, them: &Self) -> usize
How many of the same nibbles at the beginning do we match with them
?
sourcepub fn right(&'a self) -> Partial<'_>
pub fn right(&'a self) -> Partial<'_>
Return Partial
representation of this slice:
first encoded byte and following slice.
sourcepub fn right_iter(&'a self) -> impl Iterator<Item = u8> + 'a
pub fn right_iter(&'a self) -> impl Iterator<Item = u8> + 'a
Return an iterator over Partial
bytes representation.
sourcepub fn right_range_iter(&'a self, to: usize) -> impl Iterator<Item = u8> + 'a
pub fn right_range_iter(&'a self, to: usize) -> impl Iterator<Item = u8> + 'a
Return Partial
bytes iterator over a range of byte..
Warning can be slow when unaligned (similar to to_stored_range
).
sourcepub fn left(&'a self) -> Prefix<'_>
pub fn left(&'a self) -> Prefix<'_>
Return left portion of NibbleSlice
, if the slice
originates from a full key it will be the Prefix of the node
.
sourcepub fn original_data_as_prefix(&self) -> Prefix<'_>
pub fn original_data_as_prefix(&self) -> Prefix<'_>
sourcepub fn left_owned(&'a self) -> (SmallVec<[u8; 40]>, Option<u8>)
pub fn left_owned(&'a self) -> (SmallVec<[u8; 40]>, Option<u8>)
Owned version of a Prefix
from a left
method call.
sourcepub fn starts_with_vec(&self, other: &NibbleVec) -> bool
pub fn starts_with_vec(&self, other: &NibbleVec) -> bool
Same as Self::starts_with
but using NibbleVec
.
Trait Implementations§
source§impl<'a> Clone for NibbleSlice<'a>
impl<'a> Clone for NibbleSlice<'a>
source§fn clone(&self) -> NibbleSlice<'a>
fn clone(&self) -> NibbleSlice<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for NibbleSlice<'a>
impl<'a> Debug for NibbleSlice<'a>
source§impl<'a> From<NibbleSlice<'a>> for NibbleVec
impl<'a> From<NibbleSlice<'a>> for NibbleVec
source§fn from(s: NibbleSlice<'a>) -> Self
fn from(s: NibbleSlice<'a>) -> Self
source§impl<'a> From<NibbleSlice<'a>> for NodeKey
impl<'a> From<NibbleSlice<'a>> for NodeKey
source§fn from(slice: NibbleSlice<'a>) -> NodeKey
fn from(slice: NibbleSlice<'a>) -> NodeKey
source§impl<'a> Ord for NibbleSlice<'a>
impl<'a> Ord for NibbleSlice<'a>
source§impl<'a> PartialEq<NibbleSlice<'a>> for NibbleSlice<'a>
impl<'a> PartialEq<NibbleSlice<'a>> for NibbleSlice<'a>
source§impl<'a> PartialEq<NibbleVec> for NibbleSlice<'a>
impl<'a> PartialEq<NibbleVec> for NibbleSlice<'a>
source§impl<'a> PartialOrd<NibbleSlice<'a>> for NibbleSlice<'a>
impl<'a> PartialOrd<NibbleSlice<'a>> for NibbleSlice<'a>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more