peniko 0.6.0

Primitive types for styling vector graphics
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
// Copyright 2024 the Peniko Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![allow(unsafe_code, reason = "unsafe is required for bytemuck unsafe impls")]

use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix};

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for Compose {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for Compose {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for Compose {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for Compose {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Clear as u8;
    const MAX_VALUE: u8 = Self::PlusLighter as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for Extend {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for Extend {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for Extend {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for Extend {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Pad as u8;
    const MAX_VALUE: u8 = Self::Reflect as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for Fill {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for Fill {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for Fill {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for Fill {
    type Int = u8;
    const MIN_VALUE: u8 = Self::NonZero as u8;
    const MAX_VALUE: u8 = Self::EvenOdd as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for ImageAlphaType {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for ImageAlphaType {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for ImageAlphaType {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for ImageAlphaType {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Alpha as u8;
    const MAX_VALUE: u8 = Self::AlphaPremultiplied as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for ImageFormat {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for ImageFormat {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for ImageFormat {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for ImageFormat {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Rgba8 as u8;
    const MAX_VALUE: u8 = Self::Bgra8 as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for ImageQuality {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for ImageQuality {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for ImageQuality {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for ImageQuality {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Low as u8;
    const MAX_VALUE: u8 = Self::High as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
unsafe impl bytemuck::NoUninit for Mix {}

// Safety: The enum is `repr(u8)` and `0` is a valid value.
unsafe impl bytemuck::Zeroable for Mix {}

// Safety: The enum is `repr(u8)`.
unsafe impl bytemuck::checked::CheckedBitPattern for Mix {
    type Bits = u8;

    fn is_valid_bit_pattern(bits: &u8) -> bool {
        use bytemuck::Contiguous;
        // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
        *bits <= Self::MAX_VALUE
    }
}

// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
// the min and max values.
unsafe impl bytemuck::Contiguous for Mix {
    type Int = u8;
    const MIN_VALUE: u8 = Self::Normal as u8;
    const MAX_VALUE: u8 = Self::Luminosity as u8;
}

#[cfg(test)]
mod tests {
    use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix};
    use bytemuck::{Contiguous, Zeroable, checked::try_from_bytes};
    use core::ptr;

    #[test]
    fn checked_bit_pattern() {
        let valid_zero = bytemuck::bytes_of(&0_u8);
        let valid_one = bytemuck::bytes_of(&1_u8);
        let invalid = bytemuck::bytes_of(&200_u8);

        assert_eq!(Ok(&Compose::Copy), try_from_bytes::<Compose>(valid_one));
        assert!(try_from_bytes::<Compose>(invalid).is_err());

        assert_eq!(Ok(&Extend::Repeat), try_from_bytes::<Extend>(valid_one));
        assert!(try_from_bytes::<Extend>(invalid).is_err());

        assert_eq!(Ok(&Fill::EvenOdd), try_from_bytes::<Fill>(valid_one));
        assert!(try_from_bytes::<Fill>(invalid).is_err());

        assert_eq!(
            Ok(&ImageAlphaType::Alpha),
            try_from_bytes::<ImageAlphaType>(valid_zero)
        );
        assert_eq!(
            Ok(&ImageAlphaType::AlphaPremultiplied),
            try_from_bytes::<ImageAlphaType>(valid_one)
        );
        assert!(try_from_bytes::<ImageAlphaType>(invalid).is_err());

        assert_eq!(
            Ok(&ImageFormat::Rgba8),
            try_from_bytes::<ImageFormat>(valid_zero)
        );
        assert_eq!(
            Ok(&ImageFormat::Bgra8),
            try_from_bytes::<ImageFormat>(valid_one)
        );
        assert!(try_from_bytes::<ImageFormat>(invalid).is_err());

        assert_eq!(
            Ok(&ImageQuality::Medium),
            try_from_bytes::<ImageQuality>(valid_one)
        );
        assert!(try_from_bytes::<ImageQuality>(invalid).is_err());

        assert_eq!(Ok(&Mix::Multiply), try_from_bytes::<Mix>(valid_one));
        assert!(try_from_bytes::<Mix>(invalid).is_err());
    }

    #[test]
    fn contiguous() {
        let compose1 = Compose::PlusLighter;
        let compose2 = Compose::from_integer(compose1.into_integer());
        assert_eq!(Some(compose1), compose2);

        assert_eq!(None, Compose::from_integer(255));

        let extend1 = Extend::Repeat;
        let extend2 = Extend::from_integer(extend1.into_integer());
        assert_eq!(Some(extend1), extend2);

        assert_eq!(None, Extend::from_integer(255));

        let fill1 = Fill::EvenOdd;
        let fill2 = Fill::from_integer(fill1.into_integer());
        assert_eq!(Some(fill1), fill2);

        assert_eq!(None, Fill::from_integer(255));

        let image_alpha_type_1 = ImageAlphaType::Alpha;
        let image_alpha_type_2 = ImageAlphaType::from_integer(image_alpha_type_1.into_integer());
        assert_eq!(Some(image_alpha_type_1), image_alpha_type_2);

        assert_eq!(None, ImageAlphaType::from_integer(255));

        let image_format_1 = ImageFormat::Rgba8;
        let image_format_2 = ImageFormat::from_integer(image_format_1.into_integer());
        assert_eq!(Some(image_format_1), image_format_2);

        assert_eq!(None, ImageFormat::from_integer(255));

        let image_quality_1 = ImageQuality::Low;
        let image_quality_2 = ImageQuality::from_integer(image_quality_1.into_integer());
        assert_eq!(Some(image_quality_1), image_quality_2);

        assert_eq!(None, ImageQuality::from_integer(255));

        let mix_1 = Mix::Multiply;
        let mix_2 = Mix::from_integer(mix_1.into_integer());
        assert_eq!(Some(mix_1), mix_2);

        assert_eq!(None, Mix::from_integer(255));
    }

    #[test]
    fn zeroable() {
        let compose = Compose::zeroed();
        assert_eq!(compose, Compose::Clear);

        let extend = Extend::zeroed();
        assert_eq!(extend, Extend::Pad);

        let fill = Fill::zeroed();
        assert_eq!(fill, Fill::NonZero);

        let image_alpha_type = ImageAlphaType::zeroed();
        assert_eq!(image_alpha_type, ImageAlphaType::Alpha);

        let image_format = ImageFormat::zeroed();
        assert_eq!(image_format, ImageFormat::Rgba8);

        let image_quality = ImageQuality::zeroed();
        assert_eq!(image_quality, ImageQuality::Low);

        let mix = Mix::zeroed();
        assert_eq!(mix, Mix::Normal);
    }

    /// Tests that the [`Contiguous`] impl for [`Compose`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= Compose::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid Compose, that will be detected.
            let it: Compose = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`Extend`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= Extend::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid Extend, that will be detected.
            let it: Extend = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`Fill`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= Fill::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid Fill, that will be detected.
            let it: Fill = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`ImageAlphaType`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= ImageAlphaType::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid ImageFormat, that will be detected.
            let it: ImageAlphaType = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`ImageFormat`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= ImageFormat::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid ImageFormat, that will be detected.
            let it: ImageFormat = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`ImageQuality`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= ImageQuality::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid ImageQuality, that will be detected.
            let it: ImageQuality = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };

    /// Tests that the [`Contiguous`] impl for [`Mix`] is not trivially incorrect.
    const _: () = {
        let mut value = 0;
        while value <= ImageQuality::MAX_VALUE {
            // Safety: In a const context, therefore if this makes an invalid ImageQuality, that will be detected.
            let it: ImageQuality = unsafe { ptr::read((&raw const value).cast()) };
            // Evaluate the enum value to ensure it actually has a valid tag
            if it as u8 != value {
                unreachable!();
            }
            value += 1;
        }
    };
}

#[cfg(doctest)]
/// Doctests aren't collected under `cfg(test)`; we can use `cfg(doctest)` instead
mod doctests {
    /// Validates that any new variants in `Compose` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::Compose;
    /// const {
    ///     let value = Compose::MAX_VALUE + 1;
    ///     // Safety: In a const context, therefore if this makes an invalid Compose, that will be detected.
    ///     // (Indeed, we rely upon that)
    ///     let it: Compose = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _COMPOSE: () = {};

    /// Validates that any new variants in `Extend` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::Extend;
    /// const {
    ///     let value = Extend::MAX_VALUE + 1;
    ///     let it: Extend = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _EXTEND: () = {};

    /// Validates that any new variants in `Fill` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::Fill;
    /// const {
    ///     let value = Fill::MAX_VALUE + 1;
    ///     let it: Fill = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _FILL: () = {};

    /// Validates that any new variants in `ImageAlphaType` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::ImageAlphaType;
    /// const {
    ///     let value = ImageAlphaType::MAX_VALUE + 1;
    ///     let it: ImageAlphaType = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _IMAGE_ALPHA_TYPE: () = {};

    /// Validates that any new variants in `ImageFormat` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::ImageFormat;
    /// const {
    ///     let value = ImageFormat::MAX_VALUE + 1;
    ///     let it: ImageFormat = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _IMAGE_FORMAT: () = {};

    /// Validates that any new variants in `ImageQuality` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::ImageQuality;
    /// const {
    ///     let value = ImageQuality::MAX_VALUE + 1;
    ///     let it: ImageQuality = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _IMAGE_QUALITY: () = {};

    /// Validates that any new variants in `Mix` has led to a change in the `Contiguous` impl.
    /// Note that to test this robustly, we'd need 256 tests, which is impractical.
    /// We make the assumption that all new variants will maintain contiguousness.
    ///
    /// ```compile_fail,E0080
    /// use bytemuck::Contiguous;
    /// use peniko::Mix;
    /// const {
    ///     let value = Mix::MAX_VALUE + 1;
    ///     let it: Mix = unsafe { core::ptr::read((&raw const value).cast()) };
    ///     // Evaluate the enum value to ensure it actually has an invalid tag
    ///     if it as u8 != value {
    ///         unreachable!();
    ///     }
    /// }
    /// ```
    const _MIX: () = {};
}