From cb615cf5e1c9d5631efcd723000dfa095acf1408 Mon Sep 17 00:00:00 2001 From: caglarkaya Date: Sat, 15 Feb 2025 18:05:17 +0300 Subject: [PATCH] refactor: replace into bound with standalone function (#14512) --- crates/ethereum/primitives/src/receipt.rs | 4 +++ crates/ethereum/primitives/src/transaction.rs | 4 +++ crates/optimism/primitives/src/receipt.rs | 4 +++ .../primitives/src/transaction/signed.rs | 4 +++ .../primitives-traits/src/block/recovered.rs | 4 +++ crates/primitives-traits/src/block/sealed.rs | 6 ++++- crates/primitives-traits/src/header/sealed.rs | 6 ++++- .../src/serde_bincode_compat.rs | 27 ++++++++++++++++--- 8 files changed, 53 insertions(+), 6 deletions(-) diff --git a/crates/ethereum/primitives/src/receipt.rs b/crates/ethereum/primitives/src/receipt.rs index e364a13ae..aabecf4b9 100644 --- a/crates/ethereum/primitives/src/receipt.rs +++ b/crates/ethereum/primitives/src/receipt.rs @@ -200,6 +200,10 @@ impl reth_primitives_traits::serde_bincode_compat::SerdeBincodeCompat for Receip fn as_repr(&self) -> Self::BincodeRepr<'_> { self.clone() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr + } } #[cfg(test)] diff --git a/crates/ethereum/primitives/src/transaction.rs b/crates/ethereum/primitives/src/transaction.rs index 159be023b..7a481d3b6 100644 --- a/crates/ethereum/primitives/src/transaction.rs +++ b/crates/ethereum/primitives/src/transaction.rs @@ -974,6 +974,10 @@ pub mod serde_bincode_compat { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } #[cfg(test)] diff --git a/crates/optimism/primitives/src/receipt.rs b/crates/optimism/primitives/src/receipt.rs index 019095dbc..05033592c 100644 --- a/crates/optimism/primitives/src/receipt.rs +++ b/crates/optimism/primitives/src/receipt.rs @@ -218,6 +218,10 @@ impl reth_primitives_traits::serde_bincode_compat::SerdeBincodeCompat for OpRece fn as_repr(&self) -> Self::BincodeRepr<'_> { self.clone() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr + } } /// Trait for deposit receipt. diff --git a/crates/optimism/primitives/src/transaction/signed.rs b/crates/optimism/primitives/src/transaction/signed.rs index 64790226b..688eb9f66 100644 --- a/crates/optimism/primitives/src/transaction/signed.rs +++ b/crates/optimism/primitives/src/transaction/signed.rs @@ -779,5 +779,9 @@ pub mod serde_bincode_compat { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } } diff --git a/crates/primitives-traits/src/block/recovered.rs b/crates/primitives-traits/src/block/recovered.rs index e2c53a601..934a59884 100644 --- a/crates/primitives-traits/src/block/recovered.rs +++ b/crates/primitives-traits/src/block/recovered.rs @@ -606,5 +606,9 @@ pub(super) mod serde_bincode_compat { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } } diff --git a/crates/primitives-traits/src/block/sealed.rs b/crates/primitives-traits/src/block/sealed.rs index 4a12556cb..3f50d7f90 100644 --- a/crates/primitives-traits/src/block/sealed.rs +++ b/crates/primitives-traits/src/block/sealed.rs @@ -429,7 +429,7 @@ pub(super) mod serde_bincode_compat { From> for super::SealedBlock { fn from(value: SealedBlock<'a, T>) -> Self { - Self::from_sealed_parts(value.header.into(), value.body.into()) + Self::from_sealed_parts(value.header.into(), SerdeBincodeCompat::from_repr(value.body)) } } @@ -463,5 +463,9 @@ pub(super) mod serde_bincode_compat { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } } diff --git a/crates/primitives-traits/src/header/sealed.rs b/crates/primitives-traits/src/header/sealed.rs index 8f12ea425..dc59c1f06 100644 --- a/crates/primitives-traits/src/header/sealed.rs +++ b/crates/primitives-traits/src/header/sealed.rs @@ -274,7 +274,7 @@ pub(super) mod serde_bincode_compat { impl<'a, H: Sealable + SerdeBincodeCompat> From> for super::SealedHeader { fn from(value: SealedHeader<'a, H>) -> Self { - Self::new(value.header.into(), value.hash) + Self::new(SerdeBincodeCompat::from_repr(value.header), value.hash) } } @@ -301,6 +301,10 @@ pub(super) mod serde_bincode_compat { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } #[cfg(test)] diff --git a/crates/primitives-traits/src/serde_bincode_compat.rs b/crates/primitives-traits/src/serde_bincode_compat.rs index 8e5e8936b..fe5a1822d 100644 --- a/crates/primitives-traits/src/serde_bincode_compat.rs +++ b/crates/primitives-traits/src/serde_bincode_compat.rs @@ -15,10 +15,13 @@ pub trait SerdeBincodeCompat: Sized + 'static { /// Serde representation of the type for bincode serialization. /// /// This type defines the bincode compatible serde format for the type. - type BincodeRepr<'a>: Debug + Serialize + DeserializeOwned + Into; + type BincodeRepr<'a>: Debug + Serialize + DeserializeOwned; /// Convert this type into its bincode representation fn as_repr(&self) -> Self::BincodeRepr<'_>; + + /// Convert from the bincode representation + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self; } impl SerdeBincodeCompat for alloy_consensus::Header { @@ -27,6 +30,10 @@ impl SerdeBincodeCompat for alloy_consensus::Header { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } /// Type alias for the [`SerdeBincodeCompat::BincodeRepr`] associated type. @@ -75,7 +82,7 @@ mod block_bincode { for alloy_consensus::Block { fn from(value: Block<'a, T, H>) -> Self { - Self { header: value.header.into(), body: value.body.into() } + Self { header: SerdeBincodeCompat::from_repr(value.header), body: value.body.into() } } } @@ -112,6 +119,10 @@ mod block_bincode { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } /// Bincode-compatible [`alloy_consensus::BlockBody`] serde implementation. @@ -154,8 +165,12 @@ mod block_bincode { { fn from(value: BlockBody<'a, T, H>) -> Self { Self { - transactions: value.transactions.into_iter().map(Into::into).collect(), - ommers: value.ommers.into_iter().map(Into::into).collect(), + transactions: value + .transactions + .into_iter() + .map(SerdeBincodeCompat::from_repr) + .collect(), + ommers: value.ommers.into_iter().map(SerdeBincodeCompat::from_repr).collect(), withdrawals: value.withdrawals.into_owned(), } } @@ -194,5 +209,9 @@ mod block_bincode { fn as_repr(&self) -> Self::BincodeRepr<'_> { self.into() } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + repr.into() + } } }