From b58448c30caa3cfc15bf696c020abb01001cac89 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 14 Dec 2022 13:47:12 +0200 Subject: [PATCH] Implements From impl from SerializeError to MerkleizationError::SerializationError. --- ssz-rs/examples/container_with_some_types.rs | 2 +- ssz-rs/src/bitlist.rs | 5 ++--- ssz-rs/src/bitvector.rs | 4 +--- ssz-rs/src/merkleization/mod.rs | 11 ++++++++--- ssz-rs/src/uint.rs | 4 +--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ssz-rs/examples/container_with_some_types.rs b/ssz-rs/examples/container_with_some_types.rs index bf5d019f..c1b73b9a 100644 --- a/ssz-rs/examples/container_with_some_types.rs +++ b/ssz-rs/examples/container_with_some_types.rs @@ -60,7 +60,7 @@ fn main() { let mut restored_foo = match Foo::<4>::deserialize(&encoding) { Ok(value) => value, Err(e) => { - eprintln!("some error decoding: {:?}", e); + eprintln!("some error decoding: {}", e); return; } }; diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index a19cc2d3..41ce27be 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -86,9 +86,7 @@ impl Bitlist { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self - .serialize_with_length(&mut data, false) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize_with_length(&mut data, false); pack_bytes(&mut data); Ok(data) } @@ -210,6 +208,7 @@ impl FromIterator for Bitlist { #[cfg(test)] mod tests { + use std::hash::Hash; use super::*; use crate::serialize; diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 1210ed75..e2eadfac 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -93,9 +93,7 @@ impl Bitvector { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self - .serialize(&mut data) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize(&mut data); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index e2799db6..7e59b7c2 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -10,6 +10,7 @@ use sha2::{Digest, Sha256}; pub use cache::Cache as MerkleCache; pub use node::Node; pub use proofs::is_valid_merkle_branch; +use crate::SerializeError; pub(crate) const BYTES_PER_CHUNK: usize = 32; @@ -25,6 +26,12 @@ pub enum MerkleizationError { InputExceedsLimit(usize), // cannot merkleize data that exceeds the declared limit {0} } +impl From for MerkleizationError { + fn from(_: SerializeError) -> Self { + return MerkleizationError::SerializationError + } +} + impl Display for MerkleizationError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { @@ -60,9 +67,7 @@ where { let mut buffer = vec![]; for value in values { - value - .serialize(&mut buffer) - .map_err(|_| MerkleizationError::SerializationError)?; + value.serialize(&mut buffer); } pack_bytes(&mut buffer); Ok(buffer) diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 398c0a14..30552687 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -44,9 +44,7 @@ macro_rules! define_uint { impl Merkleized for $uint { fn hash_tree_root(&mut self) -> Result { let mut root = vec![]; - let _ = self - .serialize(&mut root) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize(&mut root)?; pack_bytes(&mut root); Ok(root.as_slice().try_into().expect("is valid root")) }