Skip to content

Commit

Permalink
Add example for libafl_derive::Display
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcondiro committed Jul 8, 2024
1 parent 179da1b commit 5b10934
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions libafl_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,44 @@ pub fn libafl_serdeany_derive(input: TokenStream) -> TokenStream {
/// Options: Some => inner type display None => "".
/// Vec: inner type display space separated concatenation.
/// Generics and other more or less exotic stuff are not supported.
///
/// # Examples
///
/// ```rust
/// use libafl_derive;
///
/// #[derive(libafl_derive::Display)]
/// struct MyStruct {
/// foo: String,
/// bar: Option<u32>,
/// }
/// ```
///
/// The above code will expand to:
///
/// ```rust
/// struct MyStruct {
/// foo: String,
/// bar: Option<u32>,
/// }
///
/// impl core::fmt::Display for MyStruct {
/// fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
/// f.write_fmt(format_args!(" {0}", self.foo))?;
/// if let Some(opt) = &self.bar {
/// f.write_fmt(format_args!(" {0}", opt))?;
/// }
/// Ok(())
/// }
/// }
/// ```
#[proc_macro_derive(Display)]
pub fn libafl_display(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input as DeriveInput);

if let Struct(s) = data {
if let Named(fields) = s.fields {
let fields_fmt = fields
.named
.iter()
.map(libafl_display_field_by_type);
let fields_fmt = fields.named.iter().map(libafl_display_field_by_type);

return quote! {
impl core::fmt::Display for #ident {
Expand Down

0 comments on commit 5b10934

Please sign in to comment.