diff --git a/crates/storage/codecs/derive/src/compact/generator.rs b/crates/storage/codecs/derive/src/compact/generator.rs index 03dab1a144c4..c28bf8d1a4fb 100644 --- a/crates/storage/codecs/derive/src/compact/generator.rs +++ b/crates/storage/codecs/derive/src/compact/generator.rs @@ -52,7 +52,8 @@ pub fn generate_from_to(ident: &Ident, fields: &FieldList, is_zstd: bool) -> Tok /// Generates code to implement the `Compact` trait method `to_compact`. fn generate_from_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> TokenStream2 { let mut lines = vec![]; - let mut known_types = vec!["B256", "Address", "Bloom", "Vec", "TxHash", "BlockHash"]; + let mut known_types = + vec!["B256", "Address", "Bloom", "Vec", "TxHash", "BlockHash", "FixedBytes"]; // Only types without `Bytes` should be added here. It's currently manually added, since // it's hard to figure out with derive_macro which types have Bytes fields. diff --git a/crates/storage/codecs/src/lib.rs b/crates/storage/codecs/src/lib.rs index 9c5d757b9fde..907fee440d8e 100644 --- a/crates/storage/codecs/src/lib.rs +++ b/crates/storage/codecs/src/lib.rs @@ -17,7 +17,7 @@ pub use reth_codecs_derive::*; -use alloy_primitives::{Address, Bloom, Bytes, B256, B512, U256}; +use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U256}; use bytes::Buf; #[cfg(any(test, feature = "alloy"))] @@ -301,9 +301,9 @@ impl Compact for [u8; N] { } } -/// Implements the [`Compact`] trait for fixed size byte array types like [`B256`]. +/// Implements the [`Compact`] trait for wrappers over fixed size byte array types. #[macro_export] -macro_rules! impl_compact_for_bytes { +macro_rules! impl_compact_for_wrapped_bytes { ($($name:tt),+) => { $( impl Compact for $name { @@ -324,8 +324,23 @@ macro_rules! impl_compact_for_bytes { )+ }; } +impl_compact_for_wrapped_bytes!(Address, Bloom); -impl_compact_for_bytes!(Address, B256, B512, Bloom); +impl Compact for FixedBytes { + #[inline] + fn to_compact(self, buf: &mut B) -> usize + where + B: bytes::BufMut + AsMut<[u8]>, + { + self.0.to_compact(buf) + } + + #[inline] + fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) { + let (v, buf) = <[u8; N]>::from_compact(buf, len); + (Self::from(v), buf) + } +} impl Compact for bool { /// `bool` vars go directly to the `StructFlags` and are not written to the buffer. @@ -378,6 +393,7 @@ const fn decode_varuint_panic() -> ! { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::B256; #[test] fn compact_bytes() {