#macro #getter #setter #getters #setters

nightly macro getset

Getset, we’re ready to go! A procedural macro for generating the most basic getters and setters on fields

8 releases

✓ Uses Rust 2018 edition

0.0.8 Sep 1, 2019
0.0.7 Mar 16, 2019
0.0.6 May 14, 2018
0.0.5 Jun 2, 2017

#7 in Procedural macro helpers

Download history 1605/week @ 2019-05-19 1521/week @ 2019-05-26 1407/week @ 2019-06-02 1561/week @ 2019-06-09 1470/week @ 2019-06-16 1474/week @ 2019-06-23 1470/week @ 2019-06-30 1266/week @ 2019-07-07 1611/week @ 2019-07-14 1793/week @ 2019-07-21 1255/week @ 2019-07-28 1207/week @ 2019-08-04 1331/week @ 2019-08-11 2624/week @ 2019-08-18 3316/week @ 2019-08-25

7,197 downloads per month
Used in 71 crates (25 directly)

MIT license

17KB
207 lines

getset

Build Status Docs

Getset, we're ready to go!

A procedural macro for generating the most basic getters and setters on fields.

Getters are generated as fn field(&self) -> &type, while setters are generated as fn field(&mut self, val: type).

These macros are not intended to be used on fields which require custom logic inside of their setters and getters. Just write your own in that case!

use getset::{Getters, MutGetters, Setters};

#[derive(Getters, Setters, MutGetters, Default)]
pub struct Foo<T>
where
    T: Copy + Clone + Default,
{
    /// Doc comments are supported!
    /// Multiline, even.
    #[get]
    #[set]
    #[get_mut]
    private: T,

    /// Doc comments are supported!
    /// Multiline, even.
    #[get = "pub"]
    #[set = "pub"]
    #[get_mut = "pub"]
    public: T,
}

fn main() {
    let mut foo = Foo::default();
    foo.set_private(1);
    (*foo.private_mut()) += 1;
    assert_eq!(*foo.private(), 2);
}

The above structure definition generates the following output with cargo expand.

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std as std;
use getset::{Getters, MutGetters, Setters};
pub struct Foo<T>
where
    T: Copy + Clone + Default,
{
    /// Doc comments are supported!
    /// Multiline, even.
    #[get]
    #[set]
    #[get_mut]
    private: T,
    /// Doc comments are supported!
    /// Multiline, even.
    #[get = "pub"]
    #[set = "pub"]
    #[get_mut = "pub"]
    public: T,
}
impl<T> Foo<T>
where
    T: Copy + Clone + Default,
{
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    fn private(&self) -> &T {
        &self.private
    }
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    pub fn public(&self) -> &T {
        &self.public
    }
}
impl<T> Foo<T>
where
    T: Copy + Clone + Default,
{
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    fn set_private(&mut self, val: T) -> &mut Self {
        self.private = val;
        self
    }
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    pub fn set_public(&mut self, val: T) -> &mut Self {
        self.public = val;
        self
    }
}
impl<T> Foo<T>
where
    T: Copy + Clone + Default,
{
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    fn private_mut(&mut self) -> &mut T {
        &mut self.private
    }
    /// Doc comments are supported!
    /// Multiline, even.
    #[inline(always)]
    pub fn public_mut(&mut self) -> &mut T {
        &mut self.public
    }
}
#[automatically_derived]
#[allow(unused_qualifications)]
impl<T: ::core::default::Default> ::core::default::Default for Foo<T>
where
    T: Copy + Clone + Default,
{
    #[inline]
    fn default() -> Foo<T> {
        Foo {
            private: ::core::default::Default::default(),
            public: ::core::default::Default::default(),
        }
    }
}

Attributes can be set on struct level for all fields in struct as well. Field level attributes take precedence.

#[macro_use]
extern crate getset;

mod submodule {
    #[derive(Getters, Default)]
    #[get = "pub"] // By default add a pub getting for all fields.
    pub struct Foo {
        public: i32,
        #[get] // Override as private
        private: i32,
    }
    fn demo() {
        let mut foo = Foo::default();
        foo.private();
    }
}
fn main() {
    let mut foo = submodule::Foo::default();
    foo.public();
}

For some purposes, it's useful to have the get_ prefix on the getters for either legacy of compatability reasons. It is done with get-prefix.

#[macro_use]
extern crate getset;

#[derive(Getters, Default)]
pub struct Foo {
    #[get = "pub with_prefix"]
    field: bool,
}

fn main() {
    let mut foo = Foo::default();
    let val = foo.get_field();
}

Dependencies

~0.7–1MB
~22K SLoC