Skip to content

Commit

Permalink
Merge pull request #380 from Ogeon/issue_283_into_iterator
Browse files Browse the repository at this point in the history
Avoid recursive trait resolution for IntoIterator
  • Loading branch information
Ogeon authored Feb 25, 2024
2 parents e75eab2 + ce86a27 commit 25ca8af
Show file tree
Hide file tree
Showing 22 changed files with 1,313 additions and 499 deletions.
52 changes: 0 additions & 52 deletions palette/src/alpha/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,58 +864,6 @@ where
}
}

impl<C, A> IntoIterator for Alpha<C, A>
where
C: IntoIterator,
A: IntoIterator,
{
type Item = Alpha<C::Item, A::Item>;

type IntoIter = Iter<C::IntoIter, A::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 Alpha<C, A>
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<C, A>
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<C, A> {
pub(crate) color: C,
Expand Down
2 changes: 1 addition & 1 deletion palette/src/cast/array.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
23 changes: 4 additions & 19 deletions palette/src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,6 @@ unsafe impl<S: 'static, T> bytemuck::Pod for Hsl<S, T> where T: bytemuck::Pod {}
mod test {
use super::Hsl;

#[cfg(feature = "alloc")]
use crate::Srgb;

test_convert_into_from_xyz!(Hsl);

#[cfg(feature = "approx")]
Expand Down Expand Up @@ -622,24 +619,12 @@ mod test {
}

struct_of_arrays_tests!(
Hsl<Srgb>,
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<crate::encoding::Srgb>[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<Srgb>,
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() {
Expand Down
20 changes: 4 additions & 16 deletions palette/src/hsluv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,24 +345,12 @@ mod test {
}

struct_of_arrays_tests!(
Hsluv<D65>,
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<D65>[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<D65>,
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() {
Expand Down
23 changes: 4 additions & 19 deletions palette/src/hsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,6 @@ unsafe impl<S: 'static, T> bytemuck::Pod for Hsv<S, T> where T: bytemuck::Pod {}
mod test {
use super::Hsv;

#[cfg(feature = "alloc")]
use crate::Srgb;

test_convert_into_from_xyz!(Hsv);

#[cfg(feature = "approx")]
Expand Down Expand Up @@ -629,24 +626,12 @@ mod test {
}

struct_of_arrays_tests!(
Hsv<Srgb>,
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<crate::encoding::Srgb>[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<Srgb>,
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() {
Expand Down
128 changes: 115 additions & 13 deletions palette/src/hues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -582,36 +579,141 @@ macro_rules! make_hues {
}
}

impl<C> IntoIterator for $name<C> where C: IntoIterator {
type Item = $name<C::Item>;
type IntoIter = $iter_name<C::IntoIter>;
impl<T, const N: usize> IntoIterator for $name<[T; N]> {
type Item = $name<T>;
type IntoIter = $iter_name<core::array::IntoIter<T, N>>;

#[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<core::slice::Iter<'a, T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name(self.0.into_iter())
}
}

impl<'a, T> IntoIterator for $name<&'a mut [T]> {
type Item = $name<&'a mut T>;
type IntoIter = $iter_name<core::slice::IterMut<'a, T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name(self.0.into_iter())
}
}

#[cfg(feature = "alloc")]
impl<T> IntoIterator for $name<alloc::vec::Vec<T>> {
type Item = $name<T>;
type IntoIter = $iter_name<alloc::vec::IntoIter<T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name(self.0.into_iter())
}
}

impl<'a, C> IntoIterator for &'a $name<C> where &'a C: IntoIterator {
type Item = $name<<&'a C as IntoIterator>::Item>;
type IntoIter = $iter_name<<&'a C as IntoIterator>::IntoIter>;
impl<'a, T, const N: usize> IntoIterator for &'a $name<[T; N]> {
type Item = $name<&'a T>;
type IntoIter = $iter_name<core::slice::Iter<'a, T>>;

#[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<core::slice::Iter<'a, T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name(self.0.into_iter())
}
}

impl<'a, C> IntoIterator for &'a mut $name<C> where &'a mut C: IntoIterator {
type Item = $name<<&'a mut C as IntoIterator>::Item>;
type IntoIter = $iter_name<<&'a mut C as IntoIterator>::IntoIter>;
impl<'a, 'b, T> IntoIterator for &'a $name<&'b mut [T]> {
type Item = $name<&'a T>;
type IntoIter = $iter_name<core::slice::Iter<'a, T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name((&*self.0).into_iter())
}
}

#[cfg(feature = "alloc")]
impl<'a, T> IntoIterator for &'a $name<alloc::vec::Vec<T>> {
type Item = $name<&'a T>;
type IntoIter = $iter_name<core::slice::Iter<'a, T>>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
$iter_name((&self.0).into_iter())
}
}

#[cfg(feature = "alloc")]
impl<'a, T> IntoIterator for &'a $name<alloc::boxed::Box<[T]>> {
type Item = $name<&'a T>;
type IntoIter = $iter_name<core::slice::Iter<'a, T>>;

#[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<core::slice::IterMut<'a, T>>;

#[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<core::slice::IterMut<'a, T>>;

#[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<alloc::vec::Vec<T>> {
type Item = $name<&'a mut T>;
type IntoIter = $iter_name<core::slice::IterMut<'a, T>>;

#[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<alloc::boxed::Box<[T]>> {
type Item = $name<&'a mut T>;
type IntoIter = $iter_name<core::slice::IterMut<'a, T>>;

#[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>(I);

Expand Down
23 changes: 4 additions & 19 deletions palette/src/hwb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,6 @@ unsafe impl<S: 'static, T> bytemuck::Pod for Hwb<S, T> where T: bytemuck::Pod {}
mod test {
use super::Hwb;

#[cfg(feature = "alloc")]
use crate::Srgb;

test_convert_into_from_xyz!(Hwb);

#[cfg(feature = "approx")]
Expand Down Expand Up @@ -456,24 +453,12 @@ mod test {
}

struct_of_arrays_tests!(
Hwb<Srgb>,
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<crate::encoding::Srgb>[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<Srgb>,
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() {
Expand Down
Loading

0 comments on commit 25ca8af

Please sign in to comment.