Derive Getters
Simple boilerplate library for getters.
The need for this library came about when I was making various data structures for JSON to deserialize into. These data structures had many fields in them and weren't going to change once created. Of course one could just use pub
everywhere but that would enable mutating the fields which is what this proc macro crate aims to avoid.
Getters will be generated according to convention. This means that the generated methods will reside within the struct namespace.
What this crate won't do
There are no mutable getters and it's not planned. There are no setters either nor will there ever be.
Rust Docs
Installation
Add to your Cargo.toml
:
[]
= "0.0.9"
Then import the Getters
macro in whichever module it's needed (assuming 2018 edition).
use Getters;
Otherwise just import at crate root.
extern crate derive_getters;
Usage
When you have a struct you want to automatically derive getters for... Just add the derive at the top like so;
A new impl will be produced for MyCheesyStruct
.
This crate can also handle structs with simple generic parameters and lifetime annotations. Check docs for further details.
Caveats
- Will not work on unit structs, tuples or enums. Derive
Getters
over them and the code will chuck a wobbly. - All getter methods return a
&
immutable reference to their field. This means for some types it can get awkward.