From a76b0e7684840e1e2bfa2b9e84b3f831a3cc15e5 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Sun, 19 Jan 2025 17:37:09 -0600 Subject: [PATCH] clippy + fmt --- atrium-repo/src/blockstore/car.rs | 10 +-- atrium-repo/src/blockstore/memory.rs | 6 ++ atrium-repo/src/mst.rs | 107 +++++++++++-------------- atrium-repo/src/repo.rs | 10 +-- examples/firehose/src/stream/frames.rs | 22 +---- 5 files changed, 69 insertions(+), 86 deletions(-) diff --git a/atrium-repo/src/blockstore/car.rs b/atrium-repo/src/blockstore/car.rs index 7e7788d6..2fc3aa8a 100644 --- a/atrium-repo/src/blockstore/car.rs +++ b/atrium-repo/src/blockstore/car.rs @@ -129,7 +129,7 @@ impl CarStore { let header_bytes = serde_ipld_dagcbor::to_vec(&header).unwrap(); let mut buf = unsigned_varint::encode::usize_buffer(); let buf = unsigned_varint::encode::usize(header_bytes.len(), &mut buf); - storage.write_all(&buf).await?; + storage.write_all(buf).await?; storage.write_all(&header_bytes).await?; Ok(Self { storage, header, index: HashMap::new() }) @@ -143,7 +143,7 @@ impl CarStore { let mut buf = unsigned_varint::encode::usize_buffer(); let buf = unsigned_varint::encode::usize(header_bytes.len(), &mut buf); self.storage.seek(SeekFrom::Start(0)).await?; - self.storage.write_all(&buf).await?; + self.storage.write_all(buf).await?; self.storage.write_all(&header_bytes).await?; Ok(()) @@ -184,7 +184,7 @@ impl AsyncBlockStoreWrite let cid = Cid::new_v1(codec, hash); // Only write the record if the CAR file does not already contain it. - if !self.index.contains_key(&cid) { + if let std::collections::hash_map::Entry::Vacant(e) = self.index.entry(cid) { let mut fc = vec![]; cid.write_bytes(&mut fc).expect("internal error writing CID"); fc.extend_from_slice(contents); @@ -192,12 +192,12 @@ impl AsyncBlockStoreWrite let mut buf = unsigned_varint::encode::u64_buffer(); let buf = unsigned_varint::encode::u64(fc.len() as u64, &mut buf); - self.storage.write_all(&buf).await?; + self.storage.write_all(buf).await?; let offs = self.storage.stream_position().await?; self.storage.write_all(&fc).await?; // Update the index with the new block. - self.index.insert(cid, (offs, contents.len())); + e.insert((offs, contents.len())); } Ok(cid) diff --git a/atrium-repo/src/blockstore/memory.rs b/atrium-repo/src/blockstore/memory.rs index 64ae42db..c20c48f6 100644 --- a/atrium-repo/src/blockstore/memory.rs +++ b/atrium-repo/src/blockstore/memory.rs @@ -10,6 +10,12 @@ pub struct MemoryBlockStore { blocks: HashMap>, } +impl Default for MemoryBlockStore { + fn default() -> Self { + Self::new() + } +} + impl MemoryBlockStore { pub fn new() -> Self { Self { blocks: HashMap::new() } diff --git a/atrium-repo/src/mst.rs b/atrium-repo/src/mst.rs index 22b66984..d73c1e53 100644 --- a/atrium-repo/src/mst.rs +++ b/atrium-repo/src/mst.rs @@ -126,9 +126,9 @@ mod algos { } /// Traverse through the tree, finding the node that contains a key. - pub fn traverse_find<'a>( - key: &'a str, - ) -> impl FnMut(Node, Cid) -> Result, Error> + 'a { + pub fn traverse_find( + key: &str, + ) -> impl FnMut(Node, Cid) -> Result, Error> + '_ { move |node, _cid| -> Result<_, Error> { if let Some(index) = node.find_ge(key) { if let Some(NodeEntry::Leaf(e)) = node.entries.get(index) { @@ -140,26 +140,26 @@ mod algos { // Check if the left neighbor is a tree, and if so, recurse into it. if let Some(index) = index.checked_sub(1) { if let Some(subtree) = node.entries.get(index).unwrap().tree() { - return Ok(TraverseAction::Continue((subtree.clone(), index))); + Ok(TraverseAction::Continue((*subtree, index))) } else { - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } else { // There is no left neighbor. The key is not present. - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } else { // We've recursed into an empty node, so the key is not present in the tree. - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } } /// Traverse through the tree, finding the node that contains a key. This will record /// the CIDs of all nodes traversed. - pub fn traverse_find_path<'a>( - key: &'a str, - ) -> impl FnMut(Node, Cid) -> Result, Error> + 'a { + pub fn traverse_find_path( + key: &str, + ) -> impl FnMut(Node, Cid) -> Result, Error> + '_ { move |node, cid| -> Result<_, Error> { if let Some(index) = node.find_ge(key) { if let Some(NodeEntry::Leaf(e)) = node.entries.get(index) { @@ -171,17 +171,17 @@ mod algos { // Check if the left neighbor is a tree, and if so, recurse into it. if let Some(index) = index.checked_sub(1) { if let Some(subtree) = node.entries.get(index).unwrap().tree() { - return Ok(TraverseAction::Continue((subtree.clone(), subtree.clone()))); + Ok(TraverseAction::Continue((*subtree, *subtree))) } else { - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } else { // There is no left neighbor. The key is not present. - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } else { // We've recursed into an empty node, so the key is not present in the tree. - return Err(Error::KeyNotFound); + Err(Error::KeyNotFound) } } } @@ -192,11 +192,11 @@ mod algos { move |node, cid| -> Result<_, Error> { if node.entries.len() == 1 { if let Some(NodeEntry::Tree(cid)) = node.entries.first() { - return Ok(TraverseAction::Continue((cid.clone(), 0))); + return Ok(TraverseAction::Continue((*cid, 0))); } } - return Ok(TraverseAction::Stop(cid)); + Ok(TraverseAction::Stop(cid)) } } @@ -218,8 +218,8 @@ mod algos { { node_path.push((ln.clone(), rn.clone())); - lc = l.clone(); - rc = r.clone(); + lc = *l; + rc = *r; } else { break (ln, rn); } @@ -270,19 +270,17 @@ mod algos { // Left neighbor is a leaf, so we can split the current node into two and we are done. let right = node.entries.split_off(partition + 1); - return Ok(TraverseAction::Stop(( + Ok(TraverseAction::Stop(( Some(node), - (!right.is_empty()).then(|| Node { entries: right }), - ))); - } - Some(NodeEntry::Tree(e)) => { - return Ok(TraverseAction::Continue((e.clone(), partition))); + (!right.is_empty()).then_some(Node { entries: right }), + ))) } + Some(NodeEntry::Tree(e)) => Ok(TraverseAction::Continue((*e, partition))), // This should not happen; node.find_ge() should return `None` in this case. None => panic!(), } } else { - return Ok(TraverseAction::Stop((None, Some(node)))); + Ok(TraverseAction::Stop((None, Some(node)))) } } else { todo!() @@ -343,7 +341,7 @@ mod algos { // Now traverse to the node containing the target layer. let mut node_path = vec![]; - let mut node_cid = root.clone(); + let mut node_cid = root; // There are three cases we need to handle: // 1) The target layer is above the tree (and our entire tree needs to be pushed down). @@ -388,13 +386,12 @@ mod algos { { layer -= 1; return Ok(algos::TraverseAction::Continue(( - subtree.clone(), - partition, + *subtree, partition, ))); } } - return Ok(algos::TraverseAction::Stop((node, partition))); + Ok(algos::TraverseAction::Stop((node, partition))) } }) .await?; @@ -449,7 +446,7 @@ mod algos { } Some(NodeEntry::Tree(e)) => { // Need to split the subtree into two based on the node's key. - let (left, right) = algos::split_subtree(&mut bs, e.clone(), key).await?; + let (left, right) = algos::split_subtree(&mut bs, *e, key).await?; // Insert the new node inbetween the two subtrees. let right_subvec = node.entries.split_off(partition + 1); @@ -527,7 +524,7 @@ mod algos { if let (Some(NodeEntry::Tree(lc)), Some(NodeEntry::Tree(rc))) = (node.entries.get(index), node.entries.get(index + 1)) { - let cid = algos::merge_subtrees(&mut bs, lc.clone(), rc.clone()).await?; + let cid = algos::merge_subtrees(&mut bs, *lc, *rc).await?; node.entries[index] = NodeEntry::Tree(cid); node.entries.remove(index + 1); } @@ -542,7 +539,7 @@ mod algos { // Now walk back up the node path chain and update parent entries to point to the new node's CID. for (mut parent, i) in node_path.into_iter().rev() { if let Some(cid) = cid.as_mut() { - parent.entries[i] = NodeEntry::Tree(cid.clone()); + parent.entries[i] = NodeEntry::Tree(*cid); *cid = parent.serialize_into(&mut bs).await?; } else { // The node ended up becoming empty, so it will be orphaned. @@ -644,7 +641,7 @@ pub struct Tree { // so implement clone if the storage also implements it. impl Clone for Tree { fn clone(&self) -> Self { - Self { storage: self.storage.clone(), root: self.root.clone() } + Self { storage: self.storage.clone(), root: self.root } } } @@ -706,7 +703,7 @@ impl Tree { /// /// This function will _not_ work with a partial MST, such as one received from /// a firehose record. - pub fn entries<'a>(&'a mut self) -> impl Stream> + 'a { + pub fn entries(&mut self) -> impl Stream> + '_ { // Start from the root of the tree. let mut stack = vec![Located::InSubtree(self.root)]; @@ -718,10 +715,10 @@ impl Tree { for entry in node.entries.iter().rev() { match entry { NodeEntry::Tree(entry) => { - stack.push(Located::InSubtree(entry.clone())); + stack.push(Located::InSubtree(*entry)); } NodeEntry::Leaf(entry) => { - stack.push(Located::Entry((entry.key.clone(), entry.value.clone()))); + stack.push(Located::Entry((entry.key.clone(), entry.value))); } } } @@ -736,7 +733,7 @@ impl Tree { /// /// This function will _not_ work with a partial MST, such as one received from /// a firehose record. - pub fn keys<'a>(&'a mut self) -> impl Stream> + 'a { + pub fn keys(&mut self) -> impl Stream> + '_ { self.entries().map(|e| e.map(|(k, _)| k)) } @@ -810,7 +807,7 @@ impl Node { let mut entries = vec![]; if let Some(left) = &node.left { - entries.push(NodeEntry::Tree(left.clone())); + entries.push(NodeEntry::Tree(*left)); } let mut prev_key = vec![]; @@ -822,7 +819,7 @@ impl Node { // Nested subtrees are located to the right of the entry. if let Some(tree) = &entry.tree { - entries.push(NodeEntry::Tree(tree.clone())); + entries.push(NodeEntry::Tree(*tree)); } } @@ -841,7 +838,7 @@ impl Node { // Special case: if the first entry is a tree, that gets inserted into the node directly. let ents = match self.entries.first() { Some(NodeEntry::Tree(cid)) => { - node.left = Some(cid.clone()); + node.left = Some(*cid); &self.entries[1..] } _ => &self.entries, @@ -864,12 +861,12 @@ impl Node { } }; - let prefix = prefix(&prev_key, &leaf.key.as_bytes()); + let prefix = prefix(&prev_key, leaf.key.as_bytes()); node.entries.push(schema::TreeEntry { prefix_len: prefix, key_suffix: Ipld::Bytes(leaf.key[prefix..].as_bytes().to_vec()), - value: leaf.value.clone(), + value: leaf.value, tree: tree.cloned(), }); @@ -899,11 +896,7 @@ impl Node { /// Computes the node's layer, or returns `None` if this node has no leaves. fn layer(&self) -> Option { - if let Some(e) = self.leaves().next() { - Some(leading_zeroes(&e.key.as_bytes())) - } else { - None - } + self.leaves().next().map(|e| leading_zeroes(e.key.as_bytes())) } /// Find the index of the first leaf node that has a key greater than or equal to the provided key. @@ -915,12 +908,10 @@ impl Node { if let Some((i, _e)) = e.find(|(_i, e)| e.key.as_str() >= key) { Some(i) + } else if !self.entries.is_empty() { + Some(self.entries.len()) } else { - if self.entries.len() != 0 { - Some(self.entries.len()) - } else { - None - } + None } } @@ -932,12 +923,12 @@ impl Node { if let Some(NodeEntry::Leaf(e)) = self.entries.get(i) { if e.key == key { - return Some(Located::Entry(e.value.clone())); + return Some(Located::Entry(e.value)); } } if let Some(NodeEntry::Tree(cid)) = self.entries.get(i - 1) { - Some(Located::InSubtree(cid.clone())) + Some(Located::InSubtree(*cid)) } else { None } @@ -960,7 +951,7 @@ impl Node { if let Some(index) = index.checked_sub(1) { if let Some(NodeEntry::Tree(cid)) = self.entries.get(index) { - list.push(Located::InSubtree(cid.clone())); + list.push(Located::InSubtree(*cid)); } } @@ -969,14 +960,14 @@ impl Node { for e in e.chunks(2) { if let NodeEntry::Leaf(t) = &e[0] { if t.key.starts_with(prefix) { - list.push(Located::Entry((&t.key[..], t.value.clone()))); + list.push(Located::Entry((&t.key[..], t.value))); if let Some(NodeEntry::Tree(cid)) = e.get(1) { - list.push(Located::InSubtree(cid.clone())); + list.push(Located::InSubtree(*cid)); } } else if prefix > t.key.as_str() { if let Some(NodeEntry::Tree(cid)) = e.get(1) { - list.push(Located::InSubtree(cid.clone())); + list.push(Located::InSubtree(*cid)); } } } diff --git a/atrium-repo/src/repo.rs b/atrium-repo/src/repo.rs index 24931e98..b462149b 100644 --- a/atrium-repo/src/repo.rs +++ b/atrium-repo/src/repo.rs @@ -87,7 +87,7 @@ impl Commit { }) .unwrap(); // This should (hopefully!) never fail - sha2::Sha256::digest(&commit).into() + sha2::Sha256::digest(commit).into() } /// Return the commit object's cryptographic signature. @@ -144,7 +144,7 @@ impl Repository { pub async fn get(&mut self, rkey: &str) -> Result, Error> { let mut mst = mst::Tree::open(&mut self.db, self.latest_commit.data); - if let Some(cid) = mst.get(&rkey).await? { + if let Some(cid) = mst.get(rkey).await? { Ok(Some(read_record::(&mut self.db, cid).await?)) } else { Ok(None) @@ -190,11 +190,11 @@ mod test { // Read out the commit record. let commit: Object = - serde_ipld_dagcbor::from_reader(&DATA[..]).unwrap(); + serde_ipld_dagcbor::from_reader(DATA).unwrap(); println!("{:?}", commit.ops); - let mut repo = load(commit.blocks.as_slice()).await.unwrap(); + let _repo = load(commit.blocks.as_slice()).await.unwrap(); } #[tokio::test] @@ -203,7 +203,7 @@ mod test { // Read out the commit record. let commit: Object = - serde_ipld_dagcbor::from_reader(&DATA[..]).unwrap(); + serde_ipld_dagcbor::from_reader(DATA).unwrap(); println!("{:?}", commit.ops); diff --git a/examples/firehose/src/stream/frames.rs b/examples/firehose/src/stream/frames.rs index 3edd1c1e..bb9d2126 100644 --- a/examples/firehose/src/stream/frames.rs +++ b/examples/firehose/src/stream/frames.rs @@ -80,12 +80,7 @@ impl TryFrom<&[u8]> for Frame { }; let header = FrameHeader::try_from(serde_ipld_dagcbor::from_slice::(left)?)?; if let FrameHeader::Message(t) = &header { - Ok(Frame::Message( - t.clone(), - MessageFrame { - body: right.to_vec(), - }, - )) + Ok(Frame::Message(t.clone(), MessageFrame { body: right.to_vec() })) } else { Ok(Frame::Error(ErrorFrame {})) } @@ -103,10 +98,7 @@ mod tests { b'a'..=b'f' => b - b'a' + 10, _ => unreachable!(), }; - s.as_bytes() - .chunks(2) - .map(|b| (b2u(b[0]) << 4) + b2u(b[1])) - .collect() + s.as_bytes().chunks(2).map(|b| (b2u(b[0]) << 4) + b2u(b[1])).collect() } #[test] @@ -138,10 +130,7 @@ mod tests { let ipld = serde_ipld_dagcbor::from_slice::(&data).expect("failed to deserialize"); let result = FrameHeader::try_from(ipld); - assert_eq!( - result.expect_err("must be failed").to_string(), - "invalid frame type" - ); + assert_eq!(result.expect_err("must be failed").to_string(), "invalid frame type"); } { // {"op": -2} @@ -149,10 +138,7 @@ mod tests { let ipld = serde_ipld_dagcbor::from_slice::(&data).expect("failed to deserialize"); let result = FrameHeader::try_from(ipld); - assert_eq!( - result.expect_err("must be failed").to_string(), - "invalid frame type" - ); + assert_eq!(result.expect_err("must be failed").to_string(), "invalid frame type"); } } }