Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add helper for encoded #12868

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading