Coder Social home page Coder Social logo

prk-jr / getset-macro Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 5 KB

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.

Rust 100.00%

getset-macro's Introduction

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:

    [dependencies]
    getset-macro = "0.1"
  2. Import the GetSet procedural macro into your Rust code:

    use getset_macro::GetSet;
  3. Apply the #[derive(GetSet)] attribute to your struct. This will automatically generate constructor, getter and setter methods for all the struct's fields.

    #[derive(GetSet)]
        struct MyStruct {
            field1: FieldType1,
            field2: FieldType2,
            // ... more fields
        }
  4. Use the generated constrotor, getter and setter methods as follows:

        let mut instance = MyStruct {
                field1: initial_value1,
                field2: initial_value2,
                };
    
        // Or Using derived constructor
        let mut new_instance = MyStruct::new(field1, field2);
    
        // 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:

use getset_macro::GetSet;

#[derive(GetSet)]
struct Person {
    name: String,
    age: u32,
}

fn main() {
    // Using constructor API
    let mut person = Person::new("Alice".to_string(), 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 new(), get_name(), get_age(), set_name(), and set_age() methods for the Person struct's fields.

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

It generates getter methods (get_fieldname()) for all fields in the struct. It generates setter methods (set_fieldname(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.

getset-macro's People

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.