-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: improve CompactZstd
macro
#13277
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -20,6 +20,12 @@ use syn::{ | |||
mod arbitrary; | ||||
mod compact; | ||||
|
||||
#[derive(Clone)] | ||||
pub(crate) struct ZstdConfig { | ||||
compressor: syn::Path, | ||||
decompressor: syn::Path, | ||||
} | ||||
|
||||
/// Derives the `Compact` trait for custom structs, optimizing serialization with a possible | ||||
/// bitflag struct. | ||||
/// | ||||
|
@@ -51,15 +57,49 @@ mod compact; | |||
/// efficient decoding. | ||||
#[proc_macro_derive(Compact, attributes(maybe_zero, reth_codecs))] | ||||
pub fn derive(input: TokenStream) -> TokenStream { | ||||
let is_zstd = false; | ||||
compact::derive(input, is_zstd) | ||||
compact::derive(input, None) | ||||
} | ||||
|
||||
/// Adds `zstd` compression to derived [`Compact`]. | ||||
#[proc_macro_derive(CompactZstd, attributes(maybe_zero, reth_codecs))] | ||||
#[proc_macro_derive(CompactZstd, attributes(maybe_zero, reth_codecs, reth_zstd))] | ||||
pub fn derive_zstd(input: TokenStream) -> TokenStream { | ||||
let is_zstd = true; | ||||
compact::derive(input, is_zstd) | ||||
let derive_input = { | ||||
let input = input.clone(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if we need to do this here or if we can do this
and get rid of the clone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||
parse_macro_input!(input as DeriveInput) | ||||
}; | ||||
|
||||
let mut compressor = None; | ||||
let mut decompressor = None; | ||||
|
||||
for attr in derive_input.attrs { | ||||
if attr.path().is_ident("reth_zstd") { | ||||
if let Err(err) = attr.parse_nested_meta(|meta| { | ||||
if meta.path.is_ident("compressor") { | ||||
let value = meta.value()?; | ||||
let path: syn::Path = value.parse()?; | ||||
compressor = Some(path); | ||||
} else if meta.path.is_ident("decompressor") { | ||||
let value = meta.value()?; | ||||
let path: syn::Path = value.parse()?; | ||||
decompressor = Some(path); | ||||
} else { | ||||
return Err(meta.error("unsupported attribute")) | ||||
} | ||||
Ok(()) | ||||
}) { | ||||
return err.to_compile_error().into() | ||||
} | ||||
} | ||||
} | ||||
|
||||
let (Some(compressor), Some(decompressor)) = (compressor, decompressor) else { | ||||
return quote! { | ||||
compile_error!("missing compressor or decompressor attribute"); | ||||
} | ||||
.into() | ||||
}; | ||||
|
||||
compact::derive(input, Some(ZstdConfig { compressor, decompressor })) | ||||
} | ||||
|
||||
/// Generates tests for given type. | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub use modular_bitfield; | ||
|
||
pub use bytes::Buf; | ||
pub use bytes::{self, Buf}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding reth_zstd makes sense, this a lot smarter than adding it to reth_codecs because then we don't run into the issue I had on my pr