diff --git a/crates/ethereum/primitives/src/transaction.rs b/crates/ethereum/primitives/src/transaction.rs index c8f9a0133..59f738c9c 100644 --- a/crates/ethereum/primitives/src/transaction.rs +++ b/crates/ethereum/primitives/src/transaction.rs @@ -898,6 +898,10 @@ pub mod serde_bincode_compat { } impl SerdeBincodeCompat for super::TransactionSigned { type BincodeRepr<'a> = TransactionSigned<'a>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } #[cfg(test)] diff --git a/crates/optimism/primitives/src/transaction/signed.rs b/crates/optimism/primitives/src/transaction/signed.rs index a6def1d4a..1a23958be 100644 --- a/crates/optimism/primitives/src/transaction/signed.rs +++ b/crates/optimism/primitives/src/transaction/signed.rs @@ -711,5 +711,9 @@ pub mod serde_bincode_compat { impl SerdeBincodeCompat for super::OpTransactionSigned { type BincodeRepr<'a> = OpTransactionSigned<'a>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } } diff --git a/crates/primitives-traits/src/block/recovered.rs b/crates/primitives-traits/src/block/recovered.rs index 9ae2b5f0a..c46b8b2e6 100644 --- a/crates/primitives-traits/src/block/recovered.rs +++ b/crates/primitives-traits/src/block/recovered.rs @@ -602,5 +602,9 @@ pub(super) mod serde_bincode_compat { SerdeBincodeCompat for super::RecoveredBlock { type BincodeRepr<'a> = RecoveredBlock<'a, T>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } } diff --git a/crates/primitives-traits/src/block/sealed.rs b/crates/primitives-traits/src/block/sealed.rs index 611b1ad7e..4a12556cb 100644 --- a/crates/primitives-traits/src/block/sealed.rs +++ b/crates/primitives-traits/src/block/sealed.rs @@ -421,7 +421,7 @@ pub(super) mod serde_bincode_compat { From<&'a super::SealedBlock> for SealedBlock<'a, T> { fn from(value: &'a super::SealedBlock) -> Self { - Self { header: (&value.header).into(), body: (&value.body).into() } + Self { header: value.header.as_repr(), body: value.body.as_repr() } } } @@ -459,5 +459,9 @@ pub(super) mod serde_bincode_compat { SerdeBincodeCompat for super::SealedBlock { type BincodeRepr<'a> = SealedBlock<'a, T>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } } diff --git a/crates/primitives-traits/src/header/sealed.rs b/crates/primitives-traits/src/header/sealed.rs index c852ba26d..8f12ea425 100644 --- a/crates/primitives-traits/src/header/sealed.rs +++ b/crates/primitives-traits/src/header/sealed.rs @@ -268,7 +268,7 @@ pub(super) mod serde_bincode_compat { for SealedHeader<'a, H> { fn from(value: &'a super::SealedHeader) -> Self { - Self { hash: value.hash(), header: (&value.header).into() } + Self { hash: value.hash(), header: value.header.as_repr() } } } @@ -298,6 +298,9 @@ pub(super) mod serde_bincode_compat { impl SerdeBincodeCompat for super::SealedHeader { type BincodeRepr<'a> = SealedHeader<'a, H>; + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } #[cfg(test)] diff --git a/crates/primitives-traits/src/serde_bincode_compat.rs b/crates/primitives-traits/src/serde_bincode_compat.rs index 1921210d2..1c323f2b2 100644 --- a/crates/primitives-traits/src/serde_bincode_compat.rs +++ b/crates/primitives-traits/src/serde_bincode_compat.rs @@ -15,11 +15,18 @@ 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 + From<&'a Self> + Into; + type BincodeRepr<'a>: Debug + Serialize + DeserializeOwned + Into; + + /// Convert this type into its bincode representation + fn as_repr(&self) -> Self::BincodeRepr<'_>; } impl SerdeBincodeCompat for alloy_consensus::Header { type BincodeRepr<'a> = alloy_consensus::serde_bincode_compat::Header<'a>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } /// Type alias for the [`SerdeBincodeCompat::BincodeRepr`] associated type. @@ -61,7 +68,7 @@ mod block_bincode { for Block<'a, T, H> { fn from(value: &'a alloy_consensus::Block) -> Self { - Self { header: (&value.header).into(), body: (&value.body).into() } + Self { header: value.header.as_repr(), body: (&value.body).into() } } } @@ -102,6 +109,10 @@ mod block_bincode { for alloy_consensus::Block { type BincodeRepr<'a> = Block<'a, T, H>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } /// Bincode-compatible [`alloy_consensus::BlockBody`] serde implementation. @@ -130,7 +141,7 @@ mod block_bincode { impl<'a, T: SerdeBincodeCompat> From<&'a alloy_consensus::BlockBody> for BlockBody<'a, T> { fn from(value: &'a alloy_consensus::BlockBody) -> Self { Self { - transactions: value.transactions.iter().map(Into::into).collect(), + transactions: value.transactions.iter().map(|tx| tx.as_repr()).collect(), ommers: value.ommers.iter().map(Into::into).collect(), withdrawals: Cow::Borrowed(&value.withdrawals), } @@ -172,5 +183,9 @@ mod block_bincode { impl SerdeBincodeCompat for alloy_consensus::BlockBody { type BincodeRepr<'a> = BlockBody<'a, T>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + self.into() + } } }