generic-array-struct 0.3.2

An attribute proc macro to convert structs with named fields of the same generic type into a single-array-field tuple struct with array-index-based accessor and mutator methods.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# generic-array-struct

An attribute proc macro to convert structs with named fields of the same generic type into a single-array-field tuple struct with array-index-based accessor and mutator methods.

## MSRV

`rustc 1.83.0` (stabilization of [`core::mem::replace()`](`core::mem::replace()`) in `const`)

## Example Usage

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Cartesian<T> {
    /// x-coordinate
    pub x: T,

    /// y-coordinate
    pub y: T,
}
```

expands to

```rust
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Cartesian<T>([T; CARTESIAN_LEN]);

impl<T> Cartesian<T> {
    /// x-coordinate
    #[inline]
    pub const fn x(&self) -> &T {
        &self.0[CARTESIAN_IDX_X]
    }

    #[inline]
    pub const fn x_mut(&mut self) -> &mut T {
        &mut self.0[CARTESIAN_IDX_X]
    }

    /// Returns the old field value
    #[inline]
    pub const fn set_x(&mut self, val: T) -> T {
        core::mem::replace(&mut self.0[CARTESIAN_IDX_X], val)
    }

    #[inline]
    pub fn with_x(mut self, val: T) -> Self {
        self.0[CARTESIAN_IDX_X] = val;
        self
    }

    /// y-coordinate
    #[inline]
    pub const fn y(&self) -> &T {
        &self.0[CARTESIAN_IDX_Y]
    }

    #[inline]
    pub const fn y_mut(&mut self) -> &mut T {
        &mut self.0[CARTESIAN_IDX_Y]
    }

    /// Returns the old field value
    #[inline]
    pub const fn set_y(&mut self, val: T) -> T {
        core::mem::replace(&mut self.0[CARTESIAN_IDX_Y], val)
    }

    #[inline]
    pub fn with_y(mut self, val: T) -> Self {
        self.0[CARTESIAN_IDX_Y] = val;
        self
    }
}

impl<T: Copy> Cartesian<T> {
    #[inline]
    pub const fn const_with_x(mut self, val: T) -> Self {
        self.0[CARTESIAN_IDX_X] = val;
        self
    }

    #[inline]
    pub const fn const_with_y(mut self, val: T) -> Self {
        self.0[CARTESIAN_IDX_Y] = val;
        self
    }
}


impl<T> Cartesian<T> {
    pub const LEN: usize = 2;

    pub const IDX_X: usize = 0;
    pub const IDX_Y: usize = 1;
}

// consts are also exported with prefix (not just as associated consts)
// so that we dont need turbofish e.g. `Cartesian::<f32>::IDX_X`

pub const CARTESIAN_LEN: usize = 2;

pub const CARTESIAN_IDX_X: usize = 0;
pub const CARTESIAN_IDX_Y: usize = 1;
```

## Usage Notes

### Declaration Order

Because this attribute modifies the struct definition, it must be placed above any derive attributes or attributes that use the struct definition

#### WRONG ❌

```rust,compile_fail,E0609
use generic_array_struct::generic_array_struct;

// Fails to compile because #[generic_array_struct] is below #[derive] attribute
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[generic_array_struct]
pub struct Cartesian<D> {
    pub x: D,
    pub y: D,
}
```

#### RIGHT ✅

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cartesian<D> {
    pub x: D,
    pub y: D,
}
```

### Field Visibility

All methods have the same visibility as that of the originally declared field in the struct.

```rust,compile_fail,E0624
mod private {
    use generic_array_struct::generic_array_struct;

    #[generic_array_struct]
    #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct Cartesian<T> {
        // Note: fields are private
        x: T,
        y: T,
    }
}

use private::Cartesian;

// fails to compile because [`Cartesian::const_with_x`] is private
const ONE_COMMA_ZERO: Cartesian<f64> = Cartesian([0.0; 2]).const_with_x(1.0);
```

### Attribute args

The attribute can be further customized by the following space-separated positional args.

#### `destr` Arg

An optional `destr` prefix arg controls whether to output the original struct definition as a separate struct for destructuring.

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(destr pub)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}
```

expands to

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(pub)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CartesianDestr<Z> {
    pub x: Z,
    pub y: Z,
}

impl<T> Cartesian<T> {
    #[inline]
    pub fn from_destr(CartesianDestr { x, y, }: CartesianDestr<T>) -> Self {
        Self([x, y,])
    }

    #[inline]
    pub fn into_destr(self) -> CartesianDestr<T> {
        let Self([x, y,]) = self;
        CartesianDestr { x, y, }
    }
}

impl<T: Copy> Cartesian<T> {
    #[inline]
    pub const fn const_from_destr(CartesianDestr { x, y, }: CartesianDestr<T>) -> Self {
        Self([x, y,])
    }

    #[inline]
    pub const fn const_into_destr(self) -> CartesianDestr<T> {
        let Self([x, y,]) = self;
        CartesianDestr { x, y, }
    }
}

impl<T> From<CartesianDestr<T>> for Cartesian<T> {
    #[inline]
    fn from(d: CartesianDestr<T>) -> Self {
        Self::from_destr(d)
    }
}

impl<T> From<Cartesian<T>> for CartesianDestr<T> {
    #[inline]
    fn from(d: Cartesian<T>) -> Self {
        d.into_destr()
    }
}
```

#### `builder` Arg

An optional `builder` prefix arg controls whether to generate a builder struct that, at compile-time, ensures that every field is set exactly once before creating the struct.

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(builder pub)]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}
```

expands to

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(pub)]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}

// The const generic booleans track which fields have been set
#[repr(transparent)]
pub struct CartesianBuilder<Z, const S0: bool, const S1: bool>([core::mem::MaybeUninit<Z>; CARTESIAN_LEN]);

pub type NewCartesianBuilder<Z> = CartesianBuilder<Z, false, false>;

impl<T> NewCartesianBuilder<T> {
    // impl notes:
    // need to specify as associated const instead of fn local const, otherwise errors with
    // 'can't use generic parameters from outer item'
    const _UNINIT: core::mem::MaybeUninit<T> = core::mem::MaybeUninit::uninit();

    #[inline]
    pub const fn start() -> Self {
        Self([Self::_UNINIT; CARTESIAN_LEN])
    }
}

// impl notes:
// - cannot use transmute() due to const generic, cannot move out of struct due to Drop.
//   Hopefully rustc is able to optimize away all the 
//   transmute_copy() + core::mem::forget()s and use the same memory.
//   I cannot wait for array transmutes to be stabilized.

impl<Z, const S1: bool> CartesianBuilder<Z, false, S1> {
    #[inline]
    pub const fn with_x(
        mut self,
        val: Z,
    ) -> CartesianBuilder<Z, true, S1> {
        self.0[CARTESIAN_IDX_X] = core::mem::MaybeUninit::new(val);
        unsafe {
            core::mem::transmute_copy::<_, _>(
                &core::mem::ManuallyDrop::new(self)
            )
        }
    }
}

impl<Z, const S0: bool> CartesianBuilder<Z, S0, false> {
    #[inline]
    pub const fn with_y(
        mut self,
        val: Z,
    ) -> CartesianBuilder<Z, S0, true> {
        self.0[CARTESIAN_IDX_Y] = core::mem::MaybeUninit::new(val);
        unsafe {
            core::mem::transmute_copy::<_, _>(
                &core::mem::ManuallyDrop::new(self)
            )
        }
    }
}

impl<Z> CartesianBuilder<Z, true, true> {
    #[inline]
    pub const fn build(self) -> Cartesian<Z> {
        // if not `repr(transparent)`, must use `self.0` instead of `self`,
        // but we always enforce repr(transparent)
        unsafe {
            Cartesian(
                core::mem::transmute_copy::<_, _>(
                    &core::mem::ManuallyDrop::new(self)
                )
            )
        }
    }
}

/// This gets called if the Builder struct was dropped before `self.build()` was called
impl<Z, const S0: bool, const S1: bool> Drop for CartesianBuilder<Z, S0, S1> {
    fn drop(&mut self) {
        if S0 {
            unsafe {
                self.0[CARTESIAN_IDX_X].assume_init_drop();
            }
        }
        if S1 {
            unsafe {
                self.0[CARTESIAN_IDX_Y].assume_init_drop();
            } 
        }
    }
}

impl<Z, const S0: bool, const S1: bool> Clone for CartesianBuilder<Z, S0, S1> where Z: Copy {
    #[inline]
    fn clone(&self) -> Self {
        Self(self.0)
    }
}
```

##### Example Builder Usages

###### Attempting to build before setting all fields

```rust,compile_fail,E0599
use generic_array_struct::generic_array_struct;

#[generic_array_struct(builder)]
pub struct Cartesian<T> {
    pub x: T,
    pub y: T,
}

// y has not been set, this fails to compile with
// "method not found in `CartesianBuilder<{integer}, true, false>`"
let pt: Cartesian<u8> = NewCartesianBuilder::start().with_x(1).build();
```

###### Attempting to set a field twice

```rust,compile_fail,E0599
use generic_array_struct::generic_array_struct;

#[generic_array_struct(builder pub)]
pub struct Cartesian<T> {
    pub x: T,
    pub y: T,
}

// attempted to set x twice, this fails to compile with
// "no method named `with_x` found for struct `CartesianBuilder<{integer}, true, true>` in the current scope"
let pt: Cartesian<u8> = NewCartesianBuilder::start().with_x(1).with_y(0).with_x(2).build();
```

###### Proper initialization

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(builder pub(crate))]
pub struct Cartesian<T> {
    pub x: T,
    pub y: T,
}

// proper initialization after setting all fields exactly once
let pt: Cartesian<u8> = NewCartesianBuilder::start().with_x(1).with_y(0).build();
```

#### `trymap` Arg

An optional `trymap` prefix arg controls whether to generate 2 util methods, `try_map_opt` and `try_map_res` for the struct.

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(trymap)]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}
```

expands to

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct]
pub struct Cartesian<Z> {
    pub x: Z,
    pub y: Z,
}

// impl notes:
// - cannot use transmute() due to const generic, cannot move out of struct due to Drop.
//   Hopefully rustc is able to optimize away all the 
//   transmute_copy() + core::mem::forget()s and use the same memory.
//   I cannot wait for array transmutes to be stabilized.
// - generate 2 separate methods instead of using `Try` trait so that its compatible
//   with stable rust

impl<T> Cartesian<T> {
    #[inline]
    pub fn try_map_opt<B, F>(
        self,
        mut f: F,
    ) -> Option<Cartesian<B>> where F: FnMut(T) -> Option<B> {
        let mut res: Cartesian<core::mem::MaybeUninit<B>>
            = Cartesian(core::array::from_fn(|_| core::mem::MaybeUninit::uninit()));
        let written = self.0.into_iter().zip(res.0.iter_mut()).try_fold(
            0usize,
            |written, (val, rmut)| {
                rmut.write(f(val).ok_or(written)?);
                Ok(written + 1)
            }
        );
        if let Err(written) = written {
            res.0.iter_mut().take(written).for_each(
                |mu| unsafe { mu.assume_init_drop() }
            );
            None
        } else {
            Some(Cartesian(
                unsafe {
                    core::mem::transmute_copy::<_, _>(
                        &core::mem::ManuallyDrop::new(res.0)
                    )
                }
            ))
        }
    }

    #[inline]
    pub fn try_map_res<B, E, F>(
        self,
        mut f: F,
    ) -> Result<Cartesian<B>, E> where F: FnMut(T) -> Result<B, E> {
        let mut res: Cartesian<core::mem::MaybeUninit<B>>
            = Cartesian(core::array::from_fn(|_| core::mem::MaybeUninit::uninit()));
        let written = self.0.into_iter().zip(res.0.iter_mut()).try_fold(
            0usize,
            |written, (val, rmut)| {
                rmut.write(f(val).map_err(|e| (e, written))?);
                Ok(written + 1)
            }
        );
        if let Err((e, written)) = written {
            res.0.iter_mut().take(written).for_each(
                |mu| unsafe { mu.assume_init_drop() }
            );
            Err(e)
        } else {
            Ok(Cartesian(
                unsafe {
                    core::mem::transmute_copy::<_, _>(
                        &core::mem::ManuallyDrop::new(res.0)
                    )
                }
            ))
        }
    }
}
```

#### `.0` Visibility Attribute Arg

The attribute's final position arg is a [`syn::Visibility`](`syn::Visibility`) that controls the visibility of the resulting `.0` array field. 

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct]
pub struct Cartesian<T> {
    pub x: T,
    pub y: T,
}
```

generates

```rust
pub struct Cartesian<T>([T; 2]);
```

while

```rust
use generic_array_struct::generic_array_struct;

#[generic_array_struct(pub(crate))]
pub struct Cartesian<T> {
    pub x: T,
    pub y: T,
}
```

generates

```rust
pub struct Cartesian<T>(pub(crate) [T; 2]);
```