From 2c7f1ed66bb3276e91dbd391e07bc950b6d7ad19 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Dec 2024 20:22:09 +0100 Subject: [PATCH] chore: map header fns (#1840) --- crates/consensus/src/block/mod.rs | 10 ++++++++++ crates/rpc-types-eth/src/block.rs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index cb316f3ed60..5e54f9e2d2e 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -44,6 +44,16 @@ impl Block { self.body } + /// Converts the block's header type by applying a function to it. + pub fn map_header(self, f: impl FnOnce(H) -> U) -> Block { + 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(self, f: impl FnOnce(H) -> Result) -> Result, 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. diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 4225798a1c4..1ad362f0fa2 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -56,6 +56,26 @@ impl Default for Block { } impl Block { + /// Converts the block's header type by applying a function to it. + pub fn map_header(self, f: impl FnOnce(H) -> U) -> Block { + 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(self, f: impl FnOnce(H) -> Result) -> Result, 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.