Attribute Macro encapsulated

Source
#[encapsulated]
Expand description

Java style encapsulation for struct fields

§Example

#[encapsulated]
pub struct Example {
    // make the getter method copy the value instead
    // returning a reference
    #[getter(copy_val)]
    #[setter]
    a: usize,

    // return a mutable reference
    #[getter(mutable, doc = "Your documentation here")]
    // create a setter method with an Into impl
    #[setter(use_into_impl)]
    b: f64,
    c: f32,

    #[setter]
    // it also possible to create chainable methods with this macro
    // (including with documentation)
    #[chainable(doc = "Documentation for `field_0`")]
    chainable: u32,

    #[setter]
    #[chainable(collapse_option)]
    thing1: Option<u32>,

    #[setter]
    #[chainable(collapse_option, use_into_impl)]
    thing2: Option<usize>
}

§Method Visibility

Methods generated by this macro have the same visibility as the annotated struct (if the struct is pub, so will the generated methods)

§Helper attributes:

§#[setter]

Create a setter method for the annotated field

§Options:

use_into_impl: Make the generated method take an [Into] implementation of the field type and convert it

doc = "Your documentation here": Creates documentation for the generated setter method if #[chainable] is also specified for this field without documentation, the documentation from this attribute is used for the chainable method

§#[getter]

Create a setter method for the annotated field

§Options:

copy_val: copy the field value instead of returning a reference. This only works if the field’s type implements [Copy]!

mutable: Create a getter method that returns a mutable reference to the field

doc = "Your documentation here": Creates documentation for the generated setter method

§#[chainable]

Works exactly like the chainable macro on struct fields