GetSet Procedural Macro

The GetSet procedural macro simplifies the creation of getter and setter methods for fields in your Rust structs. With this macro, you can generate these methods automatically, reducing boilerplate code and enhancing code readability.

Table of Contents

Usage

To use the GetSet procedural macro, follow these steps:

  1. Add the GetSet crate to your Cargo.toml:

    toml [dependencies] getset-macro = "0.1"

  2. Import the GetSet procedural macro into your Rust code: rust use getset_macro::GetSet;

  3. Apply the #[derive(GetSet)] attribute to your struct. This will automatically generate getter and setter methods for all the struct's fields. ```rust

    [derive(GetSet)]

    struct MyStruct {
        field1: FieldType1,
        field2: FieldType2,
        // ... more fields
    }
    

    ```

  4. Use the generated getter and setter methods as follows:

    ```rust let mut instance = MyStruct { field1: initialvalue1, field2: initialvalue2, };

    // Get the value of field1
    let value1 = instance.get_field1();
    
    // Set the value of field2
    instance.set_field2(new_value2);
    

    ```

Example

Here's an example of how to use the GetSet procedural macro: ```rust use getset_macro::GetSet;

[derive(GetSet)]

struct Person { name: String, age: u32, }

fn main() { let mut person = Person { name: "Alice".to_string(), age: 30, };

// Get the name and age
let name = person.get_name();
let age = person.get_age();

// Modify the age
person.set_age(25);

}

``` In this example, the GetSet macro generates getname(), getage(), setname(), and setage() methods for the Person struct's fields.

How It Works The GetSet macro takes care of the following tasks:

It generates getter methods (getfieldname()) for all fields in the struct. It generates setter methods (setfieldname(value)) for all fields in the struct. The generated getter methods return a reference to the field's value. The generated setter methods allow you to set the field's value. The macro analyzes the struct's fields and automatically generates code for these methods, reducing manual code writing.