From 9d4fb5416be1f963eca87691390bb7f6b8ff50cb Mon Sep 17 00:00:00 2001 From: Preston Evans <32944016+preston-evans98@users.noreply.github.com> Date: Sat, 23 Mar 2024 07:35:16 -0700 Subject: [PATCH] jmt: expose `NodeKey::new` and restrict `NibblePath` (#111) * Add Nibblepath to API * fmt --------- Co-authored-by: Erwan Or --- src/lib.rs | 1 + src/node_type.rs | 4 ++-- src/types/nibble/nibble_path.rs | 13 ++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bc9c226..553bc91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,7 @@ pub mod storage { pub use node_type::{LeafNode, Node, NodeKey}; pub use reader::HasPreimage; pub use reader::TreeReader; + pub use types::nibble::nibble_path::NibblePath; pub use writer::{ NodeBatch, NodeStats, StaleNodeIndex, StaleNodeIndexBatch, TreeUpdateBatch, TreeWriter, }; diff --git a/src/node_type.rs b/src/node_type.rs index 33fa74c..e12bf1b 100644 --- a/src/node_type.rs +++ b/src/node_type.rs @@ -57,7 +57,7 @@ pub struct NodeKey { impl NodeKey { /// Creates a new `NodeKey`. - pub(crate) fn new(version: Version, nibble_path: NibblePath) -> Self { + pub fn new(version: Version, nibble_path: NibblePath) -> Self { Self { version, nibble_path, @@ -75,7 +75,7 @@ impl NodeKey { } /// Gets the nibble path. - pub(crate) fn nibble_path(&self) -> &NibblePath { + pub fn nibble_path(&self) -> &NibblePath { &self.nibble_path } diff --git a/src/types/nibble/nibble_path.rs b/src/types/nibble/nibble_path.rs index 5eaf180..85b74ca 100644 --- a/src/types/nibble/nibble_path.rs +++ b/src/types/nibble/nibble_path.rs @@ -98,14 +98,17 @@ prop_compose! { impl NibblePath { /// Creates a new `NibblePath` from a vector of bytes assuming each byte has 2 nibbles. - pub fn new(bytes: Vec) -> Self { + pub(crate) fn new(bytes: Vec) -> Self { checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2); let num_nibbles = bytes.len() * 2; NibblePath { num_nibbles, bytes } } /// Similar to `new()` but assumes that the bytes have one less nibble. - pub fn new_odd(bytes: Vec) -> Self { + // Unlike `new`, this function is not used under all feature combinations - so + // we #[allow(unused)] to silence the warnings + #[allow(unused)] + pub(crate) fn new_odd(bytes: Vec) -> Self { checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2); assert_eq!( bytes.last().expect("Should have odd number of nibbles.") & 0x0f, @@ -117,7 +120,7 @@ impl NibblePath { } /// Adds a nibble to the end of the nibble path. - pub fn push(&mut self, nibble: Nibble) { + pub(crate) fn push(&mut self, nibble: Nibble) { assert!(ROOT_NIBBLE_HEIGHT > self.num_nibbles); if self.num_nibbles % 2 == 0 { self.bytes.push(u8::from(nibble) << 4); @@ -128,7 +131,7 @@ impl NibblePath { } /// Pops a nibble from the end of the nibble path. - pub fn pop(&mut self) -> Option { + pub(crate) fn pop(&mut self) -> Option { let poped_nibble = if self.num_nibbles % 2 == 0 { self.bytes.last_mut().map(|last_byte| { let nibble = *last_byte & 0x0f; @@ -195,7 +198,7 @@ impl NibblePath { } /// Get the underlying bytes storing nibbles. - pub fn bytes(&self) -> &[u8] { + pub(crate) fn bytes(&self) -> &[u8] { &self.bytes } }