refactor: replace into bound with standalone function (#14512)

This commit is contained in:
caglarkaya
2025-02-15 18:05:17 +03:00
committed by GitHub
parent 0f4914a944
commit cb615cf5e1
8 changed files with 53 additions and 6 deletions

View File

@ -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)]

View File

@ -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)]

View File

@ -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.

View File

@ -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()
}
}
}

View File

@ -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()
}
}
}

View File

@ -429,7 +429,7 @@ pub(super) mod serde_bincode_compat {
From<SealedBlock<'a, T>> for super::SealedBlock<T>
{
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()
}
}
}

View File

@ -274,7 +274,7 @@ pub(super) mod serde_bincode_compat {
impl<'a, H: Sealable + SerdeBincodeCompat> From<SealedHeader<'a, H>> for super::SealedHeader<H> {
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)]

View File

@ -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<Self>;
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<T, H>
{
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()
}
}
}