3 releases (breaking)
0.4.0 | Dec 29, 2024 |
---|---|
0.3.2 | Dec 25, 2024 |
0.2.1 | Sep 15, 2024 |
#1695 in Procedural macros
1,463 downloads per month
Used in 9 crates
(5 directly)
18KB
301 lines
getset2
Getset2 is a derive macro, which is inspired by getset, is designed for generating the most basic getters and setters on struct fields.
Install
Run the following Cargo command in your project directory:
cargo add getset2
Example
use getset2::Getset2;
#[derive(Default, Getset2)]
#[getset2(get_ref, set_with)]
pub struct Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[getset2(get_ref, set, get_mut, skip(set_with))]
private: T,
/// Doc comments are supported!
/// Multiline, even.
#[getset2(
get_copy(pub, const),
set(pub = "crate"),
get_mut(pub = "super"),
set_with(pub = "self")
)]
public: T,
#[getset2(skip)]
skip: (),
}
impl<T: Copy + Clone + Default> Foo<T> {
fn with_private(mut self, private: T) -> Self {
self.private = private;
self
}
fn skip(&self) {
self.skip
}
}
// cargo expand --example simple
fn main() {
let mut foo = Foo::default();
foo.set_private(1);
(*foo.private_mut()) += 1;
assert_eq!(*foo.private(), 2);
foo = foo.with_private(3);
assert_eq!(*foo.private(), 3);
foo.set_public(3);
assert_eq!(foo.public(), 3);
assert_eq!(foo.skip(), ());
}
Expand the source code above (Run cargo expand --example simple
):
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2018::*;
#[macro_use]
extern crate std;
use getset2::Getset2;
#[getset2(get_ref, set_with)]
pub struct Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[getset2(set, get_mut, skip(get_ref))]
private: T,
/// Doc comments are supported!
/// Multiline, even.
#[getset2(
get_copy(pub, const),
set(pub = "crate"),
get_mut(pub = "super"),
set_with(pub = "self")
)]
public: T,
#[getset2(skip)]
skip: (),
}
#[automatically_derived]
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(),
skip: ::core::default::Default::default(),
}
}
}
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)]
fn private_mut(&mut self) -> &mut T {
&mut self.private
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn with_private(mut self, val: T) -> Self {
self.private = val;
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub const fn public(&self) -> T {
self.public
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub(crate) fn set_public(&mut self, val: T) -> &mut Self {
self.public = val;
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub(crate) fn public_mut(&mut self) -> &mut T {
&mut self.public
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub(self) const fn with_public(mut self, val: T) -> Self {
self.public = val;
self
}
}
impl<T: Copy + Clone + Default> Foo<T> {
fn private(&self) -> &T {
&self.private
}
fn skip(&self) {
self.skip
}
}
fn main() {
let mut foo = Foo::default();
foo.set_private(1);
(*foo.private_mut()) += 1;
assert_eq!(*foo.private(), 2);
foo = foo.with_private(3);
assert_eq!(*foo.private(), 3);
foo.set_public(3);
assert_eq!(foo.public(), 3);
assert_eq!(foo.skip(), ());
}
Dependencies
~275–720KB
~16K SLoC