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
inOption<T>
orVec<T>
shouldn’t exceed0xffff
. - 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§
Sourcefn to_compact<B>(&self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
Takes a buffer which can be written to. Ideally, it returns the length written to.
Sourcefn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
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§
Sourcefn specialized_to_compact<B>(&self, buf: &mut B) -> usize
fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
“Optional”: If there’s no good reason to use it, don’t.
Sourcefn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
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 AccessListItem
Available on crate feature test-utils
only.Implement Compact
for AccessListItem
and AccessList
.
impl Compact for AccessListItem
test-utils
only.Implement Compact
for AccessListItem
and AccessList
.
Source§impl Compact for LogData
Available on crate feature test-utils
only.Implement Compact
for LogData
and Log
.
impl Compact for LogData
test-utils
only.Implement Compact
for LogData
and Log
.
Source§impl<T> Compact for &[T]where
T: Compact,
impl<T> Compact for &[T]where
T: Compact,
Source§fn to_compact<B>(&self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
Returns 0 since we won’t include it in the StructFlags
.
Source§fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
To be used by fixed sized types like &[B256]
.
fn from_compact(_: &[u8], _: usize) -> (Self, &[u8])
fn specialized_from_compact(_: &[u8], _: usize) -> (Self, &[u8])
Source§impl<T> Compact for Option<T>where
T: Compact,
impl<T> Compact for Option<T>where
T: Compact,
Source§fn to_compact<B>(&self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
Returns 0 for None
and 1 for Some(_)
.
Source§fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
To be used by fixed sized types like Option<B256>
.
Source§fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
To be used by fixed sized types like Option<B256>
.
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
Source§impl<T> Compact for Vec<T>where
T: Compact,
impl<T> Compact for Vec<T>where
T: Compact,
Source§fn to_compact<B>(&self, buf: &mut B) -> usize
fn to_compact<B>(&self, buf: &mut B) -> usize
Returns 0 since we won’t include it in the StructFlags
.
Source§fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
fn specialized_to_compact<B>(&self, buf: &mut B) -> usize
To be used by fixed sized types like Vec<B256>
.
Source§fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
fn specialized_from_compact(buf: &[u8], len: usize) -> (Self, &[u8])
To be used by fixed sized types like Vec<B256>
.
fn from_compact(buf: &[u8], _: usize) -> (Self, &[u8])
Source§impl<T: Compact + ToOwned<Owned = T>> Compact for Cow<'_, T>
impl<T: Compact + ToOwned<Owned = T>> Compact for Cow<'_, T>
Implementors§
impl Compact for reth_codecs::alloy::authorization_list::Authorization
test-utils
only.impl Compact for reth_codecs::alloy::genesis_account::GenesisAccount
test-utils
only.impl Compact for reth_codecs::alloy::header::Header
test-utils
only.impl Compact for HeaderExt
test-utils
only.impl Compact for reth_codecs::alloy::transaction::eip1559::TxEip1559
test-utils
only.impl Compact for reth_codecs::alloy::transaction::eip2930::TxEip2930
test-utils
only.impl Compact for reth_codecs::alloy::transaction::eip4844::TxEip4844
test-utils
only.impl Compact for reth_codecs::alloy::transaction::eip7702::TxEip7702
test-utils
only.impl Compact for reth_codecs::alloy::transaction::legacy::TxLegacy
test-utils
only.impl Compact for reth_codecs::alloy::transaction::optimism::TxDeposit
test-utils
and op
only.impl Compact for reth_codecs::alloy::withdrawal::Withdrawal
test-utils
only.