Skip to content

Commit

Permalink
chore: add helper for encoded (#12868)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 26, 2024
1 parent a28fa24 commit 334ccd0
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/primitives-traits/src/encoded.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::Bytes;

/// Generic wrapper with encoded Bytes, such as transaction data.
Expand All @@ -17,8 +18,8 @@ impl<T> WithEncoded<T> {
}

/// Get the encoded bytes
pub fn encoded_bytes(&self) -> Bytes {
self.0.clone()
pub const fn encoded_bytes(&self) -> &Bytes {
&self.0
}

/// Get the underlying value
Expand Down Expand Up @@ -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>> {
/// returns `None` if the inner value is `None`, otherwise returns `Some(WithEncoded<T>)`.
pub fn transpose(self) -> Option<WithEncoded<T>> {
Expand Down

0 comments on commit 334ccd0

Please sign in to comment.