From 10501db464ceb21a79b03fb81059198f16a36e33 Mon Sep 17 00:00:00 2001 From: Erik Hedvall Date: Sun, 18 Feb 2024 19:55:44 +0100 Subject: [PATCH 1/4] Avoid recursive trait resolution for IntoIterator --- palette/src/alpha/alpha.rs | 52 --- palette/src/hues.rs | 123 +++++- palette/src/macros/struct_of_arrays.rs | 506 +++++++++++++++++++++++-- 3 files changed, 596 insertions(+), 85 deletions(-) diff --git a/palette/src/alpha/alpha.rs b/palette/src/alpha/alpha.rs index e1e92351c..c75b80b51 100644 --- a/palette/src/alpha/alpha.rs +++ b/palette/src/alpha/alpha.rs @@ -864,58 +864,6 @@ where } } -impl IntoIterator for Alpha -where - C: IntoIterator, - A: IntoIterator, -{ - type Item = Alpha; - - type IntoIter = Iter; - - fn into_iter(self) -> Self::IntoIter { - Iter { - color: self.color.into_iter(), - alpha: self.alpha.into_iter(), - } - } -} - -impl<'a, C, A> IntoIterator for &'a Alpha -where - &'a C: IntoIterator, - &'a A: IntoIterator, -{ - type Item = Alpha<<&'a C as IntoIterator>::Item, <&'a A as IntoIterator>::Item>; - - type IntoIter = Iter<<&'a C as IntoIterator>::IntoIter, <&'a A as IntoIterator>::IntoIter>; - - fn into_iter(self) -> Self::IntoIter { - Iter { - color: (&self.color).into_iter(), - alpha: (&self.alpha).into_iter(), - } - } -} - -impl<'a, C, A> IntoIterator for &'a mut Alpha -where - &'a mut C: IntoIterator, - &'a mut A: IntoIterator, -{ - type Item = Alpha<<&'a mut C as IntoIterator>::Item, <&'a mut A as IntoIterator>::Item>; - - type IntoIter = - Iter<<&'a mut C as IntoIterator>::IntoIter, <&'a mut A as IntoIterator>::IntoIter>; - - fn into_iter(self) -> Self::IntoIter { - Iter { - color: (&mut self.color).into_iter(), - alpha: (&mut self.alpha).into_iter(), - } - } -} - /// An iterator for transparent colors. pub struct Iter { pub(crate) color: C, diff --git a/palette/src/hues.rs b/palette/src/hues.rs index 2fdfbcaef..371bf9a8f 100644 --- a/palette/src/hues.rs +++ b/palette/src/hues.rs @@ -582,9 +582,19 @@ macro_rules! make_hues { } } - impl IntoIterator for $name where C: IntoIterator { - type Item = $name; - type IntoIter = $iter_name; + impl IntoIterator for $name<[T; N]> { + type Item = $name; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name(IntoIterator::into_iter(self.0)) + } + } + + impl<'a, T> IntoIterator for $name<&'a [T]> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -592,9 +602,9 @@ macro_rules! make_hues { } } - impl<'a, C> IntoIterator for &'a $name where &'a C: IntoIterator { - type Item = $name<<&'a C as IntoIterator>::Item>; - type IntoIter = $iter_name<<&'a C as IntoIterator>::IntoIter>; + impl<'a, T> IntoIterator for $name<&'a mut [T]> { + type Item = $name<&'a mut T>; + type IntoIter = $iter_name>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -602,9 +612,30 @@ macro_rules! make_hues { } } - impl<'a, C> IntoIterator for &'a mut $name where &'a mut C: IntoIterator { - type Item = $name<<&'a mut C as IntoIterator>::Item>; - type IntoIter = $iter_name<<&'a mut C as IntoIterator>::IntoIter>; + #[cfg(feature = "alloc")] + impl IntoIterator for $name> { + type Item = $name; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name(self.0.into_iter()) + } + } + + impl<'a, T, const N: usize> IntoIterator for &'a $name<[T; N]> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&self.0).into_iter()) + } + } + + impl<'a, 'b, T> IntoIterator for &'a $name<&'b [T]> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -612,6 +643,80 @@ macro_rules! make_hues { } } + impl<'a, 'b, T> IntoIterator for &'a $name<&'b mut [T]> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&*self.0).into_iter()) + } + } + + #[cfg(feature = "alloc")] + impl<'a, T> IntoIterator for &'a $name> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&self.0).into_iter()) + } + } + + #[cfg(feature = "alloc")] + impl<'a, T> IntoIterator for &'a $name> { + type Item = $name<&'a T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&self.0).into_iter()) + } + } + + impl<'a, T, const N: usize> IntoIterator for &'a mut $name<[T; N]> { + type Item = $name<&'a mut T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&mut self.0).into_iter()) + } + } + + impl<'a, 'b, T> IntoIterator for &'a mut $name<&'b mut [T]> { + type Item = $name<&'a mut T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name(self.0.into_iter()) + } + } + + #[cfg(feature = "alloc")] + impl<'a, T> IntoIterator for &'a mut $name> { + type Item = $name<&'a mut T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&mut self.0).into_iter()) + } + } + + #[cfg(feature = "alloc")] + impl<'a, T> IntoIterator for &'a mut $name> { + type Item = $name<&'a mut T>; + type IntoIter = $iter_name>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + $iter_name((&mut *self.0).into_iter()) + } + } + #[doc = concat!("Iterator over [`", stringify!($name), "`] values.")] pub struct $iter_name(I); diff --git a/palette/src/macros/struct_of_arrays.rs b/palette/src/macros/struct_of_arrays.rs index 82200a617..5483fe745 100644 --- a/palette/src/macros/struct_of_arrays.rs +++ b/palette/src/macros/struct_of_arrays.rs @@ -46,13 +46,28 @@ macro_rules! impl_struct_of_array_traits { } } - impl<$($phantom_ty,)? C> IntoIterator for $self_ty<$($phantom_ty,)? C> + impl<$($phantom_ty,)? T, const N: usize> IntoIterator for $self_ty<$($phantom_ty,)? [T; N]> + { + type Item = $self_ty<$($phantom_ty,)? T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: IntoIterator::into_iter(self.$element),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a [T]> where - C: IntoIterator, + T: 'a, { - type Item = $self_ty<$($phantom_ty,)? C::Item>; + type Item = $self_ty<$($phantom_ty,)? &'a T>; - type IntoIter = Iter; + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -63,13 +78,63 @@ macro_rules! impl_struct_of_array_traits { } } - impl<'a, $($phantom_ty,)? C> IntoIterator for &'a $self_ty<$($phantom_ty,)? C> + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a mut [T]> where - &'a C: IntoIterator, + T: 'a, + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<$($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? alloc::vec::Vec> { - type Item = $self_ty<$($phantom_ty,)? <&'a C as IntoIterator>::Item>; + type Item = $self_ty<$($phantom_ty,)? T>; - type IntoIter = Iter<<&'a C as IntoIterator>::IntoIter $(,$phantom_ty)?>; + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<$($phantom_ty,)? C, T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + $self_ty<$($phantom_ty,)? C>: IntoIterator>, + C: IntoIterator, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; + + type IntoIter = crate::alpha::Iter<<$self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, C::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a $self_ty<$($phantom_ty,)? [T; N]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -80,13 +145,136 @@ macro_rules! impl_struct_of_array_traits { } } - impl<'a, $($phantom_ty,)? C> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? C> + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b [T]> where - &'a mut C: IntoIterator, + T: 'a { - type Item = $self_ty<$($phantom_ty,)? <&'a mut C as IntoIterator>::Item>; + type Item = $self_ty<$($phantom_ty,)? &'a T>; - type IntoIter = Iter<<&'a mut C as IntoIterator>::IntoIter $(,$phantom_ty)?>; + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b mut [T]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&*self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::vec::Vec> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + &'a $self_ty<$($phantom_ty,)? C>: IntoIterator>, + &'a C: IntoIterator, + T: 'a, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter<<&'a $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a C as IntoIterator>::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? [T; N]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&mut self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? &'b mut [T]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::vec::Vec> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -97,6 +285,42 @@ macro_rules! impl_struct_of_array_traits { } } + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + $($element: (&mut *self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + &'a mut $self_ty<$($phantom_ty,)? C>: IntoIterator>, + &'a mut C: IntoIterator, + T: 'a, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter<<&'a mut $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a mut C as IntoIterator>::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + #[doc = concat!("An iterator for [`", stringify!($self_ty), "`] values.")] pub struct Iter { $(pub(crate) $element: I,)+ @@ -212,13 +436,29 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<$($phantom_ty,)? C> IntoIterator for $self_ty<$($phantom_ty,)? C> + impl<$($phantom_ty,)? T, const N: usize> IntoIterator for $self_ty<$($phantom_ty,)? [T; N]> + { + type Item = $self_ty<$($phantom_ty,)? T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: self.hue.into_iter(), + $($element: IntoIterator::into_iter(self.$element),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a [T]> where - C: IntoIterator, + T: 'a, { - type Item = $self_ty<$($phantom_ty,)? C::Item>; + type Item = $self_ty<$($phantom_ty,)? &'a T>; - type IntoIter = Iter; + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -230,13 +470,65 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<'a, $($phantom_ty,)? C> IntoIterator for &'a $self_ty<$($phantom_ty,)? C> + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a mut [T]> where - &'a C: IntoIterator + 'a, + T: 'a, + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: self.hue.into_iter(), + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<$($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? alloc::vec::Vec> { - type Item = $self_ty<$($phantom_ty,)? <&'a C as IntoIterator>::Item>; + type Item = $self_ty<$($phantom_ty,)? T>; - type IntoIter = Iter<<&'a C as IntoIterator>::IntoIter $(,$phantom_ty)?>; + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: self.hue.into_iter(), + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<$($phantom_ty,)? C, T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + $self_ty<$($phantom_ty,)? C>: IntoIterator>, + C: IntoIterator, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; + + type IntoIter = crate::alpha::Iter<<$self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, C::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a $self_ty<$($phantom_ty,)? [T; N]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -248,13 +540,142 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<'a, $($phantom_ty,)? C> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? C> + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b [T]> where - &'a mut C: IntoIterator + 'a, + T: 'a { - type Item = $self_ty<$($phantom_ty,)? <&'a mut C as IntoIterator>::Item>; + type Item = $self_ty<$($phantom_ty,)? &'a T>; - type IntoIter = Iter<<&'a mut C as IntoIterator>::IntoIter $(,$phantom_ty)?>; + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: self.hue.into_iter(), + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b mut [T]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&self.hue).into_iter(), + $($element: (&*self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::vec::Vec> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&self.hue).into_iter(), + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&self.hue).into_iter(), + $($element: (&self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + &'a $self_ty<$($phantom_ty,)? C>: IntoIterator>, + &'a C: IntoIterator, + T: 'a, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter<<&'a $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a C as IntoIterator>::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? [T; N]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&mut self.hue).into_iter(), + $($element: (&mut self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? &'b mut [T]> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&mut self.hue).into_iter(), + $($element: self.$element.into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::vec::Vec> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { @@ -266,6 +687,43 @@ macro_rules! impl_struct_of_array_traits_hue { } } + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> + where + T: 'a + { + type Item = $self_ty<$($phantom_ty,)? &'a mut T>; + + type IntoIter = Iter $(,$phantom_ty)?>; + + #[inline(always)] + fn into_iter(self) -> Self::IntoIter { + Iter { + hue: (&mut self.hue).into_iter(), + $($element: (&mut *self.$element).into_iter(),)+ + $($phantom: core::marker::PhantomData)? + } + } + } + + impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> + where + &'a mut $self_ty<$($phantom_ty,)? C>: IntoIterator>, + &'a mut C: IntoIterator, + T: 'a, + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter<<&'a mut $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a mut C as IntoIterator>::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + #[doc = concat!("An iterator for [`", stringify!($self_ty), "`] values.")] pub struct Iter { pub(crate) hue: $hue_iter_ty, From 98b40cd443fc72c881eeb5d029d6a28a3bfbb927 Mon Sep 17 00:00:00 2001 From: Erik Hedvall Date: Mon, 19 Feb 2024 19:06:03 +0100 Subject: [PATCH 2/4] Add specific IntoIterator implementations for Alpha --- palette/src/macros/struct_of_arrays.rs | 382 ++++++++++++++++++++----- 1 file changed, 304 insertions(+), 78 deletions(-) diff --git a/palette/src/macros/struct_of_arrays.rs b/palette/src/macros/struct_of_arrays.rs index 5483fe745..ff6f8bb5d 100644 --- a/palette/src/macros/struct_of_arrays.rs +++ b/palette/src/macros/struct_of_arrays.rs @@ -61,9 +61,21 @@ macro_rules! impl_struct_of_array_traits { } } + impl<$($phantom_ty,)? T, const N: usize> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::array::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: IntoIterator::into_iter(self.alpha) + } + } + } + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a [T]> - where - T: 'a, { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -78,9 +90,21 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a [T]>, &'a [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a mut [T]> - where - T: 'a, { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -95,6 +119,20 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut [T]>, &'a mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<$($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? alloc::vec::Vec> { @@ -111,14 +149,12 @@ macro_rules! impl_struct_of_array_traits { } } - impl<$($phantom_ty,)? C, T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - $self_ty<$($phantom_ty,)? C>: IntoIterator>, - C: IntoIterator, + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; - type IntoIter = crate::alpha::Iter<<$self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, C::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, alloc::vec::IntoIter>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { @@ -129,8 +165,6 @@ macro_rules! impl_struct_of_array_traits { } impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a $self_ty<$($phantom_ty,)? [T; N]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -145,9 +179,21 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -162,9 +208,21 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b [T]>, &'b [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b mut [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -179,10 +237,22 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b mut [T]>, &'b mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&*self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::vec::Vec> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -197,10 +267,23 @@ macro_rules! impl_struct_of_array_traits { } } + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -215,15 +298,12 @@ macro_rules! impl_struct_of_array_traits { } } - impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - &'a $self_ty<$($phantom_ty,)? C>: IntoIterator>, - &'a C: IntoIterator, - T: 'a, + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>>, alloc::boxed::Box<[T]>> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; - type IntoIter = crate::alpha::Iter<<&'a $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a C as IntoIterator>::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { @@ -234,8 +314,6 @@ macro_rules! impl_struct_of_array_traits { } impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? [T; N]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -250,9 +328,21 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? &'b mut [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -267,10 +357,22 @@ macro_rules! impl_struct_of_array_traits { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b mut [T]>, &'b mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::vec::Vec> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -285,6 +387,21 @@ macro_rules! impl_struct_of_array_traits { } } + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> where @@ -303,20 +420,17 @@ macro_rules! impl_struct_of_array_traits { } } - impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - &'a mut $self_ty<$($phantom_ty,)? C>: IntoIterator>, - &'a mut C: IntoIterator, - T: 'a, + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>>, alloc::boxed::Box<[T]>> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; - type IntoIter = crate::alpha::Iter<<&'a mut $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a mut C as IntoIterator>::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { color: (&mut self.color).into_iter(), - alpha: (&mut self.alpha).into_iter(), + alpha: (&mut *self.alpha).into_iter(), } } } @@ -452,9 +566,21 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<$($phantom_ty,)? T, const N: usize> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::array::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: IntoIterator::into_iter(self.alpha) + } + } + } + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a [T]> - where - T: 'a, { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -470,9 +596,21 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a [T]>, &'a [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + impl<'a, $($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? &'a mut [T]> - where - T: 'a, { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -488,6 +626,20 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut [T]>, &'a mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<$($phantom_ty,)? T> IntoIterator for $self_ty<$($phantom_ty,)? alloc::vec::Vec> { @@ -505,14 +657,12 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<$($phantom_ty,)? C, T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - $self_ty<$($phantom_ty,)? C>: IntoIterator>, - C: IntoIterator, + #[cfg(feature = "alloc")] + impl<'a, $($phantom_ty,)? T> IntoIterator for crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? T>, T>; - type IntoIter = crate::alpha::Iter<<$self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, C::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, alloc::vec::IntoIter>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { @@ -523,8 +673,6 @@ macro_rules! impl_struct_of_array_traits_hue { } impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a $self_ty<$($phantom_ty,)? [T; N]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -540,9 +688,21 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -558,9 +718,21 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b [T]>, &'b [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: self.color.into_iter(), + alpha: self.alpha.into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? &'b mut [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -576,10 +748,22 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b mut [T]>, &'b mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&*self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::vec::Vec> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -595,10 +779,23 @@ macro_rules! impl_struct_of_array_traits_hue { } } + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&self.color).into_iter(), + alpha: (&self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a T>; @@ -614,15 +811,12 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - &'a $self_ty<$($phantom_ty,)? C>: IntoIterator>, - &'a C: IntoIterator, - T: 'a, + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>>, alloc::boxed::Box<[T]>> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a T>, &'a T>; - type IntoIter = crate::alpha::Iter<<&'a $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a C as IntoIterator>::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::Iter<'a, T>>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { @@ -633,8 +827,6 @@ macro_rules! impl_struct_of_array_traits_hue { } impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? [T; N]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -650,9 +842,21 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, $($phantom_ty,)? T, const N: usize> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? [T; N]>, [T; N]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? &'b mut [T]> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -668,10 +872,22 @@ macro_rules! impl_struct_of_array_traits_hue { } } + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'b mut [T]>, &'b mut [T]> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::vec::Vec> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -687,10 +903,23 @@ macro_rules! impl_struct_of_array_traits_hue { } } + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::vec::Vec>, alloc::vec::Vec> + { + type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; + + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; + + fn into_iter(self) -> Self::IntoIter { + crate::alpha::Iter { + color: (&mut self.color).into_iter(), + alpha: (&mut self.alpha).into_iter(), + } + } + } + #[cfg(feature = "alloc")] impl<'a, $($phantom_ty,)? T> IntoIterator for &'a mut $self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>> - where - T: 'a { type Item = $self_ty<$($phantom_ty,)? &'a mut T>; @@ -706,20 +935,17 @@ macro_rules! impl_struct_of_array_traits_hue { } } - impl<'a, $($phantom_ty,)? C, T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? C>, C> - where - &'a mut $self_ty<$($phantom_ty,)? C>: IntoIterator>, - &'a mut C: IntoIterator, - T: 'a, + #[cfg(feature = "alloc")] + impl<'a, 'b, $($phantom_ty,)? T> IntoIterator for &'a mut crate::alpha::Alpha<$self_ty<$($phantom_ty,)? alloc::boxed::Box<[T]>>, alloc::boxed::Box<[T]>> { type Item = crate::alpha::Alpha<$self_ty<$($phantom_ty,)? &'a mut T>, &'a mut T>; - type IntoIter = crate::alpha::Iter<<&'a mut $self_ty<$($phantom_ty,)? C> as IntoIterator>::IntoIter, <&'a mut C as IntoIterator>::IntoIter>; + type IntoIter = crate::alpha::Iter $(,$phantom_ty)?>, core::slice::IterMut<'a, T>>; fn into_iter(self) -> Self::IntoIter { crate::alpha::Iter { color: (&mut self.color).into_iter(), - alpha: (&mut self.alpha).into_iter(), + alpha: (&mut *self.alpha).into_iter(), } } } From 449d044c5bacabf7a206ddcb9b38a7afa063c375 Mon Sep 17 00:00:00 2001 From: Erik Hedvall Date: Sat, 24 Feb 2024 15:53:42 +0100 Subject: [PATCH 3/4] Add tests to type check IntoIterator implementations --- palette/src/hsl.rs | 20 +- palette/src/hsluv.rs | 20 +- palette/src/hsv.rs | 20 +- palette/src/hwb.rs | 20 +- palette/src/lab.rs | 20 +- palette/src/lch.rs | 20 +- palette/src/lchuv.rs | 20 +- palette/src/luma/luma.rs | 20 +- palette/src/luv.rs | 20 +- palette/src/macros/struct_of_arrays.rs | 322 ++++++++++++++++++++++++- palette/src/okhsl.rs | 20 +- palette/src/okhsv.rs | 20 +- palette/src/okhwb.rs | 20 +- palette/src/oklab.rs | 20 +- palette/src/oklch.rs | 20 +- palette/src/rgb/rgb.rs | 20 +- palette/src/xyz.rs | 20 +- palette/src/yxy.rs | 20 +- 18 files changed, 383 insertions(+), 279 deletions(-) diff --git a/palette/src/hsl.rs b/palette/src/hsl.rs index f478a88ba..50c0e0bc5 100644 --- a/palette/src/hsl.rs +++ b/palette/src/hsl.rs @@ -622,24 +622,12 @@ mod test { } struct_of_arrays_tests!( - Hsl, - Hsl::new(0.1f32, 0.2, 0.3), - Hsl::new(0.2, 0.3, 0.4), - Hsl::new(0.3, 0.4, 0.5) + Hsl[hue, saturation, lightness] phantom: standard, + super::Hsla::new(0.1f32, 0.2, 0.3, 0.4), + super::Hsla::new(0.2, 0.3, 0.4, 0.5), + super::Hsla::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{encoding::Srgb, hsl::Hsla}; - - struct_of_arrays_tests!( - Hsla, - Hsla::new(0.1f32, 0.2, 0.3, 0.4), - Hsla::new(0.2, 0.3, 0.4, 0.5), - Hsla::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/hsluv.rs b/palette/src/hsluv.rs index 13b90e8da..b025d13a6 100644 --- a/palette/src/hsluv.rs +++ b/palette/src/hsluv.rs @@ -345,24 +345,12 @@ mod test { } struct_of_arrays_tests!( - Hsluv, - Hsluv::new(0.1f32, 0.2, 0.3), - Hsluv::new(0.2, 0.3, 0.4), - Hsluv::new(0.3, 0.4, 0.5) + Hsluv[hue, saturation, l] phantom: white_point, + super::Hsluva::new(0.1f32, 0.2, 0.3, 0.4), + super::Hsluva::new(0.2, 0.3, 0.4, 0.5), + super::Hsluva::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{hsluv::Hsluva, white_point::D65}; - - struct_of_arrays_tests!( - Hsluva, - Hsluva::new(0.1f32, 0.2, 0.3, 0.4), - Hsluva::new(0.2, 0.3, 0.4, 0.5), - Hsluva::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/hsv.rs b/palette/src/hsv.rs index 1a5bb01f4..559676df7 100644 --- a/palette/src/hsv.rs +++ b/palette/src/hsv.rs @@ -629,24 +629,12 @@ mod test { } struct_of_arrays_tests!( - Hsv, - Hsv::new(0.1f32, 0.2, 0.3), - Hsv::new(0.2, 0.3, 0.4), - Hsv::new(0.3, 0.4, 0.5) + Hsv[hue, saturation, value] phantom: standard, + super::Hsva::new(0.1f32, 0.2, 0.3, 0.4), + super::Hsva::new(0.2, 0.3, 0.4, 0.5), + super::Hsva::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{encoding::Srgb, hsv::Hsva}; - - struct_of_arrays_tests!( - Hsva, - Hsva::new(0.1f32, 0.2, 0.3, 0.4), - Hsva::new(0.2, 0.3, 0.4, 0.5), - Hsva::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/hwb.rs b/palette/src/hwb.rs index 1e7d8df14..cb815e8cc 100644 --- a/palette/src/hwb.rs +++ b/palette/src/hwb.rs @@ -456,24 +456,12 @@ mod test { } struct_of_arrays_tests!( - Hwb, - Hwb::new(0.1f32, 0.2, 0.3), - Hwb::new(0.2, 0.3, 0.4), - Hwb::new(0.3, 0.4, 0.5) + Hwb[hue, whiteness, blackness] phantom: standard, + super::Hwba::new(0.1f32, 0.2, 0.3, 0.4), + super::Hwba::new(0.2, 0.3, 0.4, 0.5), + super::Hwba::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{encoding::Srgb, hwb::Hwba}; - - struct_of_arrays_tests!( - Hwba, - Hwba::new(0.1f32, 0.2, 0.3, 0.4), - Hwba::new(0.2, 0.3, 0.4, 0.5), - Hwba::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/lab.rs b/palette/src/lab.rs index 961bab216..f81febf85 100644 --- a/palette/src/lab.rs +++ b/palette/src/lab.rs @@ -445,24 +445,12 @@ mod test { } struct_of_arrays_tests!( - Lab, - Lab::new(0.1f32, 0.2, 0.3), - Lab::new(0.2, 0.3, 0.4), - Lab::new(0.3, 0.4, 0.5) + Lab[l, a, b] phantom: white_point, + super::Laba::new(0.1f32, 0.2, 0.3, 0.4), + super::Laba::new(0.2, 0.3, 0.4, 0.5), + super::Laba::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{lab::Laba, white_point::D65}; - - struct_of_arrays_tests!( - Laba, - Laba::new(0.1f32, 0.2, 0.3, 0.4), - Laba::new(0.2, 0.3, 0.4, 0.5), - Laba::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/lch.rs b/palette/src/lch.rs index c0c18807f..98d341d8b 100644 --- a/palette/src/lch.rs +++ b/palette/src/lch.rs @@ -455,24 +455,12 @@ mod test { } struct_of_arrays_tests!( - Lch, - Lch::new(0.1f32, 0.2, 0.3), - Lch::new(0.2, 0.3, 0.4), - Lch::new(0.3, 0.4, 0.5) + Lch[l, chroma, hue] phantom: white_point, + super::Lcha::new(0.1f32, 0.2, 0.3, 0.4), + super::Lcha::new(0.2, 0.3, 0.4, 0.5), + super::Lcha::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{lch::Lcha, white_point::D65}; - - struct_of_arrays_tests!( - Lcha, - Lcha::new(0.1f32, 0.2, 0.3, 0.4), - Lcha::new(0.2, 0.3, 0.4, 0.5), - Lcha::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/lchuv.rs b/palette/src/lchuv.rs index bf55999bd..4a3882646 100644 --- a/palette/src/lchuv.rs +++ b/palette/src/lchuv.rs @@ -317,24 +317,12 @@ mod test { } struct_of_arrays_tests!( - Lchuv, - Lchuv::new(0.1f32, 0.2, 0.3), - Lchuv::new(0.2, 0.3, 0.4), - Lchuv::new(0.3, 0.4, 0.5) + Lchuv[l, chroma, hue] phantom: white_point, + super::Lchuva::new(0.1f32, 0.2, 0.3, 0.4), + super::Lchuva::new(0.2, 0.3, 0.4, 0.5), + super::Lchuva::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{lchuv::Lchuva, white_point::D65}; - - struct_of_arrays_tests!( - Lchuva, - Lchuva::new(0.1f32, 0.2, 0.3, 0.4), - Lchuva::new(0.2, 0.3, 0.4, 0.5), - Lchuva::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/luma/luma.rs b/palette/src/luma/luma.rs index d04e0fc72..5b0d704c2 100644 --- a/palette/src/luma/luma.rs +++ b/palette/src/luma/luma.rs @@ -895,24 +895,12 @@ mod test { } struct_of_arrays_tests!( - Luma, - Luma::new(0.1f32), - Luma::new(0.2), - Luma::new(0.3) + Luma[luma] phantom: standard, + super::Lumaa::new(0.1f32, 0.4), + super::Lumaa::new(0.2, 0.5), + super::Lumaa::new(0.3, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{encoding::Srgb, luma::Lumaa}; - - struct_of_arrays_tests!( - Lumaa, - Lumaa::new(0.1f32, 0.4), - Lumaa::new(0.2, 0.5), - Lumaa::new(0.3, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/luv.rs b/palette/src/luv.rs index 8f5722e32..ec39a18a5 100644 --- a/palette/src/luv.rs +++ b/palette/src/luv.rs @@ -386,24 +386,12 @@ mod test { } struct_of_arrays_tests!( - Luv, - Luv::new(0.1f32, 0.2, 0.3), - Luv::new(0.2, 0.3, 0.4), - Luv::new(0.3, 0.4, 0.5) + Luv[l, u, v] phantom: white_point, + super::Luva::new(0.1f32, 0.2, 0.3, 0.4), + super::Luva::new(0.2, 0.3, 0.4, 0.5), + super::Luva::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{luv::Luva, white_point::D65}; - - struct_of_arrays_tests!( - Luva, - Luva::new(0.1f32, 0.2, 0.3, 0.4), - Luva::new(0.2, 0.3, 0.4, 0.5), - Luva::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/macros/struct_of_arrays.rs b/palette/src/macros/struct_of_arrays.rs index ff6f8bb5d..937b5656e 100644 --- a/palette/src/macros/struct_of_arrays.rs +++ b/palette/src/macros/struct_of_arrays.rs @@ -1441,23 +1441,48 @@ macro_rules! impl_struct_of_arrays_methods_hue { #[cfg(test)] macro_rules! struct_of_arrays_tests { - ($color_ty: ident $(<$phantom_ty:ident>)?, $($values:expr),+) => { + ($color_ty: ident $(<$phantom_ty:ty>)? [$($element: ident),+] $(phantom: $phantom: ident)?, $($values:expr),+) => { #[cfg(feature = "alloc")] #[test] fn collect() { - let vec_of_colors = vec![$($values),+]; + let vec_of_colors = vec![$($values.color),+]; let color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = vec_of_colors.into_iter().collect(); let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values.color),+]); + } + + #[cfg(feature = "alloc")] + #[test] + fn collect_alpha() { + let vec_of_colors = vec![$($values),+]; + let color_of_vecs: crate::alpha::Alpha<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>> = vec_of_colors.into_iter().collect(); + let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values),+]); } + #[cfg(feature = "alloc")] #[test] fn extend() { + let vec_of_colors = vec![$($values.color),+]; + + let mut color_of_vecs = $color_ty::<$($phantom_ty,)? Vec<_>>::with_capacity(vec_of_colors.len()); + color_of_vecs.extend(vec_of_colors); + + let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + + assert_eq!(vec_of_colors, vec![$($values.color),+]); + } + + + #[cfg(feature = "alloc")] + #[test] + fn extend_alpha() { let vec_of_colors = vec![$($values),+]; - let mut color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = $color_ty::with_capacity(vec_of_colors.len()); + let mut color_of_vecs = crate::alpha::Alpha::<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>>::with_capacity(vec_of_colors.len()); color_of_vecs.extend(vec_of_colors); let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); @@ -1465,10 +1490,11 @@ macro_rules! struct_of_arrays_tests { assert_eq!(vec_of_colors, vec![$($values),+]); } + #[cfg(feature = "alloc")] #[test] fn pop_push() { - let vec_of_colors = vec![$($values),+]; + let vec_of_colors = vec![$($values.color),+]; let mut color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = vec_of_colors.into_iter().collect(); let last = color_of_vecs.pop().unwrap(); @@ -1476,13 +1502,28 @@ macro_rules! struct_of_arrays_tests { let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values.color),+]); + } + + + #[cfg(feature = "alloc")] + #[test] + fn pop_push_alpha() { + let vec_of_colors = vec![$($values),+]; + + let mut color_of_vecs: crate::alpha::Alpha<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>> = vec_of_colors.into_iter().collect(); + let last = color_of_vecs.pop().unwrap(); + color_of_vecs.push(last); + + let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values),+]); } #[cfg(feature = "alloc")] #[test] fn clear() { - let vec_of_colors = vec![$($values),+]; + let vec_of_colors = vec![$($values.color),+]; let mut color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = vec_of_colors.into_iter().collect(); color_of_vecs.clear(); @@ -1494,14 +1535,43 @@ macro_rules! struct_of_arrays_tests { #[cfg(feature = "alloc")] #[test] - fn drain() { + fn clear_alpha() { let vec_of_colors = vec![$($values),+]; + let mut color_of_vecs: crate::alpha::Alpha<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>> = vec_of_colors.into_iter().collect(); + color_of_vecs.clear(); + + let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + + assert_eq!(vec_of_colors, vec![]); + } + + + #[cfg(feature = "alloc")] + #[test] + fn drain() { + let vec_of_colors = vec![$($values.color),+]; + let mut color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = vec_of_colors.into_iter().collect(); let vec_of_colors1: Vec<_> = color_of_vecs.drain(..).collect(); let vec_of_colors2: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors1, vec![$($values.color),+]); + assert_eq!(vec_of_colors2, vec![]); + } + + + #[cfg(feature = "alloc")] + #[test] + fn drain_alpha() { + let vec_of_colors = vec![$($values),+]; + + let mut color_of_vecs: crate::alpha::Alpha<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>> = vec_of_colors.into_iter().collect(); + + let vec_of_colors1: Vec<_> = color_of_vecs.drain(..).collect(); + let vec_of_colors2: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors1, vec![$($values),+]); assert_eq!(vec_of_colors2, vec![]); } @@ -1509,7 +1579,7 @@ macro_rules! struct_of_arrays_tests { #[cfg(feature = "alloc")] #[test] fn modify() { - let vec_of_colors = vec![$($values),+]; + let vec_of_colors = vec![$($values.color),+]; let mut color_of_vecs: $color_ty<$($phantom_ty,)? Vec<_>> = vec_of_colors.into_iter().collect(); @@ -1519,7 +1589,245 @@ macro_rules! struct_of_arrays_tests { let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values.color + 2.0),+]); + } + + #[cfg(feature = "alloc")] + #[test] + fn modify_alpha() { + let vec_of_colors = vec![$($values),+]; + + let mut color_of_vecs: crate::alpha::Alpha<$color_ty<$($phantom_ty,)? Vec<_>>, Vec<_>> = vec_of_colors.into_iter().collect(); + + for mut color in &mut color_of_vecs { + color.set(color.copied() + 2.0); + } + + let vec_of_colors: Vec<_> = color_of_vecs.into_iter().collect(); + assert_eq!(vec_of_colors, vec![$($values + 2.0),+]); } + + #[test] + fn into_iterator() { + fn expect_move(_: impl Iterator>){} + fn expect_ref<'a>(_: impl Iterator>){} + fn expect_ref_mut<'a>(_: impl Iterator>){} + + let arrays = $color_ty::<$($phantom_ty,)? [f32; 0]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let slices = $color_ty::<$($phantom_ty,)? &[f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let mut_slices = $color_ty::<$($phantom_ty,)? &mut [f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + + expect_move(arrays.into_iter()); + expect_ref(slices.into_iter()); + expect_ref_mut(mut_slices.into_iter()); + } + + #[test] + fn into_iterator_alpha() { + use crate::alpha::Alpha; + + fn expect_move(_: impl Iterator, f32>>){} + fn expect_ref<'a>(_: impl Iterator, &'a f32>>){} + fn expect_ref_mut<'a>(_: impl Iterator, &'a mut f32>>){} + + let arrays = Alpha::<_, [f32; 0]>{ + color: $color_ty::<$($phantom_ty,)? [f32; 0]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let slices = Alpha::<_, &[f32]>{ + color: $color_ty::<$($phantom_ty,)? &[f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let mut_slices = Alpha::<_, &mut [f32]>{ + color: $color_ty::<$($phantom_ty,)? &mut [f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + + expect_move(arrays.into_iter()); + expect_ref(slices.into_iter()); + expect_ref_mut(mut_slices.into_iter()); + } + + #[test] + fn into_iterator_ref() { + fn expect_ref<'a>(_: impl Iterator>){} + fn expect_ref_mut<'a>(_: impl Iterator>){} + + let mut arrays = $color_ty::<$($phantom_ty,)? [f32; 0]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let mut slices = $color_ty::<$($phantom_ty,)? &[f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let mut mut_slices = $color_ty::<$($phantom_ty,)? &mut [f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + + expect_ref((&arrays).into_iter()); + expect_ref((&slices).into_iter()); + expect_ref((&mut_slices).into_iter()); + + expect_ref_mut((&mut arrays).into_iter()); + expect_ref((&mut slices).into_iter()); + expect_ref_mut((&mut mut_slices).into_iter()); + } + + #[test] + fn into_iterator_ref_alpha() { + use crate::alpha::Alpha; + + fn expect_ref<'a>(_: impl Iterator, &'a f32>>){} + fn expect_ref_mut<'a>(_: impl Iterator, &'a mut f32>>){} + + let mut arrays = Alpha::<_, [f32; 0]>{ + color: $color_ty::<$($phantom_ty,)? [f32; 0]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let mut slices = Alpha::<_, &[f32]>{ + color: $color_ty::<$($phantom_ty,)? &[f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let mut mut_slices = Alpha::<_, &mut [f32]>{ + color: $color_ty::<$($phantom_ty,)? &mut [f32]>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + + expect_ref((&arrays).into_iter()); + expect_ref((&slices).into_iter()); + expect_ref((&mut_slices).into_iter()); + + expect_ref_mut((&mut arrays).into_iter()); + expect_ref((&mut slices).into_iter()); + expect_ref_mut((&mut mut_slices).into_iter()); + } + + #[cfg(feature = "alloc")] + #[test] + fn into_iterator_alloc() { + fn expect_move(_: impl Iterator>){} + fn expect_ref<'a>(_: impl Iterator>){} + + let vecs = $color_ty::<$($phantom_ty,)? Vec>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let boxed_slices = $color_ty::<$($phantom_ty,)? Box<[f32]>>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + + expect_move(vecs.into_iter()); + expect_ref(boxed_slices.into_iter()); + } + + #[cfg(feature = "alloc")] + #[test] + fn into_iterator_alloc_alpha() { + use crate::alpha::Alpha; + + fn expect_move(_: impl Iterator, f32>>){} + fn expect_ref<'a>(_: impl Iterator, &'a f32>>){} + + let vecs = Alpha::<_, Vec>{ + color: $color_ty::<$($phantom_ty,)? Vec>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let boxed_slices = Alpha::<_, Box<[f32]>>{ + color: $color_ty::<$($phantom_ty,)? Box<[f32]>>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + + expect_move(vecs.into_iter()); + expect_ref(boxed_slices.into_iter()); + } + + #[cfg(feature = "alloc")] + #[test] + fn into_iterator_alloc_ref() { + fn expect_ref<'a>(_: impl Iterator>){} + fn expect_ref_mut<'a>(_: impl Iterator>){} + + let mut vecs = $color_ty::<$($phantom_ty,)? Vec>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + let mut boxed_slices = $color_ty::<$($phantom_ty,)? Box<[f32]>>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }; + + expect_ref((&vecs).into_iter()); + expect_ref((&boxed_slices).into_iter()); + + expect_ref_mut((&mut vecs).into_iter()); + expect_ref_mut((&mut boxed_slices).into_iter()); + } + + #[cfg(feature = "alloc")] + #[test] + fn into_iterator_alloc_ref_alpha() { + use crate::alpha::Alpha; + + fn expect_ref<'a>(_: impl Iterator, &'a f32>>){} + fn expect_ref_mut<'a>(_: impl Iterator, &'a mut f32>>){} + + let mut vecs = Alpha::<_, Vec>{ + color: $color_ty::<$($phantom_ty,)? Vec>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + let mut boxed_slices = Alpha::<_, Box<[f32]>>{ + color: $color_ty::<$($phantom_ty,)? Box<[f32]>>{ + $($element: Default::default(),)+ + $($phantom: core::marker::PhantomData,)? + }, + alpha: Default::default(), + }; + + expect_ref((&vecs).into_iter()); + expect_ref((&boxed_slices).into_iter()); + + expect_ref_mut((&mut vecs).into_iter()); + expect_ref_mut((&mut boxed_slices).into_iter()); + } } } diff --git a/palette/src/okhsl.rs b/palette/src/okhsl.rs index b5f60ebc8..9ebb692d8 100644 --- a/palette/src/okhsl.rs +++ b/palette/src/okhsl.rs @@ -429,21 +429,9 @@ mod tests { } struct_of_arrays_tests!( - Okhsl, - Okhsl::new(0.1f32, 0.2, 0.3), - Okhsl::new(0.2, 0.3, 0.4), - Okhsl::new(0.3, 0.4, 0.5) + Okhsl[hue, saturation, lightness], + super::Okhsla::new(0.1f32, 0.2, 0.3, 0.4), + super::Okhsla::new(0.2, 0.3, 0.4, 0.5), + super::Okhsla::new(0.3, 0.4, 0.5, 0.6) ); - - mod alpha { - #[cfg(feature = "alloc")] - use crate::okhsl::Okhsla; - - struct_of_arrays_tests!( - Okhsla, - Okhsla::new(0.1f32, 0.2, 0.3, 0.4), - Okhsla::new(0.2, 0.3, 0.4, 0.5), - Okhsla::new(0.3, 0.4, 0.5, 0.6) - ); - } } diff --git a/palette/src/okhsv.rs b/palette/src/okhsv.rs index 468416bdd..8a2166b7b 100644 --- a/palette/src/okhsv.rs +++ b/palette/src/okhsv.rs @@ -553,21 +553,9 @@ mod tests { } struct_of_arrays_tests!( - Okhsv, - Okhsv::new(0.1f32, 0.2, 0.3), - Okhsv::new(0.2, 0.3, 0.4), - Okhsv::new(0.3, 0.4, 0.5) + Okhsv[hue, saturation, value], + super::Okhsva::new(0.1f32, 0.2, 0.3, 0.4), + super::Okhsva::new(0.2, 0.3, 0.4, 0.5), + super::Okhsva::new(0.3, 0.4, 0.5, 0.6) ); - - mod alpha { - #[cfg(feature = "alloc")] - use crate::okhsv::Okhsva; - - struct_of_arrays_tests!( - Okhsva, - Okhsva::new(0.1f32, 0.2, 0.3, 0.4), - Okhsva::new(0.2, 0.3, 0.4, 0.5), - Okhsva::new(0.3, 0.4, 0.5, 0.6) - ); - } } diff --git a/palette/src/okhwb.rs b/palette/src/okhwb.rs index 0b9f3aa1c..93e9d8674 100644 --- a/palette/src/okhwb.rs +++ b/palette/src/okhwb.rs @@ -282,21 +282,9 @@ mod tests { } struct_of_arrays_tests!( - Okhwb, - Okhwb::new(0.1f32, 0.2, 0.3), - Okhwb::new(0.2, 0.3, 0.4), - Okhwb::new(0.3, 0.4, 0.5) + Okhwb[hue, whiteness, blackness], + super::Okhwba::new(0.1f32, 0.2, 0.3, 0.4), + super::Okhwba::new(0.2, 0.3, 0.4, 0.5), + super::Okhwba::new(0.3, 0.4, 0.5, 0.6) ); - - mod alpha { - #[cfg(feature = "alloc")] - use crate::okhwb::Okhwba; - - struct_of_arrays_tests!( - Okhwba, - Okhwba::new(0.1f32, 0.2, 0.3, 0.4), - Okhwba::new(0.2, 0.3, 0.4, 0.5), - Okhwba::new(0.3, 0.4, 0.5, 0.6) - ); - } } diff --git a/palette/src/oklab.rs b/palette/src/oklab.rs index e63948f08..51db0f31e 100644 --- a/palette/src/oklab.rs +++ b/palette/src/oklab.rs @@ -679,24 +679,12 @@ mod test { } struct_of_arrays_tests!( - Oklab, - Oklab::new(0.1f32, 0.2, 0.3), - Oklab::new(0.2, 0.3, 0.4), - Oklab::new(0.3, 0.4, 0.5) + Oklab[l, a, b], + super::Oklaba::new(0.1f32, 0.2, 0.3, 0.4), + super::Oklaba::new(0.2, 0.3, 0.4, 0.5), + super::Oklaba::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::oklab::Oklaba; - - struct_of_arrays_tests!( - Oklaba, - Oklaba::new(0.1f32, 0.2, 0.3, 0.4), - Oklaba::new(0.2, 0.3, 0.4, 0.5), - Oklaba::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/oklch.rs b/palette/src/oklch.rs index 69b696a06..49198bb9f 100644 --- a/palette/src/oklch.rs +++ b/palette/src/oklch.rs @@ -271,24 +271,12 @@ mod test { } struct_of_arrays_tests!( - Oklch, - Oklch::new(0.1f32, 0.2, 0.3), - Oklch::new(0.2, 0.3, 0.4), - Oklch::new(0.3, 0.4, 0.5) + Oklch[l, chroma, hue], + super::Oklcha::new(0.1f32, 0.2, 0.3, 0.4), + super::Oklcha::new(0.2, 0.3, 0.4, 0.5), + super::Oklcha::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::oklch::Oklcha; - - struct_of_arrays_tests!( - Oklcha, - Oklcha::new(0.1f32, 0.2, 0.3, 0.4), - Oklcha::new(0.2, 0.3, 0.4, 0.5), - Oklcha::new(0.3, 0.4, 0.5, 0.6) - ); - } - test_uniform_distribution! { Oklch as crate::Oklab { l: (0.0, 1.0), diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index fffe63e69..4d41acb7f 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -1498,24 +1498,12 @@ mod test { } struct_of_arrays_tests!( - Rgb, - Rgb::new(0.1f32, 0.2, 0.3), - Rgb::new(0.2, 0.3, 0.4), - Rgb::new(0.3, 0.4, 0.5) + Rgb[red, green, blue] phantom: standard, + Rgba::new(0.1f32, 0.2, 0.3, 0.4), + Rgba::new(0.2, 0.3, 0.4, 0.5), + Rgba::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{encoding::Srgb, rgb::Rgba}; - - struct_of_arrays_tests!( - Rgba, - Rgba::new(0.1f32, 0.2, 0.3, 0.4), - Rgba::new(0.2, 0.3, 0.4, 0.5), - Rgba::new(0.3, 0.4, 0.5, 0.6) - ); - } - test_uniform_distribution! { Rgb { red: (0.0, 1.0), diff --git a/palette/src/xyz.rs b/palette/src/xyz.rs index f69556542..07f9c4c3d 100644 --- a/palette/src/xyz.rs +++ b/palette/src/xyz.rs @@ -493,24 +493,12 @@ mod test { } struct_of_arrays_tests!( - Xyz, - Xyz::new(0.1f32, 0.2, 0.3), - Xyz::new(0.2, 0.3, 0.4), - Xyz::new(0.3, 0.4, 0.5) + Xyz[x, y, z] phantom: white_point, + super::Xyza::new(0.1f32, 0.2, 0.3, 0.4), + super::Xyza::new(0.2, 0.3, 0.4, 0.5), + super::Xyza::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{white_point::D65, xyz::Xyza}; - - struct_of_arrays_tests!( - Xyza, - Xyza::new(0.1f32, 0.2, 0.3, 0.4), - Xyza::new(0.2, 0.3, 0.4, 0.5), - Xyza::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { diff --git a/palette/src/yxy.rs b/palette/src/yxy.rs index 515ac64bb..4727ddbe2 100644 --- a/palette/src/yxy.rs +++ b/palette/src/yxy.rs @@ -362,24 +362,12 @@ mod test { } struct_of_arrays_tests!( - Yxy, - Yxy::new(0.1f32, 0.2, 0.3), - Yxy::new(0.2, 0.3, 0.4), - Yxy::new(0.3, 0.4, 0.5) + Yxy[x, y, luma] phantom: white_point, + super::Yxya::new(0.1f32, 0.2, 0.3, 0.4), + super::Yxya::new(0.2, 0.3, 0.4, 0.5), + super::Yxya::new(0.3, 0.4, 0.5, 0.6) ); - mod alpha { - #[cfg(feature = "alloc")] - use crate::{white_point::D65, yxy::Yxya}; - - struct_of_arrays_tests!( - Yxya, - Yxya::new(0.1f32, 0.2, 0.3, 0.4), - Yxya::new(0.2, 0.3, 0.4, 0.5), - Yxya::new(0.3, 0.4, 0.5, 0.6) - ); - } - #[cfg(feature = "serializing")] #[test] fn serialize() { From ce86a2740e41d8b8116bd884c6dff4c60425bd9a Mon Sep 17 00:00:00 2001 From: Erik Hedvall Date: Sat, 24 Feb 2024 16:04:34 +0100 Subject: [PATCH 4/4] Remove unused traits and unnecessary import --- palette/src/cast/array.rs | 2 +- palette/src/hsl.rs | 3 --- palette/src/hsv.rs | 3 --- palette/src/hues.rs | 5 +---- palette/src/hwb.rs | 3 --- palette_derive/src/meta/mod.rs | 15 --------------- 6 files changed, 2 insertions(+), 29 deletions(-) diff --git a/palette/src/cast/array.rs b/palette/src/cast/array.rs index e55b6ac20..25c9e9f2d 100644 --- a/palette/src/cast/array.rs +++ b/palette/src/cast/array.rs @@ -1,6 +1,6 @@ use core::mem::{transmute_copy, ManuallyDrop}; -#[cfg(feature = "alloc")] +#[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::{boxed::Box, vec::Vec}; pub use palette_derive::ArrayCast; diff --git a/palette/src/hsl.rs b/palette/src/hsl.rs index 50c0e0bc5..7bc4591fa 100644 --- a/palette/src/hsl.rs +++ b/palette/src/hsl.rs @@ -533,9 +533,6 @@ unsafe impl bytemuck::Pod for Hsl where T: bytemuck::Pod {} mod test { use super::Hsl; - #[cfg(feature = "alloc")] - use crate::Srgb; - test_convert_into_from_xyz!(Hsl); #[cfg(feature = "approx")] diff --git a/palette/src/hsv.rs b/palette/src/hsv.rs index 559676df7..6188bd7ff 100644 --- a/palette/src/hsv.rs +++ b/palette/src/hsv.rs @@ -540,9 +540,6 @@ unsafe impl bytemuck::Pod for Hsv where T: bytemuck::Pod {} mod test { use super::Hsv; - #[cfg(feature = "alloc")] - use crate::Srgb; - test_convert_into_from_xyz!(Hsv); #[cfg(feature = "approx")] diff --git a/palette/src/hues.rs b/palette/src/hues.rs index 371bf9a8f..6505574b6 100644 --- a/palette/src/hues.rs +++ b/palette/src/hues.rs @@ -3,10 +3,7 @@ #[cfg(any(feature = "approx", feature = "random"))] use core::ops::Mul; -use core::{ - cmp::PartialEq, - ops::{Add, AddAssign, Neg, Sub, SubAssign}, -}; +use core::ops::{Add, AddAssign, Neg, Sub, SubAssign}; #[cfg(feature = "approx")] use approx::{AbsDiffEq, RelativeEq, UlpsEq}; diff --git a/palette/src/hwb.rs b/palette/src/hwb.rs index cb815e8cc..7b84540be 100644 --- a/palette/src/hwb.rs +++ b/palette/src/hwb.rs @@ -366,9 +366,6 @@ unsafe impl bytemuck::Pod for Hwb where T: bytemuck::Pod {} mod test { use super::Hwb; - #[cfg(feature = "alloc")] - use crate::Srgb; - test_convert_into_from_xyz!(Hwb); #[cfg(feature = "approx")] diff --git a/palette_derive/src/meta/mod.rs b/palette_derive/src/meta/mod.rs index ab01c9b00..9f0fcc11c 100644 --- a/palette_derive/src/meta/mod.rs +++ b/palette_derive/src/meta/mod.rs @@ -207,21 +207,6 @@ impl ::quote::ToTokens for IdentOrIndex { } } -pub trait MetaParser: Default { - fn internal(&mut self); - fn parse_attribute(&mut self, attribute_name: Ident, attribute_tts: TokenStream) -> Result<()>; -} - -pub trait DataMetaParser: Default { - fn parse_struct_field_attribute( - &mut self, - field_name: IdentOrIndex, - ty: Type, - attribute_name: Ident, - attribute_tts: TokenStream, - ) -> Result<()>; -} - pub trait AttributeArgumentParser: Default { fn argument(&mut self, argument: Meta) -> Result<()>; }