Crate get_set_macro

Source
Expand description

§get_set_macro

A procedural macro to generate customizable getters and setters for your Rust structs.

§Installation

Run cargo add get_set_macro

§Example Usage

use get_set_macro::get_set;

// By default, each field in `Example` will recieve a getter with the #[inline(always)] attribute.
// Here the `default` is applied to the `get`, so the global get will be #[inline(always)].
#[get_set(default(inline_always, vis = "pub"), get)]
struct Example {
    // This field will not recieve the default getter.
    #[gsflags(skip)]
    skipped: u8,

    // Despite only having the `set` flag, this member will recieve both a getter and a setter,
    // each with pub visibility and each being #[inline(always)]
    #[gsflags(set)]
    name: String,
     
    // Since u32's are trivially copyable, 
    // there is no need to pass this value by reference and so instead we will pass it by value.
    // This flag has also inhereted the default `inline_always`, so this `get_copy` will be #[inline(always)].
    // If we wanted to override the defaults, adding `default(noinline, vis = "")` anywhere 
    // in the gsflags body would change the defaults to having no inline attribute and inherited (private) visibility.
    // If we didn't want this to override the default get(_ref), we could change 
    // `get_copy` to `get_copy(rename = "get_age_copy")`,
    // which would simply create a new method that returned a copy.
    #[gsflags(get_copy)]
    age: u32,
}
 
// Has functionality
fn main() {
    let mut example = Example {
        skipped: 8,
        name: "ExampleName".to_string(),
        age: 69,
    };
 
    // The following would error as there is no method named `get_skipped`
    // assert_eq!(8, example.get_skipped());
 
    // The getters and setters of `name`
    assert_eq!("ExampleName".to_string(), *example.get_name());
    example.set_name("NewName".to_string());
    assert_eq!("NewName".to_string(), *example.get_name());
 
    assert_eq!(69, example.get_age());
}

§See Also

Attribute Macros§

get_set