5 releases

Uses old Rust 2015

0.2.2 Feb 7, 2019
0.2.1 Jan 13, 2019
0.2.0 Nov 24, 2018
0.1.1 Sep 15, 2018
0.1.0 Sep 13, 2018

#142 in #telegram

24 downloads per month
Used in actix-telegram

MIT license

28KB
685 lines

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!

#[macro_use]
extern crate getset;

#[derive(Getters, Setters, New, Default)]
#[get(vis = "pub")]
#[get(vis = "pub", mutable)]
#[set(vis = "pub", consume)]
#[set(vis = "pub")]
#[new(vis = "pub")]
pub struct Foo<T> where T: Copy + Clone + Default {
/// Doc comments are supported!
/// Multiline, even.
#[get(copy)] #[get(mutable)] #[set]
private: T,

/// Doc comments are supported!
/// Multiline, even.
public: Option<T>,

#[set(optional)]
optional: Option<String>,
}

fn main() {
let mut foo: Foo<i64> = Foo::new(1).consume_set_public(3);
assert_eq!(foo.private(), 1);
assert_eq!(*foo.public(), Some(3));
foo.set_private(3);
(*foo.private_mut()) += 1;
assert_eq!(foo.private(), 4);
foo.set_public(4);
assert_eq!(*foo.public(), Some(4));
foo.set_public(None);
assert_eq!(*foo.public(), None);
foo.set_optional(Some("test"));
assert_eq!(foo.optional(), &Some("test".to_string()));
foo.set_optional(None::<&str>);
assert_eq!(*foo.optional(), None);
}
#[macro_use]
extern crate getset;
mod submodule {
#[derive(Getters, Default)]
#[get = "pub"]
pub struct Foo {
#[get]
private: i32,
public: i32,
}
}
fn main() {
let mut foo = submodule::Foo::default();
assert_eq!(*foo.private(), 2);
}

Dependencies

~2MB
~47K SLoC