A nice and configurable derive macro for getters & setters. See derive macro docs for full list of options
```rust
struct Structopher { /// The comment gets copied over #[access(set, get, getmut)] // Generate a setter, getter ant mut getter field: String, _field2: u8, // Generate nothing } let mut data = Structopher::default(); data.setfield("Hello, world!".to_string());
let get: &String = data.field(); assert_eq!(get, "Hello, world!", "get(1)");
let mut get: &mut String = data.fieldmut(); *get = "Hello, universe!".tostring();
let mut get = data.field(); assert_eq!(get, "Hello, universe!", "get(2)"); ```
```rust impl Structopher { #[doc = " The comment gets copied over"] #[inline] pub fn field(&self) -> &String { &self.field } #[doc = " The comment gets copied over"] #[inline] pub fn fieldmut(&mut self) -> &mut String { &mut self.field } #[doc = " The comment gets copied over"] #[inline] pub fn setfield(&mut self, newvalue: String) -> &mut Self { self.field = newvalue; self } } ````
Option priority is as follows:
get
, get_mut
, set
)all
)defaults
)
get
, get_mut
, set
)all
)```rust
get, set, // derive these for all fields by default
// set defaults for whenever
defaults(
all(
constfn, // Make it a const fn
owned, // use self
and not &self
cp // Treat it as a copy type. Treats it as a reference if not set & not owned
),
get(
owned = false, // overwrite from all
vis(pub(crate)) // set visibilty to pub(crate)
)
)
)]
struct Structopher {
#[access(
all(constfn = false), // Disable the container's constfn for this field
get(constfn), // But re-enable it for the getter
get_mut // enable with defaults
)]
x: i8,
y: i8,
#[access(get_mut(skip))] // skip only get_mut
z: i8,
#[access(skip)] // skip this field altogether
w: i8,
}
const INST: Structopher = Structopher { x: 0, y: 0, z: 0, w: 0 } .sety(-10) .setz(10);
let mut inst = Structopher::default(); inst = inst.setx(10); *inst.xmut() += 1;
asserteq!(INST, Structopher { x: 0, y: -10, z: 10, w: 0 } , "const instance"); asserteq!(inst, Structopher { x: 11, y: 0, z: 0, w: 0 } , "instance"); ```
```rust impl Structopher { #[inline] pub(crate) const fn x(&self) -> i8 { self.x } #[inline] pub fn xmut(mut self) -> i8 { self.x } #[inline] pub fn setx(mut self, newvalue: i8) -> Self { self.x = newvalue; self } #[inline] pub(crate) const fn y(&self) -> i8 { self.y } #[inline] pub const fn sety(mut self, newvalue: i8) -> Self { self.y = newvalue; self } #[inline] pub(crate) const fn z(&self) -> i8 { self.z } #[inline] pub const fn setz(mut self, newvalue: i8) -> Self { self.z = newvalue; self } } ````
You can modify function return types & names
```rust
struct Structopher {
#[access(
get(suffix(rightnow), ty(&str)), // set the suffix and type
getmut(suffix("")) // remove the inherited suffix set by get_mut
)]
good: String,
}
let mut inst = Structopher::default();
*inst.good() = "On it, chief".into();
asserteq!(inst.getgoodrightnow(), "On it, chief");
```
```rust impl Structopher { #[inline] pub fn getgoodright_now(&self) -> &str { &self.good } #[inline] pub fn good(&mut self) -> &mut String { &mut self.good } } ````