| 1 | #[cfg(test)] |
| 2 | macro_rules! raw_pixel_conversion_tests { |
| 3 | ($name: ident <$($ty_param: path),*> : $($component: ident),+) => { |
| 4 | #[test] |
| 5 | fn convert_from_f32_array() { |
| 6 | raw_pixel_conversion_tests!(@float_array_test f32, $name<$($ty_param),*>: $($component),+); |
| 7 | } |
| 8 | |
| 9 | #[test] |
| 10 | fn convert_from_f64_array() { |
| 11 | raw_pixel_conversion_tests!(@float_array_test f64, $name<$($ty_param),*>: $($component),+); |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn convert_from_f32_slice() { |
| 16 | raw_pixel_conversion_tests!(@float_slice_test f32, $name<$($ty_param),*>: $($component),+); |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn convert_from_f64_slice() { |
| 21 | raw_pixel_conversion_tests!(@float_slice_test f64, $name<$($ty_param),*>: $($component),+); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | (@float_array_test $float: ty, $name: ident <$($ty_param: path),*> : $($component: ident),+) => { |
| 26 | use crate::cast::ArrayCast; |
| 27 | use crate::Alpha; |
| 28 | |
| 29 | let mut counter: $float = 0.0; |
| 30 | $( |
| 31 | counter += 0.1; |
| 32 | let $component = counter; |
| 33 | )+ |
| 34 | let alpha = counter + 0.1; |
| 35 | |
| 36 | let raw: <$name<$($ty_param,)* $float> as ArrayCast>::Array = [$($component),+]; |
| 37 | let raw_plus_1: <Alpha<$name<$($ty_param,)* $float>, $float> as ArrayCast>::Array = [ |
| 38 | $($component,)+ |
| 39 | alpha |
| 40 | ]; |
| 41 | let color: $name<$($ty_param,)* $float> = crate::cast::from_array(raw); |
| 42 | |
| 43 | let color_alpha: Alpha<$name<$($ty_param,)* $float>, $float> = crate::cast::from_array(raw_plus_1); |
| 44 | |
| 45 | assert_eq!(color, $name::new($($component),+)); |
| 46 | |
| 47 | assert_eq!(color_alpha, Alpha::<$name<$($ty_param,)* $float>, $float>::new($($component,)+ alpha)); |
| 48 | }; |
| 49 | |
| 50 | (@float_slice_test $float: ty, $name: ident <$($ty_param: path),*> : $($component: ident),+) => { |
| 51 | use core::convert::{TryInto, TryFrom}; |
| 52 | use crate::Alpha; |
| 53 | |
| 54 | let mut counter: $float = 0.0; |
| 55 | $( |
| 56 | counter += 0.1; |
| 57 | let $component = counter; |
| 58 | )+ |
| 59 | let alpha = counter + 0.1; |
| 60 | let extra = counter + 0.2; |
| 61 | let raw: &[$float] = &[$($component),+]; |
| 62 | let raw_plus_1: &[$float] = &[ |
| 63 | $($component,)+ |
| 64 | alpha |
| 65 | ]; |
| 66 | let raw_plus_2: &[$float] = &[ |
| 67 | $($component,)+ |
| 68 | alpha, |
| 69 | extra |
| 70 | ]; |
| 71 | let color: &$name<$($ty_param,)* $float> = raw.try_into().unwrap(); |
| 72 | assert!(<&$name<$($ty_param,)* $float>>::try_from(raw_plus_1).is_err()); |
| 73 | |
| 74 | let color_alpha: &Alpha<$name<$($ty_param,)* $float>, $float> = raw_plus_1.try_into().unwrap(); |
| 75 | assert!(<&Alpha<$name<$($ty_param,)* $float>, $float>>::try_from(raw_plus_2).is_err()); |
| 76 | |
| 77 | assert_eq!(color, &$name::new($($component),+)); |
| 78 | |
| 79 | assert_eq!(color_alpha, &Alpha::<$name<$($ty_param,)* $float>, $float>::new($($component,)+ alpha)); |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | #[cfg(test)] |
| 84 | macro_rules! raw_pixel_conversion_fail_tests { |
| 85 | ($name: ident <$($ty_param: path),*> : $($component: ident),+) => { |
| 86 | #[test] |
| 87 | #[should_panic(expected = "TryFromSliceError")] |
| 88 | fn convert_from_short_f32_slice() { |
| 89 | raw_pixel_conversion_fail_tests!(@float_slice_test f32, $name<$($ty_param),*>); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | #[should_panic(expected = "TryFromSliceError")] |
| 94 | fn convert_from_short_f64_slice() { |
| 95 | raw_pixel_conversion_fail_tests!(@float_slice_test f64, $name<$($ty_param),*>); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | (@float_slice_test $float: ty, $name: ident <$($ty_param: path),*>) => { |
| 100 | use core::convert::TryInto; |
| 101 | |
| 102 | let raw: &[$float] = &[0.1]; |
| 103 | let _: &$name<$($ty_param,)* $float> = raw.try_into().unwrap(); |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | macro_rules! impl_array_casts { |
| 108 | ($self_ty: ident < $($ty_param: ident),+ > $($rest: tt)*) => { |
| 109 | impl_array_casts!([$($ty_param),+] $self_ty < $($ty_param),+ > $($rest)*); |
| 110 | }; |
| 111 | ([$($ty_param: tt)+] $self_ty: ident < $($self_ty_param: ty),+ > , [$array_item: ty; $array_len: expr] $(, where $($where: tt)+)?) => { |
| 112 | impl<$($ty_param)+> AsRef<[$array_item; $array_len]> for $self_ty<$($self_ty_param),+> |
| 113 | $(where $($where)+)? |
| 114 | { |
| 115 | #[inline] |
| 116 | fn as_ref(&self) -> &[$array_item; $array_len] { |
| 117 | crate::cast::into_array_ref(self) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl<$($ty_param)+> AsRef<$self_ty<$($self_ty_param),+>> for [$array_item; $array_len] |
| 122 | $(where $($where)+)? |
| 123 | { |
| 124 | #[inline] |
| 125 | fn as_ref(&self) -> &$self_ty<$($self_ty_param),+> { |
| 126 | crate::cast::from_array_ref(self) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl<$($ty_param)+> AsMut<[$array_item; $array_len]> for $self_ty<$($self_ty_param),+> |
| 131 | $(where $($where)+)? |
| 132 | { |
| 133 | #[inline] |
| 134 | fn as_mut(&mut self) -> &mut [$array_item; $array_len] { |
| 135 | crate::cast::into_array_mut(self) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl<$($ty_param)+> AsMut<$self_ty<$($self_ty_param),+>> for [$array_item; $array_len] |
| 140 | $(where $($where)+)? |
| 141 | { |
| 142 | #[inline] |
| 143 | fn as_mut(&mut self) -> &mut $self_ty<$($self_ty_param),+> { |
| 144 | crate::cast::from_array_mut(self) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<$($ty_param)+> AsRef<[$array_item]> for $self_ty<$($self_ty_param),+> |
| 149 | $(where $($where)+)? |
| 150 | { |
| 151 | #[inline] |
| 152 | fn as_ref(&self) -> &[$array_item] { |
| 153 | &*AsRef::<[$array_item; $array_len]>::as_ref(self) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | impl<$($ty_param)+> AsMut<[$array_item]> for $self_ty<$($self_ty_param),+> |
| 158 | $(where $($where)+)? |
| 159 | { |
| 160 | #[inline] |
| 161 | fn as_mut(&mut self) -> &mut [$array_item] { |
| 162 | &mut *AsMut::<[$array_item; $array_len]>::as_mut(self) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | impl<$($ty_param)+> From<$self_ty<$($self_ty_param),+>> for [$array_item; $array_len] |
| 167 | $(where $($where)+)? |
| 168 | { |
| 169 | #[inline] |
| 170 | fn from(color: $self_ty<$($self_ty_param),+>) -> Self { |
| 171 | crate::cast::into_array(color) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl<$($ty_param)+> From<[$array_item; $array_len]> for $self_ty<$($self_ty_param),+> |
| 176 | $(where $($where)+)? |
| 177 | { |
| 178 | #[inline] |
| 179 | fn from(array: [$array_item; $array_len]) -> Self { |
| 180 | crate::cast::from_array(array) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | impl<'a, $($ty_param)+> From<&'a $self_ty<$($self_ty_param),+>> for &'a [$array_item; $array_len] |
| 185 | $(where $($where)+)? |
| 186 | { |
| 187 | #[inline] |
| 188 | fn from(color: &'a $self_ty<$($self_ty_param),+>) -> Self { |
| 189 | color.as_ref() |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl<'a, $($ty_param)+> From<&'a [$array_item; $array_len]> for &'a $self_ty<$($self_ty_param),+> |
| 194 | $(where $($where)+)? |
| 195 | { |
| 196 | #[inline] |
| 197 | fn from(array: &'a [$array_item; $array_len]) -> Self{ |
| 198 | array.as_ref() |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl<'a, $($ty_param)+> From<&'a $self_ty<$($self_ty_param),+>> for &'a [$array_item] |
| 203 | $(where $($where)+)? |
| 204 | { |
| 205 | #[inline] |
| 206 | fn from(color: &'a $self_ty<$($self_ty_param),+>) -> Self { |
| 207 | color.as_ref() |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl<'a, $($ty_param)+> core::convert::TryFrom<&'a [$array_item]> for &'a $self_ty<$($self_ty_param),+> |
| 212 | $(where $($where)+)? |
| 213 | { |
| 214 | type Error = <&'a [$array_item; $array_len] as core::convert::TryFrom<&'a [$array_item]>>::Error; |
| 215 | |
| 216 | #[inline] |
| 217 | fn try_from(slice: &'a [$array_item]) -> Result<Self, Self::Error> { |
| 218 | use core::convert::TryInto; |
| 219 | |
| 220 | slice.try_into().map(crate::cast::from_array_ref) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl<'a, $($ty_param)+> From<&'a mut $self_ty<$($self_ty_param),+>> for &'a mut [$array_item; $array_len] |
| 225 | $(where $($where)+)? |
| 226 | { |
| 227 | #[inline] |
| 228 | fn from(color: &'a mut $self_ty<$($self_ty_param),+>) -> Self { |
| 229 | color.as_mut() |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | impl<'a, $($ty_param)+> From<&'a mut [$array_item; $array_len]> for &'a mut $self_ty<$($self_ty_param),+> |
| 234 | $(where $($where)+)? |
| 235 | { |
| 236 | #[inline] |
| 237 | fn from(array: &'a mut [$array_item; $array_len]) -> Self{ |
| 238 | array.as_mut() |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | impl<'a, $($ty_param)+> From<&'a mut $self_ty<$($self_ty_param),+>> for &'a mut [$array_item] |
| 243 | $(where $($where)+)? |
| 244 | { |
| 245 | #[inline] |
| 246 | fn from(color: &'a mut $self_ty<$($self_ty_param),+>) -> Self { |
| 247 | color.as_mut() |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | impl<'a, $($ty_param)+> core::convert::TryFrom<&'a mut [$array_item]> for &'a mut $self_ty<$($self_ty_param),+> |
| 252 | $(where $($where)+)? |
| 253 | { |
| 254 | type Error = <&'a mut [$array_item; $array_len] as core::convert::TryFrom<&'a mut [$array_item]>>::Error; |
| 255 | |
| 256 | #[inline] |
| 257 | fn try_from(slice: &'a mut [$array_item]) -> Result<Self, Self::Error> { |
| 258 | use core::convert::TryInto; |
| 259 | |
| 260 | slice.try_into().map(crate::cast::from_array_mut) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #[cfg(feature = "alloc")] |
| 265 | impl<$($ty_param)+> From<alloc::boxed::Box<$self_ty<$($self_ty_param),+>>> for alloc::boxed::Box<[$array_item; $array_len]> |
| 266 | $(where $($where)+)? |
| 267 | { |
| 268 | #[inline] |
| 269 | fn from(color: alloc::boxed::Box<$self_ty<$($self_ty_param),+>>) -> Self { |
| 270 | crate::cast::into_array_box(color) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | #[cfg(feature = "alloc")] |
| 275 | impl<$($ty_param)+> From<alloc::boxed::Box<[$array_item; $array_len]>> for alloc::boxed::Box<$self_ty<$($self_ty_param),+>> |
| 276 | $(where $($where)+)? |
| 277 | { |
| 278 | #[inline] |
| 279 | fn from(array: alloc::boxed::Box<[$array_item; $array_len]>) -> Self{ |
| 280 | crate::cast::from_array_box(array) |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | macro_rules! impl_uint_casts_self { |
| 287 | ($self_ty: ident < $($ty_param: ident),+ > $($rest: tt)*) => { |
| 288 | impl_uint_casts_self!([$($ty_param),+] $self_ty < $($ty_param),+ > $($rest)*); |
| 289 | }; |
| 290 | ([$($ty_param: tt)+] $self_ty: ident < $($self_ty_param: ty),+ >, $uint: ty $(, where $($where: tt)+)?) => { |
| 291 | impl<$($ty_param)+> AsRef<$uint> for $self_ty<$($self_ty_param),+> |
| 292 | $(where $($where)+)? |
| 293 | { |
| 294 | #[inline] |
| 295 | fn as_ref(&self) -> &$uint { |
| 296 | crate::cast::into_uint_ref(self) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | impl<$($ty_param)+> AsMut<$uint> for $self_ty<$($self_ty_param),+> |
| 301 | $(where $($where)+)? |
| 302 | { |
| 303 | #[inline] |
| 304 | fn as_mut(&mut self) -> &mut $uint { |
| 305 | crate::cast::into_uint_mut(self) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | impl<$($ty_param)+> From<$uint> for $self_ty<$($self_ty_param),+> |
| 310 | $(where $($where)+)? |
| 311 | { |
| 312 | #[inline] |
| 313 | fn from(uint: $uint) -> Self { |
| 314 | crate::cast::from_uint(uint) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | impl<'a, $($ty_param)+> From<&'a $uint> for &'a $self_ty<$($self_ty_param),+> |
| 319 | where |
| 320 | $uint: AsRef<$self_ty<$($self_ty_param),+>> $(, $($where)+)? |
| 321 | { |
| 322 | #[inline] |
| 323 | fn from(uint: &'a $uint) -> Self{ |
| 324 | uint.as_ref() |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | impl<'a, $($ty_param)+> From<&'a mut $uint> for &'a mut $self_ty<$($self_ty_param),+> |
| 329 | where |
| 330 | $uint: AsMut<$self_ty<$($self_ty_param),+>> $(, $($where)+)? |
| 331 | { |
| 332 | #[inline] |
| 333 | fn from(uint: &'a mut $uint) -> Self{ |
| 334 | uint.as_mut() |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | macro_rules! impl_uint_casts_other { |
| 341 | /* |
| 342 | ($self_ty: ident < $($ty_param: ident),+ > $($rest: tt)*) => { |
| 343 | impl_uint_casts_other!([$($ty_param),+] $self_ty < $($ty_param),+ > $($rest)*); |
| 344 | }; |
| 345 | */ |
| 346 | ([$($ty_param: ident),+] $self_ty: ident < $($self_ty_param: ty),+ >, $uint: ty $(, where $($where: tt)+)?) => { |
| 347 | impl<$($ty_param)+> AsRef<$self_ty<$($self_ty_param),+>> for $uint |
| 348 | $(where $($where)+)? |
| 349 | { |
| 350 | #[inline] |
| 351 | fn as_ref(&self) -> &$self_ty<$($self_ty_param),+> { |
| 352 | crate::cast::from_uint_ref(self) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | impl<$($ty_param)+> AsMut<$self_ty<$($self_ty_param),+>> for $uint |
| 357 | $(where $($where)+)? |
| 358 | { |
| 359 | #[inline] |
| 360 | fn as_mut(&mut self) -> &mut $self_ty<$($self_ty_param),+> { |
| 361 | crate::cast::from_uint_mut(self) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | impl<$($ty_param)+> From<$self_ty<$($self_ty_param),+>> for $uint |
| 366 | $(where $($where)+)? |
| 367 | { |
| 368 | #[inline] |
| 369 | fn from(color: $self_ty<$($self_ty_param),+>) -> Self { |
| 370 | crate::cast::into_uint(color) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | impl<'a, $($ty_param)+> From<&'a $self_ty<$($self_ty_param),+>> for &'a $uint |
| 375 | $(where $($where)+)? |
| 376 | { |
| 377 | #[inline] |
| 378 | fn from(color: &'a $self_ty<$($self_ty_param),+>) -> Self { |
| 379 | color.as_ref() |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
| 384 | impl<'a, $($ty_param)+> From<&'a mut $self_ty<$($self_ty_param),+>> for &'a mut $uint |
| 385 | $(where $($where)+)? |
| 386 | { |
| 387 | #[inline] |
| 388 | fn from(color: &'a mut $self_ty<$($self_ty_param),+>) -> Self { |
| 389 | color.as_mut() |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |