reth_codecs

Trait Compact

Source
pub trait Compact: Sized {
    // Required methods
    fn to_compact<B>(&self, buf: &mut B) -> usize
       where B: BufMut + AsMut<[u8]>;
    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]);

    // Provided methods
    fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
       where B: BufMut + AsMut<[u8]> { ... }
    fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) { ... }
}
Expand description

Trait that implements the Compact codec.

When deriving the trait for custom structs, be aware of certain limitations/recommendations:

  • Works best with structs that only have native types (eg. u64, B256, U256).
  • Fixed array types (B256, Address, Bloom) are not compacted.
  • Max size of T in Option<T> or Vec<T> shouldn’t exceed 0xffff.
  • Any Bytes field should be placed last.
  • Any other type which is not known to the derive module should be placed last in they contain a Bytes field.

The last two points make it easier to decode the data without saving the length on the StructFlags. It will fail compilation if it’s not respected. If they’re alias to known types, add their definitions to get_bit_size() or known_types in generator.rs.

Regarding the specialized_to/from_compact methods: Mainly used as a workaround for not being able to specialize an impl over certain types like Vec<T>/Option<T> where T is a fixed size array like Vec<B256>.

§Caution

Due to the bitfields, every type change on the rust type (e.g. U256 to u64) is a breaking change and will lead to a new, incompatible Compact implementation. Implementers must take special care when changing or rearranging fields.

Required Methods§

Source

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Takes a buffer which can be written to. Ideally, it returns the length written to.

Source

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Takes a buffer which can be read from. Returns the object and buf with its internal cursor advanced (eg..advance(len)).

len can either be the buf remaining length, or the length of the compacted type.

It will panic, if len is smaller than buf.len().

Provided Methods§

Source

fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

“Optional”: If there’s no good reason to use it, don’t.

Source

fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

“Optional”: If there’s no good reason to use it, don’t.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Compact for bool

Source§

fn to_compact<B>(&self, _: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

bool vars go directly to the StructFlags and are not written to the buffer.

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

bool expects the real value to come in len, and does not advance the cursor.

Source§

impl Compact for u8

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for u64

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for u128

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for String

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for AccessList

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl Compact for AccessListItem

Available on crate feature test-utils only.

Implement Compact for AccessListItem and AccessList.

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl Compact for Address

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for Authorization

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for Bloom

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for BranchNodeCompact

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _len: usize) -> (Self, &[u8])

Source§

impl Compact for Bytes

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for GenesisAccount

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for HashBuilderValue

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl Compact for Header

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for Log

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl Compact for LogData

Available on crate feature test-utils only.

Implement Compact for LogData and Log.

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl Compact for OpTxType

Available on crate features test-utils and op only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8])

Source§

impl Compact for OpTypedTransaction

Available on crate features test-utils and op only.
Source§

fn to_compact<B>(&self, out: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8])

Source§

impl Compact for PrimitiveSignature

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8])

Source§

impl Compact for SignedAuthorization

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TrieMask

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _len: usize) -> (Self, &[u8])

Source§

impl Compact for TxDeposit

Available on crate features test-utils and op only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxEip1559

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxEip2930

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxEip4844

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxEip7702

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxKind

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8])

Source§

impl Compact for TxLegacy

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for TxType

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8])

Source§

impl Compact for U256

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for Withdrawal

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl Compact for Withdrawals

Available on crate feature test-utils only.
Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl<T> Compact for &[T]
where T: Compact,

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Returns 0 since we won’t include it in the StructFlags.

Source§

fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

To be used by fixed sized types like &[B256].

Source§

fn from_compact(_: &[u8], _: usize) -> (Self, &[u8])

Source§

fn specialized_from_compact(_: &[u8], _: usize) -> (Self, &[u8])

Source§

impl<T> Compact for Option<T>
where T: Compact,

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Returns 0 for None and 1 for Some(_).

Source§

fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

To be used by fixed sized types like Option<B256>.

Source§

fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

To be used by fixed sized types like Option<B256>.

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl<T> Compact for Vec<T>
where T: Compact,

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Returns 0 since we won’t include it in the StructFlags.

Source§

fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

To be used by fixed sized types like Vec<B256>.

Source§

fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

To be used by fixed sized types like Vec<B256>.

Source§

fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])

Source§

impl<T: Compact + ToOwned<Owned = T>> Compact for Cow<'_, T>

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl<T: Compact> Compact for &T

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(_: &[u8], _: usize) -> (Self, &[u8])

Source§

impl<const N: usize> Compact for FixedBytes<N>

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Source§

impl<const N: usize> Compact for [u8; N]

Source§

fn to_compact<B>(&self, buf: &mut B) -> usize
where B: BufMut + AsMut<[u8]>,

Source§

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])

Implementors§

Source§

impl Compact for reth_codecs::alloy::authorization_list::Authorization

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::genesis_account::GenesisAccount

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::header::Header

Available on crate feature test-utils only.
Source§

impl Compact for HeaderExt

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::eip1559::TxEip1559

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::eip2930::TxEip2930

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::eip4844::TxEip4844

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::eip7702::TxEip7702

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::legacy::TxLegacy

Available on crate feature test-utils only.
Source§

impl Compact for reth_codecs::alloy::transaction::optimism::TxDeposit

Available on crate features test-utils and op only.
Source§

impl Compact for reth_codecs::alloy::withdrawal::Withdrawal

Available on crate feature test-utils only.
Source§

impl Compact for CompactPlaceholder