gset 1.0.3

A procedural macro for generating the most basic getters and setters on fields.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# gset

_Getters and Setters for Rust._

[![Crates.io][crates-badge]][crates-url]
[![Documentation][docs-badge]][docs-url]
[![MIT licensed][mit-badge]][mit-url]
[![Build Status][actions-badge]][actions-url]

[crates-badge]: https://img.shields.io/crates/v/gset.svg
[crates-url]: https://crates.io/crates/gset
[docs-badge]: https://img.shields.io/docsrs/gset
[docs-url]: https://docs.rs/gset
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: https://github.com/andrewsonin/gset/blob/main/LICENSE
[actions-badge]: https://github.com/andrewsonin/gset/actions/workflows/ci.yml/badge.svg
[actions-url]: https://github.com/andrewsonin/gset/actions/workflows/ci.yml

Provides a procedural macro capable of deriving  basic getters and setters for structs.

## Usage example

An example of using this library is provided below.

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct<T>
{
    /// Field 1.
    #[getset(get_copy, name = "get_field_1", vis = "pub")]
    #[getset(set)]
    field_1: f64,

    /// Field 2.
    #[getset(get_deref, vis = "pub")]
    #[getset(get_deref_mut, vis = "pub")]
    #[getset(set, vis = "pub")]
    field_2: Vec<T>,
}
```

This also works well for tuple structures,
but the `name` parameter becomes mandatory.

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct<T>(

    /// Field 1.
    #[getset(get_copy, name = "get_field_1", vis = "pub")]
    #[getset(set, name = "set_field_1")]
    f64,

    /// Field 2.
    #[getset(get_deref, name = "get_field_2", vis = "pub")]
    #[getset(get_deref_mut, name = "get_field_2_mut", vis = "pub")]
    #[getset(set, name = "set_field_2", vis = "pub")]
    Vec<T>,
);
```

## Field attributes

All field attributes have the following named parameters:
- `name` — name of the method being inferred.
Must be a valid Rust [identifier]https://docs.rs/syn/1.0.109/syn/struct.Ident.html.
This is a required parameter for tuple structs.
- `vis` — visibility of the method being inferred.
  Must be a valid Rust [visibility modifier]https://docs.rs/syn/1.0.109/syn/enum.Visibility.html.
  Visibility is `private` by default.

And some of them have the following named parameter:
- `ty` — return type of the method being inferred. Must be a valid Rust [type]https://docs.rs/syn/1.0.109/syn/enum.Type.html.

#### Legend
Here and further we will adhere to the following notation.
- `field` — field name.
- `T` — field type.

The field attributes currently supported are listed below.

### 1. `get`

Derives a reference getter for a field.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `&T` return type.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get, vis = "pub")]
    a: f64,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: f64,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> &f64 {
        &self.a
    }
}
```

### 2. `get_mut`

Derives a mutable getter for a field.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field_mut`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `&mut T` return type.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_mut, vis = "pub")]
    a: f64,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: f64,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a_mut(&mut self) -> &mut f64 {
        &mut self.a
    }
}
```

### 3. `get_copy`

Derives a copy getter for a field.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `T` return type.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_copy, vis = "pub")]
    a: f64,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: f64,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> f64 {
        self.a
    }
}
```

### 4. `get_deref`

Derives a reference getter for a field, which applies the `deref` operation to the resulting reference.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `&<T as ::std::ops:Deref>::Target` return type.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_deref, vis = "pub")]
    a: Vec<f64>,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: Vec<f64>,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> &[f64] {
        &self.a
    }
}
```

### 5. `get_deref_mut`

Derives a mutable getter for a field, which applies the `deref_mut` operation to the resulting reference.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field_mut`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `&mut <T as ::std::ops:Deref>::Target` return type.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_deref_mut, vis = "pub")]
    a: Vec<f64>,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: Vec<f64>,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a_mut(&mut self) -> &mut [f64] {
        &mut self.a
    }
}
```

### 6. `get_deref_copy`

Derives a copy getter for a field, which applies dereferencing to the field value.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. If not set, it will have the `<T as ::std::ops:Deref>::Target` return type.

#### Example

```rust
use derive_more::Deref;
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_deref_copy, vis = "pub")]
    a: F64,
}

#[derive(Deref)]
struct F64(f64);
```
will expand into
```rust
use derive_more::Deref;

struct Struct {
    /// Doc comment.
    a: F64,
}

#[derive(Deref)]
struct F64(f64);

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> f64 {
        *self.a
    }
}
```

### 7. `get_as_ref`

Derives a reference getter for a field, which applies the `as_ref` operation to the resulting reference.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. Required parameter.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_as_ref, vis = "pub", ty = "Option<&f64>")]
    a: Option<f64>,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: Option<f64>,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> Option<&f64> {
        self.a.as_ref()
    }
}
```

### 8. `get_as_deref`

Derives a reference getter for a field, which applies the `as_deref` operation to the resulting reference.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. Required parameter.

#### Example

```rust
use derive_more::Deref;
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_as_deref, vis = "pub", ty = "Option<&f64>")]
    a: Option<F64>,
}

#[derive(Deref)]
struct F64(f64);
```
will expand into
```rust
use derive_more::Deref;

struct Struct {
    /// Doc comment.
    a: Option<F64>,
}

#[derive(Deref)]
struct F64(f64);

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a(&self) -> Option<&f64> {
        self.a.as_deref()
    }
}
```

### 9. `get_as_deref_mut`

Derives a mutable getter for a field, which applies the `as_deref_mut` operation to the resulting reference.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `field_mut`.
- `vis` — visibility of the resulting method. If not set, it will be private.
- `ty` — return type of the resulting method. Required parameter.

#### Example

```rust
use derive_more::{Deref, DerefMut};
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(get_as_deref_mut, vis = "pub", ty = "Option<&mut f64>")]
    a: Option<F64>,
}

#[derive(Deref, DerefMut)]
struct F64(f64);
```
will expand into
```rust
use derive_more::{Deref, DerefMut};

struct Struct {
    /// Doc comment.
    a: Option<F64>,
}

#[derive(Deref, DerefMut)]
struct F64(f64);

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn a_mut(&mut self) -> Option<&mut f64> {
        self.a.as_deref_mut()
    }
}
```

### 10. `set`

Derives a setter for a field.

#### Parameters
- `name` — name of the resulting method. If not set, it will be named as `set_field`.
- `vis` — visibility of the resulting method. If not set, it will be private.

#### Example

```rust
use gset::Getset;

#[derive(Getset)]
struct Struct {
    /// Doc comment.
    #[getset(set, vis = "pub")]
    a: f64,
}
```
will expand into
```rust
struct Struct {
    /// Doc comment.
    a: f64,
}

impl Struct {
    /// Doc comment.
    #[inline]
    pub fn set_a(&mut self, value: f64) {
        self.a = value
    }
}
```