curmacro/
lib.rs

1/// struct getter creation macro
2/// 
3/// # Example
4/// ```
5/// use curmacro::*;
6/// 
7/// struct Point {
8///     pub x: i32,
9///     pub y: i32
10/// }
11/// 
12/// impl Point {
13///     getters!(
14///         pub get_x(x) -> i32;
15///         pub get_y(y) -> i32;    
16///     );
17/// }
18/// 
19/// let x = 13;
20/// let y = 215;
21/// let point = Point { x, y };
22/// 
23/// assert_eq!(point.get_x().clone(), x);
24/// assert_eq!(point.get_y().clone(), y);
25/// ```
26#[macro_export]
27macro_rules! getters {
28    (
29        $($vis:vis $fn:ident($field:ident) -> $type:ty;)*
30    ) => {
31        $(
32            $vis fn $fn(&self) -> &$type {
33                &self.$field
34            }
35        )*
36    };
37}
38
39/// struct setter creation macro
40/// 
41/// # Example
42/// ```
43/// use curmacro::*;
44/// 
45/// struct Point {
46///     pub x: i32,
47///     pub y: i32
48/// }
49/// 
50/// impl Point {
51///     setters!(
52///         pub set_x(i32) -> x;
53///         pub set_y(i32) -> y;    
54///     );
55/// }
56/// 
57/// let mut point = Point { x: 0, y: 0 };
58/// let x = 13;
59/// let y = 215;
60/// 
61/// point.set_x(x);
62/// point.set_y(y);
63/// 
64/// assert_eq!(point.x, x);
65/// assert_eq!(point.y, y);
66/// ```
67#[macro_export]
68macro_rules! setters {
69    (
70        $($vis:vis $fn:ident($type:ty) -> $field:ident;)*
71    ) => {
72        $(
73            $vis fn $fn(&mut self, value: $type) {
74                self.$field = value
75            }
76        )*
77    };
78}