fovea-derive 0.1.1

Derive macros for fovea pixel and image traits
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
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Attribute, Data, DeriveInput, Fields, Result};

/// Main entry point for the PlainPixel derive macro.
///
/// # Steps to implement:
/// 1. Validate that the input is a struct (not enum or union)
/// 2. Validate that the struct has `#[repr(C)]`
/// 3. Extract named fields from the struct
/// 4. For each field, compute its channel size (will use the field type's SIZE)
/// 5. Generate the `CHANNELS` constant as a slice of sizes
/// 6. Emit the `unsafe impl PlainPixel for StructName { ... }`
pub(crate) fn derive(input: DeriveInput) -> Result<TokenStream> {
    // Step 1 - Validate struct
    let name = &input.ident;
    let fields = validate_struct(&input)?;

    // Step 2 - Validate #[repr(C)] or #[repr(transparent)]
    let repr = validate_repr(&input, fields)?;

    // Step 3 - Generate CHANNELS constant
    let channels = generate_channels_constant(fields, &repr)?;

    // Step 4 - Generate the impl block
    //
    // Force evaluation of the compile-time assertions defined as default
    // associated constants in `PlainPixel`.  Without these references the
    // assertions are dead code — a broken pixel type could slip through
    // without triggering them.  See REVIEW.md issue #2.
    // `PlainPixel` extends `PlainChannel`, so the derive must emit
    // both impls. `PlainChannel` carries the byte-layout role
    // (`SIZE`, `ALIGN`, `as_bytes`, `from_bytes`, `_ASSERT_SIZE`);
    // `PlainPixel` carries the pixel role (`CHANNELS`, `DIM`, endian
    // helpers, `cast_slice`, `_ASSERT_CHANNELS`). Both use default
    // method bodies; the `unsafe impl` blocks exist solely to witness
    // the invariants.
    //
    // SAFETY: the caller has `#[repr(C)]` or `#[repr(transparent)]`
    // (validated above). The compile-time `_ASSERT_SIZE` (byte-level
    // witness) and `_ASSERT_CHANNELS` (pixel-level witness) are
    // forced below so a broken pixel type cannot slip through
    // silently.
    let expanded = quote! {
        unsafe impl ::fovea::pixel::PlainChannel for #name {}
        unsafe impl ::fovea::pixel::PlainPixel for #name {
            const CHANNELS: &'static [usize] = #channels;
        }
        const _: () = { let _ = <#name as ::fovea::pixel::PlainChannel>::_ASSERT_SIZE; };
        const _: () = { let _ = <#name as ::fovea::pixel::PlainPixel>::_ASSERT_CHANNELS; };
    };

    Ok(expanded)
}

/// Generates the CHANNELS constant based on repr and fields.
///
/// For repr(C): &[<Field0>::SIZE, <Field1>::SIZE, ...]
/// For repr(transparent): <InnerField>::CHANNELS (inherit from inner type)
fn generate_channels_constant(fields: &Fields, repr: &Repr) -> Result<TokenStream> {
    match repr {
        Repr::C => {
            // for repr(C), CHANNELS = array of each field's SIZE.
            //
            // Resolve `SIZE` through `PlainChannel` (the byte-layout
            // role) instead of `PlainPixel` (the pixel role). Every
            // existing `PlainPixel` field type satisfies this bound
            // via the supertrait, and `f32` / `f64` satisfy it too,
            // unblocking float-channelled pixel structs (`MonoF32`,
            // `RgbF32`, ...) where `PlainPixel for f32 / f64` is
            // intentionally not provided.
            let field_types = fields.iter().map(|field| {
                let ty = &field.ty;
                quote! { <#ty as ::fovea::pixel::PlainChannel>::SIZE }
            });
            Ok(quote! { &[#(#field_types),*] })
        }
        Repr::Transparent => {
            // For repr(transparent), the wrapper has exactly one
            // field whose byte layout it inherits verbatim. Emit
            // CHANNELS as a single-channel slice whose only entry is
            // the inner type's `PlainChannel::SIZE`.
            //
            // The derive resolves through `<ty as PlainChannel>::SIZE`
            // rather than `<ty as PlainPixel>::CHANNELS`. The latter
            // would require the inner type to be a pixel — too strong,
            // because every transparent pixel in the library wraps a
            // single scalar channel (`Mono8(Saturating<u8>)`,
            // `MonoF32(f32)`, etc.) and the channel role is what
            // byte-inheritance actually requires. Going through
            // `PlainChannel::SIZE` unblocks `MonoF32` / `MonoF64`
            // (whose inner `f32` / `f64` are `PlainChannel` but not
            // `PlainPixel`) and gives the same result for every other
            // transparent wrapper because they are all single-channel.
            let field = fields.iter().next().expect("Expected exactly one field");
            let ty = &field.ty;
            Ok(quote! { &[<#ty as ::fovea::pixel::PlainChannel>::SIZE] })
        }
    }
}

/// Validates that the input is a struct with named fields.
/// Returns the fields if valid.
fn validate_struct(input: &DeriveInput) -> Result<&Fields> {
    match &input.data {
        Data::Struct(data) => Ok(&data.fields),
        Data::Enum(_) => Err(syn::Error::new_spanned(
            &input.ident,
            "PlainPixel can only be derived for structs, not enums",
        )),
        Data::Union(_) => Err(syn::Error::new_spanned(
            &input.ident,
            "PlainPixel can only be derived for structs, not unions",
        )),
    }
}

enum Repr {
    C,
    Transparent,
}

/// Validates that the struct has `#[repr(C)]` attribute.
fn validate_repr(input: &DeriveInput, fields: &Fields) -> Result<Repr> {
    let has_repr_c = has_repr_c(&input.attrs);
    let has_repr_transparent = has_repr_transparent(&input.attrs);

    match (has_repr_c, has_repr_transparent) {
        (true, false) => {
            if fields.is_empty() {
                Err(syn::Error::new_spanned(
                    &input.ident,
                    "PlainPixel requires at least one field",
                ))
            } else {
                Ok(Repr::C)
            }
        }
        (false, true) => {
            if fields.len() != 1 {
                Err(syn::Error::new_spanned(
                    &input.ident,
                    "PlainPixel requires exactly one field when #[repr(transparent)] is used",
                ))
            } else {
                Ok(Repr::Transparent)
            }
        }
        (true, true) => Err(syn::Error::new_spanned(
            &input.ident,
            "PlainPixel cannot be derived for structs with both #[repr(C)] and #[repr(transparent)] attributes",
        )),
        (false, false) => Err(syn::Error::new_spanned(
            &input.ident,
            "PlainPixel requires #[repr(C)] or #[repr(transparent)] attribute",
        )),
    }
}

fn has_repr_c(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path().is_ident("repr")
            && matches!(&attr.meta, syn::Meta::List(ml) if ml.tokens.to_string().contains("C"))
    })
}

fn has_repr_transparent(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path().is_ident("repr")
            && matches!(&attr.meta, syn::Meta::List(ml) if ml.tokens.to_string().contains("transparent"))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use syn::DeriveInput;

    #[test]
    fn test_validate_struct_with_named_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        assert!(validate_struct(&input).is_ok());
    }

    #[test]
    fn test_validate_struct_with_tuple_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel(u8, u8, u8);
        };
        assert!(validate_struct(&input).is_ok());
    }

    #[test]
    fn test_validate_struct_rejects_enum() {
        let input: DeriveInput = syn::parse_quote! {
            enum BadPixel {
                Red,
            }
        };
        let result = validate_struct(&input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not enums"));
    }

    #[test]
    fn test_validate_struct_rejects_union() {
        let input: DeriveInput = syn::parse_quote! {
            union BadPixel {
                a: u8,
                b: u16,
            }
        };
        let result = validate_struct(&input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not unions"));
    }

    #[test]
    fn test_validate_repr_c() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        assert!(matches!(validate_repr(&input, fields), Ok(Repr::C)));
    }

    #[test]
    fn test_validate_repr_transparent() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel {
                value: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        assert!(matches!(
            validate_repr(&input, fields),
            Ok(Repr::Transparent)
        ));
    }

    #[test]
    fn test_validate_repr_missing() {
        let input: DeriveInput = syn::parse_quote! {
            struct TestPixel {
                r: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("requires #[repr(C)]")
        );
    }

    #[test]
    fn test_validate_repr_c_no_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {}
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("at least one field")
        );
    }

    #[test]
    fn test_validate_repr_both_c_and_transparent() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            #[repr(transparent)]
            struct TestPixel {
                value: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("both"));
    }

    #[test]
    fn test_validate_repr_transparent_multiple_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel {
                a: u8,
                b: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let result = validate_repr(&input, fields);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("exactly one field")
        );
    }

    #[test]
    fn test_generate_channels_repr_c() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct TestPixel {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let tokens = generate_channels_constant(fields, &Repr::C).unwrap();
        let output = tokens.to_string();
        // The derive resolves per-field `SIZE` through the
        // byte-layout trait `PlainChannel` (not the pixel-role trait
        // `PlainPixel`), so that float-channelled pixels like
        // `MonoF32` / `RgbF32` can be derived even though `f32` /
        // `f64` are not `PlainPixel`.
        assert!(output.contains("PlainChannel"));
        assert!(output.contains("SIZE"));
    }

    #[test]
    fn test_generate_channels_repr_transparent() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct TestPixel {
                value: u8,
            }
        };
        let fields = validate_struct(&input).unwrap();
        let tokens = generate_channels_constant(fields, &Repr::Transparent).unwrap();
        let output = tokens.to_string();
        // Transparent wrappers emit `&[<InnerTy as PlainChannel>::SIZE]`
        // instead of inheriting `<InnerTy as PlainPixel>::CHANNELS`.
        // This works for every existing transparent pixel (they all
        // wrap a single scalar channel) and unblocks `MonoF32(f32)` /
        // `MonoF64(f64)`, where the inner float is a `PlainChannel`
        // but not a `PlainPixel`.
        assert!(output.contains("PlainChannel"));
        assert!(output.contains("SIZE"));
    }

    #[test]
    fn test_derive_repr_c_full() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Rgb8 {
                r: u8,
                g: u8,
                b: u8,
            }
        };
        let tokens = derive(input).unwrap();
        let output = tokens.to_string();
        assert!(output.contains("PlainPixel"));
        assert!(output.contains("CHANNELS"));
        assert!(
            output.contains("_ASSERT_SIZE"),
            "must force-evaluate _ASSERT_SIZE"
        );
        assert!(
            output.contains("_ASSERT_CHANNELS"),
            "must force-evaluate _ASSERT_CHANNELS"
        );
    }

    #[test]
    fn test_derive_repr_transparent_full() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct Wrapped {
                value: u8,
            }
        };
        let tokens = derive(input).unwrap();
        let output = tokens.to_string();
        assert!(output.contains("PlainPixel"));
        assert!(output.contains("CHANNELS"));
        assert!(
            output.contains("_ASSERT_SIZE"),
            "must force-evaluate _ASSERT_SIZE"
        );
        assert!(
            output.contains("_ASSERT_CHANNELS"),
            "must force-evaluate _ASSERT_CHANNELS"
        );
    }

    #[test]
    fn test_has_repr_c_true() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Foo { x: u8 }
        };
        assert!(has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_has_repr_transparent_true() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(transparent)]
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_has_repr_neither() {
        let input: DeriveInput = syn::parse_quote! {
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    #[test]
    fn test_has_repr_with_non_repr_attr() {
        // A non-repr attribute exercises the `false` branch inside the `.any()` closure
        let input: DeriveInput = syn::parse_quote! {
            #[derive(Clone)]
            struct Foo { x: u8 }
        };
        assert!(!has_repr_c(&input.attrs));
        assert!(!has_repr_transparent(&input.attrs));
    }

    // -----------------------------------------------------------------------
    // derive() error-path tests — exercise the `?` branches inside derive()
    // -----------------------------------------------------------------------

    #[test]
    fn test_derive_rejects_enum() {
        let input: DeriveInput = syn::parse_quote! {
            enum BadPixel { Red }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not enums"));
    }

    #[test]
    fn test_derive_rejects_union() {
        let input: DeriveInput = syn::parse_quote! {
            union BadPixel { a: u8, b: u16 }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("not unions"));
    }

    #[test]
    fn test_derive_rejects_missing_repr() {
        let input: DeriveInput = syn::parse_quote! {
            struct TestPixel { r: u8 }
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("requires #[repr(C)]")
        );
    }

    #[test]
    fn test_derive_rejects_repr_c_no_fields() {
        let input: DeriveInput = syn::parse_quote! {
            #[repr(C)]
            struct Empty {}
        };
        let result = derive(input);
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("at least one field")
        );
    }
}