@@ -8,46 +8,61 @@ 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,
29+
30+ /// Arc supported through CloneGetters
31+ #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
32+ arc: Arc<u16>,
2733}
2834
29- let mut foo = Foo::default();
30- foo.set_private(1);
31- (*foo.private_mut()) += 1;
32- assert_eq!(*foo.private(), 2);
35+ fn main() {
36+ let mut foo = Foo::default();
37+ foo.set_private(1);
38+ (*foo.private_mut()) += 1;
39+ assert_eq!(*foo.private(), 2);
40+ foo = foo.with_private(3);
41+ assert_eq!(*foo.private(), 3);
42+ assert_eq!(*foo.arc(), 0);
43+ }
3344```
3445
3546You can use `cargo-expand` to generate the output. Here are the functions that the above generates (Replicate with `cargo expand --example simple`):
3647
3748```rust,ignore
38- use getset::{Getters, MutGetters, CopyGetters, Setters, WithSetters};
49+ use std::sync::Arc;
50+ use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};
3951pub struct Foo<T>
4052where
4153 T: Copy + Clone + Default,
4254{
4355 /// Doc comments are supported!
4456 /// Multiline, even.
45- #[getset(get, get , get_mut)]
57+ #[getset(get, set , get_mut, set_with )]
4658 private: T,
4759 /// Doc comments are supported!
4860 /// Multiline, even.
49- #[getset(get_copy = "pub", set = "pub", get_mut = "pub")]
61+ #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub" )]
5062 public: T,
63+ /// Arc supported through CloneGetters
64+ #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
65+ arc: Arc<u16>,
5166}
5267impl<T> Foo<T>
5368where
@@ -64,13 +79,51 @@ impl<T> Foo<T>
6479where
6580 T: Copy + Clone + Default,
6681{
82+ /// Doc comments are supported!
83+ /// Multiline, even.
84+ #[inline(always)]
85+ fn set_private(&mut self, val: T) -> &mut Self {
86+ self.private = val;
87+ self
88+ }
6789 /// Doc comments are supported!
6890 /// Multiline, even.
6991 #[inline(always)]
7092 pub fn set_public(&mut self, val: T) -> &mut Self {
7193 self.public = val;
7294 self
7395 }
96+ /// Arc supported through CloneGetters
97+ #[inline(always)]
98+ pub fn set_arc(&mut self, val: Arc<u16>) -> &mut Self {
99+ self.arc = val;
100+ self
101+ }
102+ }
103+ impl<T> Foo<T>
104+ where
105+ T: Copy + Clone + Default,
106+ {
107+ /// Doc comments are supported!
108+ /// Multiline, even.
109+ #[inline(always)]
110+ fn with_private(mut self, val: T) -> Self {
111+ self.private = val;
112+ self
113+ }
114+ /// Doc comments are supported!
115+ /// Multiline, even.
116+ #[inline(always)]
117+ pub fn with_public(mut self, val: T) -> Self {
118+ self.public = val;
119+ self
120+ }
121+ /// Arc supported through CloneGetters
122+ #[inline(always)]
123+ pub fn with_arc(mut self, val: Arc<u16>) -> Self {
124+ self.arc = val;
125+ self
126+ }
74127}
75128impl<T> Foo<T>
76129where
@@ -88,6 +141,11 @@ where
88141 pub fn public_mut(&mut self) -> &mut T {
89142 &mut self.public
90143 }
144+ /// Arc supported through CloneGetters
145+ #[inline(always)]
146+ pub fn arc_mut(&mut self) -> &mut Arc<u16> {
147+ &mut self.arc
148+ }
91149}
92150impl<T> Foo<T>
93151where
@@ -100,6 +158,16 @@ where
100158 self.public
101159 }
102160}
161+ impl<T> Foo<T>
162+ where
163+ T: Copy + Clone + Default,
164+ {
165+ /// Arc supported through CloneGetters
166+ #[inline(always)]
167+ pub fn arc(&self) -> Arc<u16> {
168+ self.arc.clone()
169+ }
170+ }
103171```
104172
105173Attributes can be set on struct level for all fields in struct as well. Field level attributes take
0 commit comments