diff --git a/crates/storage/codecs/Cargo.toml b/crates/storage/codecs/Cargo.toml index c3210b21ae2c..76a3721629ae 100644 --- a/crates/storage/codecs/Cargo.toml +++ b/crates/storage/codecs/Cargo.toml @@ -26,7 +26,7 @@ op-alloy-consensus = { workspace = true, optional = true } # misc bytes.workspace = true -modular-bitfield = { workspace = true, optional = true } +modular-bitfield.workspace = true visibility = { version = "0.1.1", optional = true} serde.workspace = true arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -65,7 +65,6 @@ alloy = [ "dep:alloy-consensus", "dep:alloy-eips", "dep:alloy-genesis", - "dep:modular-bitfield", "dep:alloy-trie", ] op = ["alloy", "dep:op-alloy-consensus"] diff --git a/crates/storage/codecs/derive/src/compact/flags.rs b/crates/storage/codecs/derive/src/compact/flags.rs index 3242a611eb39..798c9ad53b45 100644 --- a/crates/storage/codecs/derive/src/compact/flags.rs +++ b/crates/storage/codecs/derive/src/compact/flags.rs @@ -1,9 +1,11 @@ use super::*; +use syn::Attribute; /// Generates the flag fieldset struct that is going to be used to store the length of fields and /// their potential presence. pub(crate) fn generate_flag_struct( ident: &Ident, + attrs: &[Attribute], has_lifetime: bool, fields: &FieldList, is_zstd: bool, @@ -13,6 +15,8 @@ pub(crate) fn generate_flag_struct( let flags_ident = format_ident!("{ident}Flags"); let mod_flags_ident = format_ident!("{ident}_flags"); + let reth_codecs = parse_reth_codecs_path(attrs).unwrap(); + let mut field_flags = vec![]; let total_bits = if is_enum { @@ -88,8 +92,9 @@ pub(crate) fn generate_flag_struct( pub use #mod_flags_ident::#flags_ident; #[allow(non_snake_case)] mod #mod_flags_ident { - use bytes::Buf; - use modular_bitfield::prelude::*; + use #reth_codecs::__private::Buf; + use #reth_codecs::__private::modular_bitfield; + use #reth_codecs::__private::modular_bitfield::prelude::*; #[doc = #docs] #[bitfield] diff --git a/crates/storage/codecs/derive/src/compact/generator.rs b/crates/storage/codecs/derive/src/compact/generator.rs index cf9bcc0c6295..63fef05ad705 100644 --- a/crates/storage/codecs/derive/src/compact/generator.rs +++ b/crates/storage/codecs/derive/src/compact/generator.rs @@ -239,7 +239,7 @@ fn generate_to_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> Vec< } /// Function to extract the crate path from `reth_codecs(crate = "...")` attribute. -fn parse_reth_codecs_path(attrs: &[Attribute]) -> syn::Result { +pub(crate) fn parse_reth_codecs_path(attrs: &[Attribute]) -> syn::Result { // let default_crate_path: syn::Path = syn::parse_str("reth-codecs").unwrap(); let mut reth_codecs_path: syn::Path = syn::parse_quote!(reth_codecs); for attr in attrs { diff --git a/crates/storage/codecs/derive/src/compact/mod.rs b/crates/storage/codecs/derive/src/compact/mod.rs index b9d5cf18d6b7..1c1723d2ec94 100644 --- a/crates/storage/codecs/derive/src/compact/mod.rs +++ b/crates/storage/codecs/derive/src/compact/mod.rs @@ -48,7 +48,7 @@ pub fn derive(input: TokenStream, is_zstd: bool) -> TokenStream { let has_lifetime = has_lifetime(&generics); let fields = get_fields(&data); - output.extend(generate_flag_struct(&ident, has_lifetime, &fields, is_zstd)); + output.extend(generate_flag_struct(&ident, &attrs, has_lifetime, &fields, is_zstd)); output.extend(generate_from_to(&ident, &attrs, has_lifetime, &fields, is_zstd)); output.into() } @@ -235,7 +235,7 @@ mod tests { let mut output = quote! {}; let DeriveInput { ident, data, attrs, .. } = parse2(f_struct).unwrap(); let fields = get_fields(&data); - output.extend(generate_flag_struct(&ident, false, &fields, false)); + output.extend(generate_flag_struct(&ident, &attrs, false, &fields, false)); output.extend(generate_from_to(&ident, &attrs, false, &fields, false)); // Expected output in a TokenStream format. Commas matter! @@ -255,8 +255,9 @@ mod tests { #[allow(non_snake_case)] mod TestStruct_flags { - use bytes::Buf; - use modular_bitfield::prelude::*; + use reth_codecs::__private::Buf; + use reth_codecs::__private::modular_bitfield; + use reth_codecs::__private::modular_bitfield::prelude::*; #[doc = "Fieldset that facilitates compacting the parent type. Used bytes: 2 | Unused bits: 1"] #[bitfield] #[derive(Clone, Copy, Debug, Default)] diff --git a/crates/storage/codecs/src/lib.rs b/crates/storage/codecs/src/lib.rs index 86d397ad24f0..8c6ba5e4c766 100644 --- a/crates/storage/codecs/src/lib.rs +++ b/crates/storage/codecs/src/lib.rs @@ -39,6 +39,11 @@ pub mod txtype; #[cfg(any(test, feature = "test-utils"))] pub mod test_utils; +// Used by generated code and doc tests. Not public API. +#[doc(hidden)] +#[path = "private.rs"] +pub mod __private; + /// Trait that implements the `Compact` codec. /// /// When deriving the trait for custom structs, be aware of certain limitations/recommendations: diff --git a/crates/storage/codecs/src/private.rs b/crates/storage/codecs/src/private.rs new file mode 100644 index 000000000000..6f54d9c9ca82 --- /dev/null +++ b/crates/storage/codecs/src/private.rs @@ -0,0 +1,3 @@ +pub use modular_bitfield; + +pub use bytes::Buf;