Skip to main content

Node

Struct Node 

Source
pub struct Node {
    pub left: Option<Cid>,
    pub entries: Vec<Entry>,
}
Expand description

One node of the tree.

A node holds every key at its own layer within some range, and a pointer to a subtree between each pair of them. left is the subtree before the first entry. A node with no entries is legal and common — it is what the layering rule produces when a run of low keys hangs under a much taller one — and it carries left alone.

Fields§

§left: Option<Cid>

Keys below this node’s layer that sort before every entry.

§entries: Vec<Entry>

This node’s own keys, in order.

Implementations§

Source§

impl Node

Source

pub fn decode(block: &[u8]) -> Result<Self, NodeError>

Decodes one node out of its block.

use std::collections::BTreeMap;
use didbot_data::mst::{Node, Tree};

let tree = Tree::build(&BTreeMap::new());
let block = &tree.blocks()[tree.root()];
assert_eq!(Node::decode(block).unwrap(), Node::default());
Source

pub fn from_value(value: &Value) -> Result<Self, NodeError>

Reads a node out of an already-decoded value.

Source

pub fn parts(&self) -> Parts

The node with its keys rebuilt; see Parts.

Every key was checked to be UTF-8 when the node was decoded, and a node this crate built has UTF-8 keys because it was handed strings. A node assembled by hand out of arbitrary bytes is the one case where this is lossy, and it is the case Node::decode refuses.

Source

pub fn from_parts(parts: &Parts) -> Self

Builds a node from full keys, values and subtrees.

The inverse of Node::parts, and the only way to make a node without going through Tree::build: the shared prefixes are computed here, so a caller cannot hold a node whose encoding disagrees with its keys.

§Panics

If there is not exactly one more subtree than there are keys, or if the keys and values are of different lengths. Both are the caller building something that is not a node.

Source

pub fn block(&self) -> (Cid, Vec<u8>)

The block this node encodes to, and the CID naming it.

Source

pub fn layer(&self) -> Option<usize>

Which layer this node is on, where that can be told from the node alone.

A node with keys is at the layer its keys are at — they all share one, which Node::decode checks. A node with no keys and no left subtree is the empty root, which is layer zero by definition. A node with no keys and a left subtree is the empty intermediate the layering rule produces, and it has no height of its own: it is one below whatever pointed at it, and nothing inside it says what that was.

Source

pub fn is_empty(&self) -> bool

Whether this is the empty node: no keys, and nothing below it.

This is the node an account’s first commit signs over, and it is also what a subtree pointer of null means — which is why an empty node is never stored as a subtree. A tree carrying one would have two spellings of “nothing here”.

Source§

impl Node

Source

pub fn to_value(&self) -> Value

The node as a data-model value, which is what it is hashed as.

The field names are the protocol’s and are one letter each: l for the left subtree, e for the entries, and within an entry p for the shared prefix length, k for the rest of the key, v for the record and t for the subtree to its right. k is a byte string and not text — a node is keyed on bytes, and a prefix length may land inside a multi-byte character.

Ordering is not decided here. dag_cbor sorts map keys length-first and then bytewise, so a node built with the fields in any order encodes to the same bytes.

Trait Implementations§

Source§

impl Clone for Node

Source§

fn clone(&self) -> Node

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Node

Source§

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

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

impl Default for Node

Source§

fn default() -> Node

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Node

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Node

Source§

impl StructuralPartialEq for Node

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnsafeUnpin for Node

§

impl UnwindSafe for Node

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: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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>,

Source§

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.