reth_primitives_traits/
encoded.rs1use alloy_eips::eip2718::Encodable2718;
2use alloy_primitives::Bytes;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct WithEncoded<T>(Bytes, pub T);
7
8impl<T> From<(Bytes, T)> for WithEncoded<T> {
9 fn from(value: (Bytes, T)) -> Self {
10 Self(value.0, value.1)
11 }
12}
13
14impl<T> WithEncoded<T> {
15 pub const fn new(bytes: Bytes, value: T) -> Self {
17 Self(bytes, value)
18 }
19
20 pub const fn encoded_bytes(&self) -> &Bytes {
22 &self.0
23 }
24
25 pub const fn value(&self) -> &T {
27 &self.1
28 }
29
30 pub fn into_value(self) -> T {
32 self.1
33 }
34
35 pub fn transform<F: From<T>>(self) -> WithEncoded<F> {
37 WithEncoded(self.0, self.1.into())
38 }
39
40 pub fn split(self) -> (Bytes, T) {
42 (self.0, self.1)
43 }
44
45 pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> WithEncoded<U> {
47 WithEncoded(self.0, op(self.1))
48 }
49}
50
51impl<T: Encodable2718> WithEncoded<T> {
52 pub fn from_2718_encodable(value: T) -> Self {
54 Self(value.encoded_2718().into(), value)
55 }
56}
57
58impl<T> WithEncoded<Option<T>> {
59 pub fn transpose(self) -> Option<WithEncoded<T>> {
61 self.1.map(|v| WithEncoded(self.0, v))
62 }
63}