chore: move withencoded struct (#12770)

This commit is contained in:
Matthias Seitz
2024-11-22 12:06:42 +01:00
committed by GitHub
parent ef3e0b360f
commit f2126f2c05
3 changed files with 58 additions and 55 deletions

View File

@ -0,0 +1,55 @@
use alloy_primitives::Bytes;
/// Generic wrapper with encoded Bytes, such as transaction data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WithEncoded<T>(Bytes, pub T);
impl<T> From<(Bytes, T)> for WithEncoded<T> {
fn from(value: (Bytes, T)) -> Self {
Self(value.0, value.1)
}
}
impl<T> WithEncoded<T> {
/// Wraps the value with the bytes.
pub const fn new(bytes: Bytes, value: T) -> Self {
Self(bytes, value)
}
/// Get the encoded bytes
pub fn encoded_bytes(&self) -> Bytes {
self.0.clone()
}
/// Get the underlying value
pub const fn value(&self) -> &T {
&self.1
}
/// Returns ownership of the underlying value.
pub fn into_value(self) -> T {
self.1
}
/// Transform the value
pub fn transform<F: From<T>>(self) -> WithEncoded<F> {
WithEncoded(self.0, self.1.into())
}
/// Split the wrapper into [`Bytes`] and value tuple
pub fn split(self) -> (Bytes, T) {
(self.0, self.1)
}
/// Maps the inner value to a new value using the given function.
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> WithEncoded<U> {
WithEncoded(self.0, op(self.1))
}
}
impl<T> WithEncoded<Option<T>> {
/// returns `None` if the inner value is `None`, otherwise returns `Some(WithEncoded<T>)`.
pub fn transpose(self) -> Option<WithEncoded<T>> {
self.1.map(|v| WithEncoded(self.0, v))
}
}

View File

@ -14,7 +14,6 @@ extern crate alloc;
/// Common constants. /// Common constants.
pub mod constants; pub mod constants;
pub use constants::gas_units::{format_gas, format_gas_throughput}; pub use constants::gas_units::{format_gas, format_gas_throughput};
/// Minimal account /// Minimal account
@ -42,7 +41,9 @@ pub use block::{
Block, FullBlock, Block, FullBlock,
}; };
mod encoded;
mod withdrawal; mod withdrawal;
pub use encoded::WithEncoded;
mod error; mod error;
pub use error::{GotExpected, GotExpectedBoxed}; pub use error::{GotExpected, GotExpectedBoxed};

View File

@ -40,6 +40,7 @@ pub use error::{
}; };
pub use meta::TransactionMeta; pub use meta::TransactionMeta;
pub use pooled::{PooledTransactionsElement, PooledTransactionsElementEcRecovered}; pub use pooled::{PooledTransactionsElement, PooledTransactionsElementEcRecovered};
pub use reth_primitives_traits::WithEncoded;
pub use sidecar::BlobTransaction; pub use sidecar::BlobTransaction;
pub use signature::{recover_signer, recover_signer_unchecked}; pub use signature::{recover_signer, recover_signer_unchecked};
pub use tx_type::TxType; pub use tx_type::TxType;
@ -1764,60 +1765,6 @@ impl Decodable for TransactionSignedEcRecovered {
} }
} }
/// Generic wrapper with encoded Bytes, such as transaction data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WithEncoded<T>(Bytes, pub T);
impl<T> From<(Bytes, T)> for WithEncoded<T> {
fn from(value: (Bytes, T)) -> Self {
Self(value.0, value.1)
}
}
impl<T> WithEncoded<T> {
/// Wraps the value with the bytes.
pub const fn new(bytes: Bytes, value: T) -> Self {
Self(bytes, value)
}
/// Get the encoded bytes
pub fn encoded_bytes(&self) -> Bytes {
self.0.clone()
}
/// Get the underlying value
pub const fn value(&self) -> &T {
&self.1
}
/// Returns ownership of the underlying value.
pub fn into_value(self) -> T {
self.1
}
/// Transform the value
pub fn transform<F: From<T>>(self) -> WithEncoded<F> {
WithEncoded(self.0, self.1.into())
}
/// Split the wrapper into [`Bytes`] and value tuple
pub fn split(self) -> (Bytes, T) {
(self.0, self.1)
}
/// Maps the inner value to a new value using the given function.
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> WithEncoded<U> {
WithEncoded(self.0, op(self.1))
}
}
impl<T> WithEncoded<Option<T>> {
/// returns `None` if the inner value is `None`, otherwise returns `Some(WithEncoded<T>)`.
pub fn transpose(self) -> Option<WithEncoded<T>> {
self.1.map(|v| WithEncoded(self.0, v))
}
}
/// Bincode-compatible transaction type serde implementations. /// Bincode-compatible transaction type serde implementations.
#[cfg(feature = "serde-bincode-compat")] #[cfg(feature = "serde-bincode-compat")]
pub mod serde_bincode_compat { pub mod serde_bincode_compat {