Skip to content

Commit

Permalink
feat: replace BincodeRepr From bound with conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed Jan 29, 2025
1 parent 5fcefce commit 3673207
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions crates/ethereum/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 4 additions & 0 deletions crates/optimism/primitives/src/transaction/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
4 changes: 4 additions & 0 deletions crates/primitives-traits/src/block/recovered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,5 +602,9 @@ pub(super) mod serde_bincode_compat {
SerdeBincodeCompat for super::RecoveredBlock<T>
{
type BincodeRepr<'a> = RecoveredBlock<'a, T>;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
self.into()
}
}
}
6 changes: 5 additions & 1 deletion crates/primitives-traits/src/block/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub(super) mod serde_bincode_compat {
From<&'a super::SealedBlock<T>> for SealedBlock<'a, T>
{
fn from(value: &'a super::SealedBlock<T>) -> Self {
Self { header: (&value.header).into(), body: (&value.body).into() }
Self { header: value.header.as_repr(), body: value.body.as_repr() }
}
}

Expand Down Expand Up @@ -459,5 +459,9 @@ pub(super) mod serde_bincode_compat {
SerdeBincodeCompat for super::SealedBlock<T>
{
type BincodeRepr<'a> = SealedBlock<'a, T>;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
self.into()
}
}
}
5 changes: 4 additions & 1 deletion crates/primitives-traits/src/header/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub(super) mod serde_bincode_compat {
for SealedHeader<'a, H>
{
fn from(value: &'a super::SealedHeader<H>) -> Self {
Self { hash: value.hash(), header: (&value.header).into() }
Self { hash: value.hash(), header: value.header.as_repr() }
}
}

Expand Down Expand Up @@ -298,6 +298,9 @@ pub(super) mod serde_bincode_compat {

impl<H: Sealable + SerdeBincodeCompat> SerdeBincodeCompat for super::SealedHeader<H> {
type BincodeRepr<'a> = SealedHeader<'a, H>;
fn as_repr(&self) -> Self::BincodeRepr<'_> {
self.into()
}
}

#[cfg(test)]
Expand Down
21 changes: 18 additions & 3 deletions crates/primitives-traits/src/serde_bincode_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;
type BincodeRepr<'a>: Debug + Serialize + DeserializeOwned + Into<Self>;

/// 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.
Expand Down Expand Up @@ -61,7 +68,7 @@ mod block_bincode {
for Block<'a, T, H>
{
fn from(value: &'a alloy_consensus::Block<T, H>) -> Self {
Self { header: (&value.header).into(), body: (&value.body).into() }
Self { header: value.header.as_repr(), body: (&value.body).into() }
}
}

Expand Down Expand Up @@ -102,6 +109,10 @@ mod block_bincode {
for alloy_consensus::Block<T, H>
{
type BincodeRepr<'a> = Block<'a, T, H>;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
self.into()
}
}

/// Bincode-compatible [`alloy_consensus::BlockBody`] serde implementation.
Expand Down Expand Up @@ -130,7 +141,7 @@ mod block_bincode {
impl<'a, T: SerdeBincodeCompat> From<&'a alloy_consensus::BlockBody<T>> for BlockBody<'a, T> {
fn from(value: &'a alloy_consensus::BlockBody<T>) -> 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),
}
Expand Down Expand Up @@ -172,5 +183,9 @@ mod block_bincode {

impl<T: SerdeBincodeCompat> SerdeBincodeCompat for alloy_consensus::BlockBody<T> {
type BincodeRepr<'a> = BlockBody<'a, T>;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
self.into()
}
}
}

0 comments on commit 3673207

Please sign in to comment.