Derive Getters
Simple Getters
derive macro for generating field getter methods on a named struct. Included is an additional derive, Dissolve
, that consumes the struct returning a tuple of all fields in the order they were declared. Dissolve can also be derived for tuple structs. Any doc comments on the target struct fields are replicated for the corresponding getters. If no comment is present one shall be generated.
The need for the Getters
macro came about when I was making various data structures for JSON to deserialize into. These data structures had many fields in them to access and they weren't going to change once created. One could use pub
everywhere but that would enable mutating the fields which is what this derive aims to avoid.
Getters will be generated according to convention. This means that the generated methods will reside within the struct namespace.
With regards to Dissolve
, sometimes during conversion a structure must be consumed. One easy way to do this is to return a tuple of all the structs fields. Thus Dissolve
can be considered a 'get (move) everything' method call.
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.5.0"
Then import the Getters
or Dissolve
macro in whichever module it's needed (assuming 2018 edition).
use ;
Otherwise just import at crate root.
extern crate derive_getters;
Features
auto_copy_getters
- enable to automatically generate copy getters for primitive types (return by value instead of by reference).
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
.
/// Auto-generated by `derive_getters::Getters`.
You can also derive "copy getters" that return by value instead of by reference:
This will generate the following impl for MyCheesyStruct
.
/// Auto-generated by `derive_getters::Getters`.
If you want to automatically generate copy getters for primitive types, you can enable the auto_copy_getters
feature.
It is recommended to enable auto_copy_getters
because it makes your code faster (no need to dereference a pointer). The Rust reference recommends implementing the Copy
trait.
This crate can also handle structs with simple generic parameters and lifetime annotations. Check docs for further details.
With Dissolve
, use it like so;
An impl will be produced for Solid
like so;
/// Auto-generated by `derive_getters::Dissolve`.
Attributes
Getters
attributes:
#[getter(skip)]
on the field to skip generating a getter for it.#[getter(copy)]
on the field to return a value by copy instead of by reference (note: the value must implement theCopy
trait).#[getter(rename = "name")]
on the field to change the getter name to "name".
Dissolve
attributes:
#[dissolve(rename = "name")]
on the struct to change the name of the dissolve function to "name".
Comment Preservation
Doc comments on fields shall be copied over onto their respective getters. If there is no comment for the field then one shall be generated.
For example, this struct;
Shall have this auto-generated for it;
/// Auto-generated by `derive_getters::Getters`.
/// Auto-generated by `derive_getters::Dissolve`.
See? Now with comments!
Testing
With the presence of the new #[cfg]
feature auto_copy_getters
, not all tests will run with the usual cargo test
. To run all tests in one go, give the cargo-hack crate a try. After installing it, run;
cargo hack test --feature-powerset
Caveats
Getters
cannot be derived for unit structs, tuple structs, or enums.Dissolve
cannot be derived for unit structs or enums.- Without
auto_copy_getters
, all getter methods return an immutable reference,&
, to their field. This means for some types it can get awkward. But withauto_copy_getters
feature set there won't be any references used in the generated getters for primitiveCopy
types.