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>.

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.

Object Safety§

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 AccessList

Available on crate feature alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 Request

Available on crate feature alloy 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 Signature

Available on crate feature alloy 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 alloy 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 alloy 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 optimism and alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 alloy 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 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 alloy 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<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> 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§