|
3 | 3 |
|
4 | 4 | #![allow(unsafe_code, reason = "unsafe is required for bytemuck unsafe impls")] |
5 | 5 |
|
6 | | -use crate::{Compose, Extend, Fill, ImageFormat, ImageQuality, Mix}; |
| 6 | +use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix}; |
7 | 7 |
|
8 | 8 | // Safety: The enum is `repr(u8)` and has only fieldless variants. |
9 | 9 | unsafe impl bytemuck::NoUninit for Compose {} |
@@ -109,6 +109,31 @@ unsafe impl bytemuck::Contiguous for ImageFormat { |
109 | 109 | const MAX_VALUE: u8 = Self::Rgba8 as u8; |
110 | 110 | } |
111 | 111 |
|
| 112 | +// Safety: The enum is `repr(u8)` and has only fieldless variants. |
| 113 | +unsafe impl bytemuck::NoUninit for ImageAlphaType {} |
| 114 | + |
| 115 | +// Safety: The enum is `repr(u8)` and `0` is a valid value. |
| 116 | +unsafe impl bytemuck::Zeroable for ImageAlphaType {} |
| 117 | + |
| 118 | +// Safety: The enum is `repr(u8)`. |
| 119 | +unsafe impl bytemuck::checked::CheckedBitPattern for ImageAlphaType { |
| 120 | + type Bits = u8; |
| 121 | + |
| 122 | + fn is_valid_bit_pattern(bits: &u8) -> bool { |
| 123 | + use bytemuck::Contiguous; |
| 124 | + // Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE. |
| 125 | + *bits <= Self::MAX_VALUE |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Safety: The enum is `repr(u8)`. All values are `u8` and fall within |
| 130 | +// the min and max values. |
| 131 | +unsafe impl bytemuck::Contiguous for ImageAlphaType { |
| 132 | + type Int = u8; |
| 133 | + const MIN_VALUE: u8 = Self::Alpha as u8; |
| 134 | + const MAX_VALUE: u8 = Self::AlphaPremultiplied as u8; |
| 135 | +} |
| 136 | + |
112 | 137 | // Safety: The enum is `repr(u8)` and has only fieldless variants. |
113 | 138 | unsafe impl bytemuck::NoUninit for ImageQuality {} |
114 | 139 |
|
@@ -151,7 +176,7 @@ unsafe impl bytemuck::checked::CheckedBitPattern for Mix { |
151 | 176 |
|
152 | 177 | #[cfg(test)] |
153 | 178 | mod tests { |
154 | | - use crate::{Compose, Extend, Fill, ImageFormat, ImageQuality, Mix}; |
| 179 | + use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix}; |
155 | 180 | use bytemuck::{checked::try_from_bytes, Contiguous, Zeroable}; |
156 | 181 | use core::ptr; |
157 | 182 |
|
|
0 commit comments