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>

source

pub fn new(data: &'a [u8]) -> Self

Create a new nibble slice with the given byte-slice.

source

pub fn new_offset(data: &'a [u8], offset: usize) -> Self

Create a new nibble slice with the given byte-slice with a nibble offset.

source

pub fn iter(&'a self) -> NibbleSliceIterator<'a>

Get an iterator for the series of nibbles.

source

pub fn from_stored(i: &NodeKey) -> NibbleSlice<'_>

Get nibble slice from a NodeKey.

source

pub fn to_stored(&self) -> NodeKey

Helper function to create a owned NodeKey from this NibbleSlice.

source

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).

source

pub fn is_empty(&self) -> bool

Return true if the slice contains no nibbles.

source

pub fn len(&self) -> usize

Get the length (in nibbles, naturally) of this slice.

source

pub fn at(&self, i: usize) -> u8

Get the nibble at position i.

source

pub fn mid(&self, i: usize) -> NibbleSlice<'a>

Return object which represents a view on to this slice (further) offset by i nibbles.

source

pub fn advance(&mut self, i: usize)

Advance the view on the slice by i nibbles.

source

pub fn back(&self, i: usize) -> NibbleSlice<'a>

Move back to a previously valid fix offset position.

source

pub fn starts_with(&self, them: &Self) -> bool

Do we start with the same nibbles as the whole of them?

source

pub fn common_prefix(&self, them: &Self) -> usize

How many of the same nibbles at the beginning do we match with them?

source

pub fn right(&'a self) -> Partial<'_>

Return Partial representation of this slice: first encoded byte and following slice.

source

pub fn right_iter(&'a self) -> impl Iterator<Item = u8> + 'a

Return an iterator over Partial bytes representation.

source

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).

source

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.

source

pub fn original_data_as_prefix(&self) -> Prefix<'_>

Get Prefix representation of the inner data.

This means the entire inner data will be returned as Prefix, ignoring any offset.

source

pub fn left_owned(&'a self) -> (SmallVec<[u8; 40]>, Option<u8>)

Owned version of a Prefix from a left method call.

source

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>

source§

fn clone(&self) -> NibbleSlice<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for NibbleSlice<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<NibbleSlice<'a>> for NodeKey

source§

fn from(slice: NibbleSlice<'a>) -> NodeKey

Converts to this type from the input type.
source§

impl<'a> From<NibbleSlice<'a>> for NibbleVec

source§

fn from(s: NibbleSlice<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> Ord for NibbleSlice<'a>

source§

fn cmp(&self, them: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'a> PartialEq<NibbleVec> for NibbleSlice<'a>

source§

fn eq(&self, other: &NibbleVec) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq for NibbleSlice<'a>

source§

fn eq(&self, them: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialOrd for NibbleSlice<'a>

source§

fn partial_cmp(&self, them: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> Copy for NibbleSlice<'a>

source§

impl<'a> Eq for NibbleSlice<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for NibbleSlice<'a>

§

impl<'a> RefUnwindSafe for NibbleSlice<'a>

§

impl<'a> Send for NibbleSlice<'a>

§

impl<'a> Sync for NibbleSlice<'a>

§

impl<'a> Unpin for NibbleSlice<'a>

§

impl<'a> UnwindSafe for NibbleSlice<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> MaybeDebug for T
where T: Debug,