get_set_macro
Procedural macro to automatically generate getters and setters for struct fields in Rust, with fine-grained control over behavior.
Features
Syntactically efficient way of creating getters and/or setters with lots of customizability (see Attributes).
Installation
Run cargo add get_set_macro.
Usage
Import the macro:
use get_set;
Apply it to a struct: (see tests/ui/ok_readme.rs)
// Has functionality
Attributes
| Attribute | Description |
|---|---|
#[get] |
Generate a getter that returns a reference. |
#[get_copy] |
Generate a getter that returns a copy. (Use only with Copy types.) |
#[set] |
Generate a setter that sets a new value. |
rename = "..." |
Customize the method name (e.g., #[gsflags(get(rename = "fetch_{name}"))]). |
inline(|_always|_never) |
Choose to have a getter or setter inlined and how (e.g. #[gsflags(get(inline_always, rename = "always_inlined_get"))]). |
#[set_get(struct-wide settings)] |
Applies get/set settings to all fields in the struct (ignores rename). If default is used here, it will be the default to every gsflags attribute in the struct. |
skip |
Skip struct-wide gs-settings for this field. |
vis = "..." |
Change the visibility of generated getters/setters. |
default(...) |
Applies get/set settings (except rename) to each getter/setter in this gsflags attribute |
Note: Only structs with named fields are currently supported.
Limitations
- Only named fields (
struct Foo { x: T }) are supported — tuple structs are not yet supported.
Planned Features
accessorshorthand. e.g.#[gsflags(accessor(...))]would be the same as#[gsflags(get(...), set(...))](ignoringrename).defaulttag changing the default behaviour. e.g.#[get_set(default(...))]or#[gsflags(default(...))]would changegetintoget(...)(ignoringrename).
Contributing
Pull requests, issues, and suggestions are welcome!
If you find a bug or would like to request a feature, feel free to open an issue.
License
This project is licensed under the MIT License.
Acknowledgments
I did this.