[−][src]Crate derive_getters
This library provides a derive macro for autogenerating getters. It can only be used on named structs. It will generate getters that will reside in the struct namespace.
Derives
Only named structs can derive Getters
.
Methods generated
The getter methods generated shall bear the same name as the struct fields and be
publicly visible. The methods return an immutable reference to the struct field of the
same name. If there is already a method defined with that name there'll be a collision.
In these cases one of two attributes can be set to either skip
or rename
the getter.
Usage
In lib.rs or main.rs;
use derive_getters::Getters; #[derive(Getters)] struct Number { num: u64, } fn main() { let number = Number { num: 655 }; assert!(number.num() == &655); }
Here, a method called num()
has been created for the Number
struct which gives a
reference to the num
field.
This macro can also derive on structs that have simple generic types. For example;
#[derive(Getters)] struct Generic<T, U> { gen_t: T, gen_u: U, }
The macro can also handle generic types with trait bounds. For example;
#[derive(Getters)] struct Generic<T: Clone, U: Copy> { gen_t: T, gen_u: U, }
The trait bounds can also be declared in a where
clause.
Additionaly, simple lifetimes are OK too;
#[derive(Getters)] struct Annotated<'a, 'b, T> { stuff: &'a T, comp: &'b str, num: u64, }
Attributes
Getters can be further configured to either skip or rename a getter.
-
#[getter(skip)] Will skip generating a getter for the field being decorated.
-
#[getter(rename = "name")] Changes the name of the getter (default is the field name) to "name".
#[derive(Getters)] struct Attributed { keep_me: u64, #[getter(skip)] skip_me: u64, #[getter(rename = "number")] rename_me: u64, }
Panics
If getters are derived on unit structs, unnamed structs, enums or unions.
Cannot Do
Const generics aren't handled by this macro nor are they tested.
Derive Macros
Getters | Generate getter methods for all named struct fields in a seperate struct |