Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generated Entity Attributes

At a times there might be a need to annotate the entities, generated by FieldX, with additional attributes. Say, to silence down check warnings (#[allow(dead_code)]), derive additional traits, or even use some home brew procedural macros. To do so FieldX provides three arguments:

  • attributes - for generated structs
  • attributes_fn - for generated methods
  • attributes_impl - for generated implementations

Say, I've got a crazy idea of having my builder type clonable. Voila!

use fieldx::fxstruct;

#[fxstruct(
    get,
    builder(
        attributes(
            derive(Clone, PartialEq, Eq, Debug)
        ),
    )
)]
struct Book {
    #[fieldx(into)]
    title:  String,
    #[fieldx(into)]
    author: String,
    year:   u32,
    #[fieldx(optional)]
    signed_by: String,
    #[fieldx(
        set("place_into"),
        inner_mut,
        default("unknown".to_string())
    )]
    location: String,
}

let book_builder = Book::builder()
    .title("The Hitchhiker's Guide to the Galaxy")
    .author("Douglas Adams")
    .year(1979);

let book_builder_clone = book_builder.clone();

assert_eq!(book_builder_clone, book_builder);

Not all arguments that support this feature support all three sub-arguments though. For example, the helper arguments only know about the attributes_fn since they work with methods. It is better to consule with the technical documentation of the fxstruct macro.