Skip to content

Commit

Permalink
chore: map header fns (#1840)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 23, 2024
1 parent 4cd7313 commit 2c7f1ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/consensus/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ impl<T, H> Block<T, H> {
self.body
}

/// Converts the block's header type by applying a function to it.
pub fn map_header<U>(self, f: impl FnOnce(H) -> U) -> Block<T, U> {
Block { header: f(self.header), body: self.body }
}

/// Converts the block's header type by applying a fallible function to it.
pub fn try_map_header<U, E>(self, f: impl FnOnce(H) -> Result<U, E>) -> Result<Block<T, U>, E> {
Ok(Block { header: f(self.header)?, body: self.body })
}

/// Converts the block's transaction type by applying a function to each transaction.
///
/// Returns the block with the new transaction type.
Expand Down
20 changes: 20 additions & 0 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ impl<T, H: Default> Default for Block<T, H> {
}

impl<T, H> Block<T, H> {
/// Converts the block's header type by applying a function to it.
pub fn map_header<U>(self, f: impl FnOnce(H) -> U) -> Block<T, U> {
Block {
header: f(self.header),
uncles: self.uncles,
transactions: self.transactions,
withdrawals: self.withdrawals,
}
}

/// Converts the block's header type by applying a fallible function to it.
pub fn try_map_header<U, E>(self, f: impl FnOnce(H) -> Result<U, E>) -> Result<Block<T, U>, E> {
Ok(Block {
header: f(self.header)?,
uncles: self.uncles,
transactions: self.transactions,
withdrawals: self.withdrawals,
})
}

/// Converts the block's transaction type by applying a function to each transaction.
///
/// Returns the block with the new transaction type.
Expand Down

0 comments on commit 2c7f1ed

Please sign in to comment.