@@ -4,7 +4,7 @@ use syn::{
4
4
self , ext:: IdentExt , spanned:: Spanned , Expr , Field , Lit , Meta , MetaNameValue , Visibility ,
5
5
} ;
6
6
7
- use self :: GenMode :: { Get , GetCopy , GetMut , Set , SetWith } ;
7
+ use self :: GenMode :: { Get , GetClone , GetCopy , GetMut , Set , SetWith } ;
8
8
use super :: parse_attr;
9
9
10
10
pub struct GenParams {
@@ -15,6 +15,7 @@ pub struct GenParams {
15
15
#[ derive( PartialEq , Eq , Copy , Clone ) ]
16
16
pub enum GenMode {
17
17
Get ,
18
+ GetClone ,
18
19
GetCopy ,
19
20
GetMut ,
20
21
Set ,
@@ -25,6 +26,7 @@ impl GenMode {
25
26
pub fn name ( self ) -> & ' static str {
26
27
match self {
27
28
Get => "get" ,
29
+ GetClone => "get_clone" ,
28
30
GetCopy => "get_copy" ,
29
31
GetMut => "get_mut" ,
30
32
Set => "set" ,
@@ -34,23 +36,23 @@ impl GenMode {
34
36
35
37
pub fn prefix ( self ) -> & ' static str {
36
38
match self {
37
- Get | GetCopy | GetMut => "" ,
39
+ Get | GetClone | GetCopy | GetMut => "" ,
38
40
Set => "set_" ,
39
41
SetWith => "with_" ,
40
42
}
41
43
}
42
44
43
45
pub fn suffix ( self ) -> & ' static str {
44
46
match self {
45
- Get | GetCopy | Set | SetWith => "" ,
47
+ Get | GetClone | GetCopy | Set | SetWith => "" ,
46
48
GetMut => "_mut" ,
47
49
}
48
50
}
49
51
50
52
fn is_get ( self ) -> bool {
51
53
match self {
52
- GenMode :: Get | GenMode :: GetCopy | GenMode :: GetMut => true ,
53
- GenMode :: Set | GenMode :: SetWith => false ,
54
+ Get | GetClone | GetCopy | GetMut => true ,
55
+ Set | SetWith => false ,
54
56
}
55
57
}
56
58
}
@@ -112,6 +114,7 @@ fn has_prefix_attr(f: &Field, params: &GenParams) -> bool {
112
114
. filter_map ( |attr| parse_attr ( attr, params. mode ) )
113
115
. find ( |meta| {
114
116
meta. path ( ) . is_ident ( "get" )
117
+ || meta. path ( ) . is_ident ( "get_clone" )
115
118
|| meta. path ( ) . is_ident ( "get_copy" )
116
119
|| meta. path ( ) . is_ident ( "get_mut" )
117
120
} )
@@ -167,7 +170,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
167
170
// Generate nothing for skipped field
168
171
Some ( meta) if meta. path ( ) . is_ident ( "skip" ) => quote ! { } ,
169
172
Some ( _) => match params. mode {
170
- GenMode :: Get => {
173
+ Get => {
171
174
quote ! {
172
175
#( #doc) *
173
176
#[ inline( always) ]
@@ -176,7 +179,16 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
176
179
}
177
180
}
178
181
}
179
- GenMode :: GetCopy => {
182
+ GetClone => {
183
+ quote ! {
184
+ #( #doc) *
185
+ #[ inline( always) ]
186
+ #visibility fn #fn_name( & self ) -> #ty {
187
+ self . #field_name. clone( )
188
+ }
189
+ }
190
+ }
191
+ GetCopy => {
180
192
quote ! {
181
193
#( #doc) *
182
194
#[ inline( always) ]
@@ -185,7 +197,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
185
197
}
186
198
}
187
199
}
188
- GenMode :: Set => {
200
+ Set => {
189
201
quote ! {
190
202
#( #doc) *
191
203
#[ inline( always) ]
@@ -195,7 +207,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
195
207
}
196
208
}
197
209
}
198
- GenMode :: GetMut => {
210
+ GetMut => {
199
211
quote ! {
200
212
#( #doc) *
201
213
#[ inline( always) ]
@@ -204,7 +216,7 @@ pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
204
216
}
205
217
}
206
218
}
207
- GenMode :: SetWith => {
219
+ SetWith => {
208
220
quote ! {
209
221
#( #doc) *
210
222
#[ inline( always) ]
@@ -234,7 +246,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
234
246
// Generate nothing for skipped field
235
247
Some ( meta) if meta. path ( ) . is_ident ( "skip" ) => quote ! { } ,
236
248
Some ( _) => match params. mode {
237
- GenMode :: Get => {
249
+ Get => {
238
250
let fn_name = Ident :: new ( "get" , Span :: call_site ( ) ) ;
239
251
quote ! {
240
252
#( #doc) *
@@ -244,7 +256,17 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
244
256
}
245
257
}
246
258
}
247
- GenMode :: GetCopy => {
259
+ GetClone => {
260
+ let fn_name = Ident :: new ( "get" , Span :: call_site ( ) ) ;
261
+ quote ! {
262
+ #( #doc) *
263
+ #[ inline( always) ]
264
+ #visibility fn #fn_name( & self ) -> #ty {
265
+ self . 0 . clone( )
266
+ }
267
+ }
268
+ }
269
+ GetCopy => {
248
270
let fn_name = Ident :: new ( "get" , Span :: call_site ( ) ) ;
249
271
quote ! {
250
272
#( #doc) *
@@ -254,7 +276,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
254
276
}
255
277
}
256
278
}
257
- GenMode :: Set => {
279
+ Set => {
258
280
let fn_name = Ident :: new ( "set" , Span :: call_site ( ) ) ;
259
281
quote ! {
260
282
#( #doc) *
@@ -265,7 +287,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
265
287
}
266
288
}
267
289
}
268
- GenMode :: GetMut => {
290
+ GetMut => {
269
291
let fn_name = Ident :: new ( "get_mut" , Span :: call_site ( ) ) ;
270
292
quote ! {
271
293
#( #doc) *
@@ -275,7 +297,7 @@ pub fn implement_for_unnamed(field: &Field, params: &GenParams) -> TokenStream2
275
297
}
276
298
}
277
299
}
278
- GenMode :: SetWith => {
300
+ SetWith => {
279
301
let fn_name = Ident :: new ( "set_with" , Span :: call_site ( ) ) ;
280
302
quote ! {
281
303
#( #doc) *
0 commit comments