diff --git a/crates/codecs/derive/src/compact/flags.rs b/crates/codecs/derive/src/compact/flags.rs index 8868236918cd..2cc1519d8885 100644 --- a/crates/codecs/derive/src/compact/flags.rs +++ b/crates/codecs/derive/src/compact/flags.rs @@ -6,11 +6,13 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt let is_enum = fields.iter().any(|field| matches!(field, FieldTypes::EnumVariant(_))); let flags_ident = format_ident!("{ident}Flags"); + let mod_flags_ident = format_ident!("{ident}_flags"); + let mut field_flags = vec![]; let total_bits = if is_enum { field_flags.push(quote! { - variant: B8, + pub variant: B8, }); 8 } else { @@ -44,17 +46,26 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt // Generate the flag struct. quote! { - #[bitfield] - #[derive(Clone, Copy, Debug, Default)] - struct #flags_ident { - #(#field_flags)* - } - impl #flags_ident { - fn from(mut buf: &[u8]) -> (Self, &[u8]) { - (#flags_ident::from_bytes([ - #(#readable_bytes)* - ]), buf) + pub use #mod_flags_ident::#flags_ident; + mod #mod_flags_ident { + use bytes::Buf; + use modular_bitfield::prelude::*; + + /// Fieldset that facilitates compacting the parent type. + #[bitfield] + #[derive(Clone, Copy, Debug, Default)] + pub struct #flags_ident { + #(#field_flags)* + } + + impl #flags_ident { + /// Deserializes this fieldset and returns it, alongside the original slice in an advanced position. + pub fn from(mut buf: &[u8]) -> (Self, &[u8]) { + (#flags_ident::from_bytes([ + #(#readable_bytes)* + ]), buf) + } } } } @@ -82,13 +93,13 @@ fn build_struct_field_flags( total_bits += bitsize; field_flags.push(quote! { - #name: #bsize , + pub #name: #bsize , }); } else { let name = format_ident!("{name}"); field_flags.push(quote! { - #name: bool , + pub #name: bool , }); total_bits += 1; @@ -120,15 +131,18 @@ fn pad_flag_struct(total_bits: u8, field_flags: &mut Vec) -> u8 { /// Placeholder struct for when there are no bitfields to be added. fn placeholder_flag_struct(flags: &Ident) -> TokenStream2 { quote! { + /// Placeholder struct for when there is no need for a fieldset. Doesn't actually write or read any data. #[derive(Debug, Default)] - struct #flags { + pub struct #flags { } impl #flags { - fn from(mut buf: &[u8]) -> (Self, &[u8]) { + /// Placeholder: does not read any value. + pub fn from(mut buf: &[u8]) -> (Self, &[u8]) { (#flags::default(), buf) } - fn into_bytes(self) -> [u8; 0] { + /// Placeholder: returns an empty array. + pub fn into_bytes(self) -> [u8; 0] { [] } } diff --git a/crates/codecs/derive/src/compact/mod.rs b/crates/codecs/derive/src/compact/mod.rs index 716efee6f1ae..7a07c881ec8e 100644 --- a/crates/codecs/derive/src/compact/mod.rs +++ b/crates/codecs/derive/src/compact/mod.rs @@ -205,25 +205,33 @@ mod tests { // Expected output in a TokenStream format. Commas matter! let should_output = quote! { - #[bitfield] - #[derive(Clone, Copy, Debug, Default)] - struct TestStructFlags { - f_u64_len: B4, - f_u256_len: B6, - f_bool_t_len: B1, - f_bool_f_len: B1, - f_option_none_len: B1, - f_option_some_len: B1, - f_option_some_u64_len: B1, - #[skip] - unused: B1, - } - impl TestStructFlags { - fn from(mut buf: &[u8]) -> (Self, &[u8]) { - ( - TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]), - buf - ) + pub use TestStruct_flags::TestStructFlags; + mod TestStruct_flags { + use bytes::Buf; + use modular_bitfield::prelude::*; + + #[doc=r" Fieldset that facilitates compacting the parent type."] + #[bitfield] + #[derive(Clone, Copy, Debug, Default)] + pub struct TestStructFlags { + pub f_u64_len: B4, + pub f_u256_len: B6, + pub f_bool_t_len: B1, + pub f_bool_f_len: B1, + pub f_option_none_len: B1, + pub f_option_some_len: B1, + pub f_option_some_u64_len: B1, + #[skip] + unused: B1, + } + impl TestStructFlags { + #[doc=r" Deserializes this fieldset and returns it, alongside the original slice in an advanced position."] + pub fn from(mut buf: &[u8]) -> (Self, &[u8]) { + ( + TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]), + buf + ) + } } } #[cfg(test)] diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs index 4a867dac76a0..6deb90a838d0 100644 --- a/crates/codecs/src/lib.rs +++ b/crates/codecs/src/lib.rs @@ -282,7 +282,6 @@ impl Compact for bool { mod tests { use super::*; use ethers_core::types::Address; - use modular_bitfield::prelude::*; #[test] fn compact_bytes() { diff --git a/crates/interfaces/src/db/codecs/compact.rs b/crates/interfaces/src/db/codecs/compact.rs index fec2d3a8b9b2..ec77dd18598c 100644 --- a/crates/interfaces/src/db/codecs/compact.rs +++ b/crates/interfaces/src/db/codecs/compact.rs @@ -2,8 +2,6 @@ use crate::db::{ models::{accounts::AccountBeforeTx, StoredBlockBody}, Compress, Decompress, Error, }; -use bytes::Buf; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; use reth_primitives::*; diff --git a/crates/interfaces/src/db/models/blocks.rs b/crates/interfaces/src/db/models/blocks.rs index c365479b5a87..493fd6864a24 100644 --- a/crates/interfaces/src/db/models/blocks.rs +++ b/crates/interfaces/src/db/models/blocks.rs @@ -7,8 +7,7 @@ use crate::{ }, impl_fixed_arbitrary, }; -use bytes::{Buf, Bytes}; -use modular_bitfield::prelude::*; +use bytes::Bytes; use reth_codecs::{main_codec, Compact}; use reth_primitives::{BlockHash, BlockNumber, Header, TxNumber, H256}; use serde::{Deserialize, Serialize}; diff --git a/crates/primitives/src/account.rs b/crates/primitives/src/account.rs index 20062d1c4d82..e14c8d4a4b0c 100644 --- a/crates/primitives/src/account.rs +++ b/crates/primitives/src/account.rs @@ -1,6 +1,4 @@ use crate::{H256, U256}; -use bytes::Buf; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; /// Account saved in database diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index 41da2358d9a4..c9acdf7f6070 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -2,9 +2,8 @@ use crate::{ proofs::{EMPTY_LIST_HASH, EMPTY_ROOT}, BlockHash, BlockNumber, Bloom, H160, H256, U256, }; -use bytes::{Buf, BufMut, BytesMut}; +use bytes::{BufMut, BytesMut}; use ethers_core::{types::H64, utils::keccak256}; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, Encodable}; use std::ops::Deref; diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 726a63792166..487263bf38fc 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -1,6 +1,5 @@ use crate::{Bloom, Log, TxType}; use bytes::{Buf, BufMut, BytesMut}; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, Encodable}; use std::cmp::Ordering; diff --git a/crates/primitives/src/storage.rs b/crates/primitives/src/storage.rs index 7f188afbdf63..26b29aff16f0 100644 --- a/crates/primitives/src/storage.rs +++ b/crates/primitives/src/storage.rs @@ -1,6 +1,4 @@ use super::{H256, U256}; -use bytes::Buf; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; /// Account storage entry. diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 5238313cc9e2..ffca1fe75081 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -3,7 +3,6 @@ pub use access_list::{AccessList, AccessListItem}; use bytes::{Buf, BytesMut}; use derive_more::{AsRef, Deref}; use ethers_core::utils::keccak256; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_STRING_CODE}; pub use signature::Signature; diff --git a/crates/primitives/src/transaction/signature.rs b/crates/primitives/src/transaction/signature.rs index 68ffd6639e35..ac6e2240164d 100644 --- a/crates/primitives/src/transaction/signature.rs +++ b/crates/primitives/src/transaction/signature.rs @@ -1,6 +1,4 @@ use crate::{transaction::util::secp256k1, Address, H256, U256}; -use bytes::Buf; -use modular_bitfield::prelude::*; use reth_codecs::{main_codec, Compact}; use reth_rlp::{Decodable, DecodeError, Encodable};