chore: add helper for encoded (#12868)

This commit is contained in:
Matthias Seitz
2024-11-26 13:00:07 +01:00
committed by GitHub
parent a28fa243c0
commit 334ccd0a53

View File

@ -1,3 +1,4 @@
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::Bytes; use alloy_primitives::Bytes;
/// Generic wrapper with encoded Bytes, such as transaction data. /// Generic wrapper with encoded Bytes, such as transaction data.
@ -17,8 +18,8 @@ impl<T> WithEncoded<T> {
} }
/// Get the encoded bytes /// Get the encoded bytes
pub fn encoded_bytes(&self) -> Bytes { pub const fn encoded_bytes(&self) -> &Bytes {
self.0.clone() &self.0
} }
/// Get the underlying value /// Get the underlying value
@ -47,6 +48,13 @@ impl<T> WithEncoded<T> {
} }
} }
impl<T: Encodable2718> WithEncoded<T> {
/// Wraps the value with the [`Encodable2718::encoded_2718`] bytes.
pub fn from_2718_encodable(value: T) -> Self {
Self(value.encoded_2718().into(), value)
}
}
impl<T> WithEncoded<Option<T>> { impl<T> WithEncoded<Option<T>> {
/// returns `None` if the inner value is `None`, otherwise returns `Some(WithEncoded<T>)`. /// returns `None` if the inner value is `None`, otherwise returns `Some(WithEncoded<T>)`.
pub fn transpose(self) -> Option<WithEncoded<T>> { pub fn transpose(self) -> Option<WithEncoded<T>> {