Refactor: use fully-qualified paths in Compact derives(Deon Branch) (#12279)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Deon
2024-11-03 18:03:45 +01:00
committed by GitHub
parent 4e3b32c5af
commit 61f19ab2dc
18 changed files with 87 additions and 34 deletions

View File

@ -18,10 +18,26 @@ pub fn maybe_generate_tests(
let mut traits = vec![];
let mut roundtrips = vec![];
let mut additional_tests = vec![];
let mut is_crate = false;
for arg in args {
let mut iter = args.into_iter().peekable();
// we check if there's a crate argument which is used from inside the codecs crate directly
if let Some(arg) = iter.peek() {
if arg.to_string() == "crate" {
is_crate = true;
iter.next();
}
}
for arg in iter {
if arg.to_string() == "compact" {
traits.push(quote! { use super::Compact; });
let path = if is_crate {
quote! { use crate::Compact; }
} else {
quote! { use reth_codecs::Compact; }
};
traits.push(path);
roundtrips.push(quote! {
{
let mut buf = vec![];

View File

@ -2,10 +2,12 @@
use super::*;
use convert_case::{Case, Casing};
use syn::{Attribute, LitStr};
/// Generates code to implement the `Compact` trait for a data type.
pub fn generate_from_to(
ident: &Ident,
attrs: &[Attribute],
has_lifetime: bool,
fields: &FieldList,
is_zstd: bool,
@ -20,6 +22,8 @@ pub fn generate_from_to(
let fuzz = format_ident!("fuzz_test_{snake_case_ident}");
let test = format_ident!("fuzz_{snake_case_ident}");
let reth_codecs = parse_reth_codecs_path(attrs).unwrap();
let lifetime = if has_lifetime {
quote! { 'a }
} else {
@ -28,11 +32,11 @@ pub fn generate_from_to(
let impl_compact = if has_lifetime {
quote! {
impl<#lifetime> Compact for #ident<#lifetime>
impl<#lifetime> #reth_codecs::Compact for #ident<#lifetime>
}
} else {
quote! {
impl Compact for #ident
impl #reth_codecs::Compact for #ident
}
};
@ -53,6 +57,7 @@ pub fn generate_from_to(
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn #fuzz(obj: #ident) {
use #reth_codecs::Compact;
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len);
@ -191,7 +196,7 @@ fn generate_to_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> Vec<
}
// Just because a type supports compression, doesn't mean all its values are to be compressed.
// We skip the smaller ones, and thus require a flag `__zstd` to specify if this value is
// We skip the smaller ones, and thus require a flag` __zstd` to specify if this value is
// compressed or not.
if is_zstd {
lines.push(quote! {
@ -232,3 +237,25 @@ fn generate_to_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> Vec<
lines
}
/// Function to extract the crate path from `reth_codecs(crate = "...")` attribute.
fn parse_reth_codecs_path(attrs: &[Attribute]) -> syn::Result<syn::Path> {
// 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 {
if attr.path().is_ident("reth_codecs") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("crate") {
let value = meta.value()?;
let lit: LitStr = value.parse()?;
reth_codecs_path = syn::parse_str(&lit.value())?;
Ok(())
} else {
Err(meta.error("unsupported attribute"))
}
})?;
}
}
Ok(reth_codecs_path)
}

View File

@ -43,13 +43,13 @@ pub enum FieldTypes {
pub fn derive(input: TokenStream, is_zstd: bool) -> TokenStream {
let mut output = quote! {};
let DeriveInput { ident, data, generics, .. } = parse_macro_input!(input);
let DeriveInput { ident, data, generics, attrs, .. } = parse_macro_input!(input);
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_from_to(&ident, has_lifetime, &fields, is_zstd));
output.extend(generate_from_to(&ident, &attrs, has_lifetime, &fields, is_zstd));
output.into()
}
@ -233,10 +233,10 @@ mod tests {
// Generate code that will impl the `Compact` trait.
let mut output = quote! {};
let DeriveInput { ident, data, .. } = parse2(f_struct).unwrap();
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_from_to(&ident, false, &fields, false));
output.extend(generate_from_to(&ident, &attrs, false, &fields, false));
// Expected output in a TokenStream format. Commas matter!
let should_output = quote! {
@ -285,6 +285,7 @@ mod tests {
#[allow(dead_code)]
#[test_fuzz::test_fuzz]
fn fuzz_test_test_struct(obj: TestStruct) {
use reth_codecs::Compact;
let mut buf = vec![];
let len = obj.clone().to_compact(&mut buf);
let (same_obj, buf) = TestStruct::from_compact(buf.as_ref(), len);
@ -295,7 +296,7 @@ mod tests {
pub fn fuzz_test_struct() {
fuzz_test_test_struct(TestStruct::default())
}
impl Compact for TestStruct {
impl reth_codecs::Compact for TestStruct {
fn to_compact<B>(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> {
let mut flags = TestStructFlags::default();
let mut total_length = 0;

View File

@ -49,14 +49,14 @@ mod compact;
/// own encoding and do not rely on the bitflag struct.
/// - `Bytes` fields and any types containing a `Bytes` field should be placed last to ensure
/// efficient decoding.
#[proc_macro_derive(Compact, attributes(maybe_zero))]
#[proc_macro_derive(Compact, attributes(maybe_zero, reth_codecs))]
pub fn derive(input: TokenStream) -> TokenStream {
let is_zstd = false;
compact::derive(input, is_zstd)
}
/// Adds `zstd` compression to derived [`Compact`].
#[proc_macro_derive(CompactZstd, attributes(maybe_zero))]
#[proc_macro_derive(CompactZstd, attributes(maybe_zero, reth_codecs))]
pub fn derive_zstd(input: TokenStream) -> TokenStream {
let is_zstd = true;
compact::derive(input, is_zstd)