From 9a451e19bbe5fdad3d354e075b5b308414e62475 Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Wed, 25 Oct 2023 17:18:24 -0400 Subject: [PATCH] Fix clippy lints --- firewood/src/merkle.rs | 62 ++++++++++++++++++++++-------------------- shale/src/lib.rs | 3 +- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index 37de8f0ce..07feb68a7 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -3,7 +3,7 @@ use crate::{nibbles::Nibbles, v2::api::Proof}; use sha3::Digest; -use shale::{disk_address::DiskAddress, ObjRef, ObjWriteError, ShaleError, ShaleStore}; +use shale::{self, disk_address::DiskAddress, ObjWriteError, ShaleError, ShaleStore}; use std::{ cmp::Ordering, collections::HashMap, @@ -21,6 +21,10 @@ pub use node::{BranchNode, Data, ExtNode, LeafNode, Node, NodeType, NBRANCH}; pub use partial_path::PartialPath; pub use trie_hash::{TrieHash, TRIE_HASH_LEN}; +type ObjRef<'a> = shale::ObjRef<'a, Node>; +type ParentRefs<'a> = Vec<(ObjRef<'a>, u8)>; +type ParentAddresses = Vec<(DiskAddress, u8)>; + #[derive(Debug, Error)] pub enum MerkleError { #[error("merkle datastore error: {0:?}")] @@ -58,11 +62,11 @@ pub struct Merkle { } impl> Merkle { - pub fn get_node(&self, ptr: DiskAddress) -> Result, MerkleError> { + pub fn get_node(&self, ptr: DiskAddress) -> Result { self.store.get_item(ptr).map_err(Into::into) } - pub fn put_node(&self, node: Node) -> Result, MerkleError> { + pub fn put_node(&self, node: Node) -> Result { self.store.put_item(node, 0).map_err(Into::into) } @@ -164,8 +168,8 @@ impl + Send + Sync> Merkle { #[allow(clippy::too_many_arguments)] fn split<'b>( &self, - mut node_to_split: ObjRef<'b, Node>, - parents: &mut [(ObjRef<'b, Node>, u8)], + mut node_to_split: ObjRef<'b>, + parents: &mut [(ObjRef<'b>, u8)], insert_path: &[u8], n_path: Vec, n_value: Option, @@ -392,7 +396,7 @@ impl + Send + Sync> Merkle { key: K, mut val: Vec, root: DiskAddress, - ) -> Result<(impl Iterator>, Vec), MerkleError> { + ) -> Result<(impl Iterator, Vec), MerkleError> { // as we split a node, we need to track deleted nodes and parents let mut deleted = Vec::new(); let mut parents = Vec::new(); @@ -578,7 +582,7 @@ impl + Send + Sync> Merkle { fn after_remove_leaf( &self, - parents: &mut Vec<(ObjRef<'_, Node>, u8)>, + parents: &mut ParentRefs, deleted: &mut Vec, ) -> Result<(), MerkleError> { let (b_chd, val) = { @@ -749,7 +753,7 @@ impl + Send + Sync> Merkle { fn after_remove_branch( &self, (c_ptr, idx): (DiskAddress, u8), - parents: &mut Vec<(ObjRef<'_, Node>, u8)>, + parents: &mut ParentRefs, deleted: &mut Vec, ) -> Result<(), MerkleError> { // [b] -> [u] -> [c] @@ -950,17 +954,17 @@ impl + Send + Sync> Merkle { fn get_node_by_key<'a, K: AsRef<[u8]>>( &'a self, - node_ref: ObjRef<'a, Node>, + node_ref: ObjRef<'a>, key: K, - ) -> Result>, MerkleError> { + ) -> Result>, MerkleError> { self.get_node_by_key_with_callback(node_ref, key, |_, _| {}) } fn get_node_and_parents_by_key<'a, K: AsRef<[u8]>>( &'a self, - node_ref: ObjRef<'a, Node>, + node_ref: ObjRef<'a>, key: K, - ) -> Result<(Option>, Vec<(ObjRef<'a, Node>, u8)>), MerkleError> { + ) -> Result<(Option>, ParentRefs<'a>), MerkleError> { let mut parents = Vec::new(); let node_ref = self.get_node_by_key_with_callback(node_ref, key, |node_ref, nib| { parents.push((node_ref, nib)); @@ -971,9 +975,9 @@ impl + Send + Sync> Merkle { fn get_node_and_parent_addresses_by_key<'a, K: AsRef<[u8]>>( &'a self, - node_ref: ObjRef<'a, Node>, + node_ref: ObjRef<'a>, key: K, - ) -> Result<(Option>, Vec<(DiskAddress, u8)>), MerkleError> { + ) -> Result<(Option>, ParentAddresses), MerkleError> { let mut parents = Vec::new(); let node_ref = self.get_node_by_key_with_callback(node_ref, key, |node_ref, nib| { parents.push((node_ref.into_ptr(), nib)); @@ -984,10 +988,10 @@ impl + Send + Sync> Merkle { fn get_node_by_key_with_callback<'a, K: AsRef<[u8]>>( &'a self, - mut node_ref: ObjRef<'a, Node>, + mut node_ref: ObjRef<'a>, key: K, - mut loop_callback: impl FnMut(ObjRef<'a, Node>, u8), - ) -> Result>, MerkleError> { + mut loop_callback: impl FnMut(ObjRef<'a>, u8), + ) -> Result>, MerkleError> { let mut key_nibbles = Nibbles::<1>::new(key.as_ref()).into_iter(); loop { @@ -1173,7 +1177,7 @@ impl + Send + Sync> Merkle { } } -fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef<'_, Node>, u8)]) { +fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef, u8)]) { let (p_ref, idx) = parents.last_mut().unwrap(); p_ref .write(|p| { @@ -1187,11 +1191,11 @@ fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef<'_, Node>, u8)]) { .unwrap(); } -pub struct Ref<'a>(ObjRef<'a, Node>); +pub struct Ref<'a>(ObjRef<'a>); pub struct RefMut<'a, S> { ptr: DiskAddress, - parents: Vec<(DiskAddress, u8)>, + parents: ParentAddresses, merkle: &'a mut Merkle, } @@ -1207,7 +1211,7 @@ impl<'a> std::ops::Deref for Ref<'a> { } impl<'a, S: ShaleStore + Send + Sync> RefMut<'a, S> { - fn new(ptr: DiskAddress, parents: Vec<(DiskAddress, u8)>, merkle: &'a mut Merkle) -> Self { + fn new(ptr: DiskAddress, parents: ParentAddresses, merkle: &'a mut Merkle) -> Self { Self { ptr, parents, @@ -1413,12 +1417,12 @@ mod tests { // insert values for key_val in u8::MIN..=u8::MAX { - let key = vec![key_val]; - let val = vec![key_val]; + let key = &[key_val]; + let val = &[key_val]; - merkle.insert(&key, val.clone(), root).unwrap(); + merkle.insert(key, val.to_vec(), root).unwrap(); - let fetched_val = merkle.get(&key, root).unwrap(); + let fetched_val = merkle.get(key, root).unwrap(); // make sure the value was inserted assert_eq!(fetched_val.as_deref(), val.as_slice().into()); @@ -1426,13 +1430,13 @@ mod tests { // remove values for key_val in u8::MIN..=u8::MAX { - let key = vec![key_val]; - let val = vec![key_val]; + let key = &[key_val]; + let val = &[key_val]; - let removed_val = merkle.remove(&key, root).unwrap(); + let removed_val = merkle.remove(key, root).unwrap(); assert_eq!(removed_val.as_deref(), val.as_slice().into()); - let fetched_val = merkle.get(&key, root).unwrap(); + let fetched_val = merkle.get(key, root).unwrap(); assert!(fetched_val.is_none()); } } diff --git a/shale/src/lib.rs b/shale/src/lib.rs index c0a25cd09..beeb75147 100644 --- a/shale/src/lib.rs +++ b/shale/src/lib.rs @@ -208,8 +208,7 @@ impl<'a, T: Send + Sync> ObjRef<'a, T> { } pub fn into_ptr(self) -> DiskAddress { - let ptr = self.deref().as_ptr(); - ptr + self.deref().as_ptr() } }