A nice and configurable derive macro for getters & setters. See derive macro docs for full list of options

MASTER CI status crates.io badge docs.rs badge dependencies badge

Examples

Basic usage

```rust

[derive(Default, accessory::Accessors)]

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)"); ```

Generated output

```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 inheritance

Option priority is as follows:

  1. Field attribute
    1. Per-accessor type (get, get_mut, set)
    2. Catch-all (all)
  2. Container attribute (defaults)
    1. Per-accessor type (get, get_mut, set)
    2. Catch-all (all)

```rust

[derive(accessory::Accessors, Default, Eq, PartialEq, Debug)]

[access(

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"); ```

Generated output

```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 } } ````

Names & types

You can modify function return types & names

```rust

[derive(Default, accessory::Accessors)]

[access(defaults(get(prefix(get))))]

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"); ```

Generated output

```rust impl Structopher { #[inline] pub fn getgoodright_now(&self) -> &str { &self.good } #[inline] pub fn good(&mut self) -> &mut String { &mut self.good } } ````

Generic bounds

```rust

[derive(Default, accessory::Accessors)]

[access(bounds(World: PartialEq))] // applies to the impl block

struct Hello { #[access(get(cp, bounds(World: Copy)))] // Applies to specific accessor world: World, }

let world: u8 = Hello { world: 10u8 }.world(); assert_eq!(world, 10); ```

Generated output

```rust impl Hello where World: PartialEq { #[inline] pub fn world(&self) -> World where World: Copy { self.world } } ````