10 releases

0.2.0 Aug 18, 2020
0.1.0 Oct 14, 2019
0.0.8 Jun 16, 2019
0.0.7 Jun 24, 2018
0.0.2 Apr 23, 2017

#105 in Procedural macros

Download history 44403/week @ 2022-08-08 53191/week @ 2022-08-15 39224/week @ 2022-08-22 39138/week @ 2022-08-29 40820/week @ 2022-09-05 45247/week @ 2022-09-12 18209/week @ 2022-09-19 34654/week @ 2022-09-26 50775/week @ 2022-10-03 46508/week @ 2022-10-10 49655/week @ 2022-10-17 34129/week @ 2022-10-24 32862/week @ 2022-10-31 57285/week @ 2022-11-07 33583/week @ 2022-11-14 31133/week @ 2022-11-21

155,412 downloads per month
Used in 43 crates (26 directly)

MIT license

23KB
422 lines

Derive Getters

Simple Getters derive macro for generating field getter methods on a named struct. Included is an additional derive, Dissolve, that consumes the named struct returning a tuple of all fields in the order they were declared.

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

Documentation is here.

Installation

Add to your Cargo.toml:

[dependencies]
derive-getters = "0.2.0"

Then import the Getters or Dissolve macro in whichever module it's needed (assuming 2018 edition).

use derive_getters::{Getters, Dissolve};

Otherwise just import at crate root.

#[macro_use]
extern crate derive_getters;

Usage

When you have a struct you want to automatically derive getters for... Just add the derive at the top like so;

#[derive(Getters)]
pub struct MyCheesyStruct {
    x: i64,
    y: i64,
}

A new impl will be produced for MyCheesyStruct.

impl MyCheesyStruct {
    pub fn x(&self) -> &i64 {
        &self.x
    }

    pub fn y(&self) -> &i64 {
        &self.y
    }
}

This crate can also handle structs with simple generic parameters and lifetime annotations. Check docs for further details.

#[derive(Getters)]
pub struct StructWithGeneric<'a, T> {
    concrete: f64,
    generic: T,
    text: &'a str,
}

With Dissolve, use it like so;

#[derive(Dissolve)]
pub struct Solid {
    a: u64,
    b: f64,
    c: i64,
}

An impl will be produced for Solid like so;

impl Solid {
    pub fn dissolve(self) -> (u64, f64, i64) {
      (self.a, self.b, self.c)
    }
}

Attributes

This macro comes with two optional field attributes for Getters.

  • #[getter(skip)] to skip generating getters for a field.
  • #[getter(rename = "name")] to change the getter name to "name".

And one optional struct attribute for Dissolve.

  • #[dissolve(rename = "name")] to change the name of the dissolve function to "name".

Caveats

  1. Will not work on unit structs, tuples or enums. Derive Getters or Dissolve over them and the macro will chuck a wobbly.
  2. All getter methods return an immutable reference, &, to their field. This means for some types it can get awkward.

Alternatives

getset.

Dependencies

~210–610KB
~15K SLoC