@@ -8,46 +8,51 @@ Getters are generated as `fn field(&self) -> &type`, while setters are generated
88These 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!
99
1010```rust
11- use getset::{CopyGetters, Getters, MutGetters, Setters} ;
11+ use std::sync::Arc ;
1212
13- #[derive(Getters, Setters, MutGetters, CopyGetters, Default)]
13+ use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};
14+
15+ #[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)]
1416pub struct Foo<T>
1517where
1618 T: Copy + Clone + Default,
1719{
1820 /// Doc comments are supported!
1921 /// Multiline, even.
20- #[getset(get, set, get_mut)]
22+ #[getset(get, set, get_mut, set_with )]
2123 private: T,
2224
2325 /// Doc comments are supported!
2426 /// Multiline, even.
25- #[getset(get_copy = "pub", set = "pub", get_mut = "pub")]
27+ #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub" )]
2628 public: T,
27- }
2829
29- let mut foo = Foo::default();
30- foo.set_private(1);
31- (*foo.private_mut()) += 1;
32- assert_eq!(*foo.private(), 2);
30+ /// Arc supported through CloneGetters
31+ #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
32+ arc: Arc<u16>,
33+ }
3334```
3435
3536You can use `cargo-expand` to generate the output. Here are the functions that the above generates (Replicate with `cargo expand --example simple`):
3637
3738```rust,ignore
38- use getset::{Getters, MutGetters, CopyGetters, Setters, WithSetters};
39+ use std::sync::Arc;
40+ use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};
3941pub struct Foo<T>
4042where
4143 T: Copy + Clone + Default,
4244{
4345 /// Doc comments are supported!
4446 /// Multiline, even.
45- #[getset(get, get , get_mut)]
47+ #[getset(get, set , get_mut, set_with )]
4648 private: T,
4749 /// Doc comments are supported!
4850 /// Multiline, even.
49- #[getset(get_copy = "pub", set = "pub", get_mut = "pub")]
51+ #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub" )]
5052 public: T,
53+ /// Arc supported through CloneGetters
54+ #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
55+ arc: Arc<u16>,
5156}
5257impl<T> Foo<T>
5358where
@@ -64,13 +69,51 @@ impl<T> Foo<T>
6469where
6570 T: Copy + Clone + Default,
6671{
72+ /// Doc comments are supported!
73+ /// Multiline, even.
74+ #[inline(always)]
75+ fn set_private(&mut self, val: T) -> &mut Self {
76+ self.private = val;
77+ self
78+ }
6779 /// Doc comments are supported!
6880 /// Multiline, even.
6981 #[inline(always)]
7082 pub fn set_public(&mut self, val: T) -> &mut Self {
7183 self.public = val;
7284 self
7385 }
86+ /// Arc supported through CloneGetters
87+ #[inline(always)]
88+ pub fn set_arc(&mut self, val: Arc<u16>) -> &mut Self {
89+ self.arc = val;
90+ self
91+ }
92+ }
93+ impl<T> Foo<T>
94+ where
95+ T: Copy + Clone + Default,
96+ {
97+ /// Doc comments are supported!
98+ /// Multiline, even.
99+ #[inline(always)]
100+ fn with_private(mut self, val: T) -> Self {
101+ self.private = val;
102+ self
103+ }
104+ /// Doc comments are supported!
105+ /// Multiline, even.
106+ #[inline(always)]
107+ pub fn with_public(mut self, val: T) -> Self {
108+ self.public = val;
109+ self
110+ }
111+ /// Arc supported through CloneGetters
112+ #[inline(always)]
113+ pub fn with_arc(mut self, val: Arc<u16>) -> Self {
114+ self.arc = val;
115+ self
116+ }
74117}
75118impl<T> Foo<T>
76119where
@@ -88,6 +131,11 @@ where
88131 pub fn public_mut(&mut self) -> &mut T {
89132 &mut self.public
90133 }
134+ /// Arc supported through CloneGetters
135+ #[inline(always)]
136+ pub fn arc_mut(&mut self) -> &mut Arc<u16> {
137+ &mut self.arc
138+ }
91139}
92140impl<T> Foo<T>
93141where
@@ -100,6 +148,16 @@ where
100148 self.public
101149 }
102150}
151+ impl<T> Foo<T>
152+ where
153+ T: Copy + Clone + Default,
154+ {
155+ /// Arc supported through CloneGetters
156+ #[inline(always)]
157+ pub fn arc(&self) -> Arc<u16> {
158+ self.arc.clone()
159+ }
160+ }
103161```
104162
105163Attributes can be set on struct level for all fields in struct as well. Field level attributes take
0 commit comments