Derive macro generating an impl for accessing the fields of a struct.\
Use #[accessors(get, get_mut, set)]
to defined which accessors you want to have on a field.
List of accessors
param.
- get
: Generate a getter returning a reference.
- get_copy
: Generate a getter returning a copy. (mutually exclusive with get)
- get_mut
: Generate a mutable getter returning a mutable reference.
- set
: Generate a setter.
Using #[accessors(...)]
on a field will generate accessors for this specific field.\
Using #[accessors(...)]
on a struct will generate accessors for all field in the struct.
Example using accessors
on fields only.
```rust
struct MyStruct {
#[accessors(getcopy, getmut)]
#[accessors(set)] // Can use multiple #[accessors(...)]
a: u8,
#[accessors(get, get_mut, set)]
b: String,
}
``
Example using
accessors` on struct.
```rust
struct MyStruct {
#[accessors(getcopy)] // get_copy
will overwrite get
.
a: u8,
b: String,
}
Those two example are equivalent and will generate the same code:
rust
impl MyStruct {
pub fn a(&self) -> u8 {
self.a
}
pub fn amut(&mut self) -> &mut u8 {
&mut self.a
}
pub fn seta(&mut self, a: u8) {
self.a = a;
}
pub fn b(&self) -> &String {
&self.b
}
pub fn bmut(&mut self) -> &mut String {
&mut self.b
}
pub fn set_b(&mut self, b: String) {
self.b = b;
}
```