From a8face0e8c5123447ce09bf96602a29536062e53 Mon Sep 17 00:00:00 2001 From: Erik Hedvall Date: Fri, 1 Sep 2023 14:25:12 +0200 Subject: [PATCH] Require clippy lints to pass --- .github/workflows/ci.yml | 11 ++++--- .vscode/settings.json | 5 +-- palette/Cargo.toml | 1 + palette/benches/matrix.rs | 2 +- palette/examples/blend.rs | 13 ++++---- palette/examples/color_scheme.rs | 6 ++-- palette/examples/compose.rs | 5 +-- palette/examples/readme_examples.rs | 2 +- palette/src/alpha.rs | 1 + palette/src/blend.rs | 1 + .../src/cast/from_into_components_traits.rs | 2 +- palette/src/color_difference.rs | 11 ++++--- palette/src/convert.rs | 20 +++++------ palette/src/convert/from_into_color_mut.rs | 3 +- .../convert/from_into_color_unclamped_mut.rs | 1 + palette/src/convert/try_from_into_color.rs | 2 -- palette/src/hues.rs | 33 +++++++++++-------- palette/src/hwb.rs | 4 +-- palette/src/lch.rs | 10 +++--- palette/src/luma.rs | 1 + palette/src/okhsv.rs | 14 ++++---- palette/src/random_sampling.rs | 1 + palette/src/random_sampling/cone.rs | 2 ++ palette/src/relative_contrast.rs | 11 ++++--- palette/src/rgb.rs | 1 + palette/src/rgb/rgb.rs | 4 +-- palette/src/serde/alpha_deserializer.rs | 2 +- palette/src/stimulus.rs | 6 ++-- palette/src/xyz.rs | 8 ++--- palette/tests/color_checker_data/babel.rs | 10 +++--- .../tests/color_checker_data/color_checker.rs | 10 +++--- palette/tests/color_checker_data/load_data.rs | 1 - palette/tests/convert/data_cie_15_2004.rs | 9 +++-- palette/tests/convert/data_ciede_2000.rs | 2 -- palette/tests/convert/data_color_mine.rs | 23 +++++++------ palette/tests/hsluv_dataset/mod.rs | 1 + palette/tests/pointer_dataset/pointer_data.rs | 9 +++-- palette_derive/Cargo.toml | 1 + palette_derive/src/cast/array_cast.rs | 4 +-- .../src/convert/from_color_unclamped.rs | 25 +++++++------- palette_derive/src/convert/util.rs | 12 +++---- palette_derive/src/meta/mod.rs | 6 ++-- 42 files changed, 153 insertions(+), 143 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 93922f6cd..8997d5ac7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,12 +22,14 @@ jobs: steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.60.0 + with: + components: clippy - name: Minimal check - run: cargo check -v -p palette --no-default-features --features std + run: cargo clippy -v -p palette --no-default-features --features std - name: find-crate check - run: cargo check -v -p palette --no-default-features --features "std find-crate" + run: cargo clippy -v -p palette --no-default-features --features "std find-crate" - name: Default check - run: cargo check -v --workspace --exclude no_std_test + run: cargo clippy -v --workspace --exclude no_std_test - name: Test all features run: cargo test -v -p palette --all-features - name: Test each feature @@ -47,8 +49,9 @@ jobs: - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.toolchain }} + components: clippy - name: Check all features - run: cargo check -v -p palette --all-features + run: cargo clippy -v -p palette --all-features no_std: name: "Test #[no_std]" runs-on: ubuntu-latest diff --git a/.vscode/settings.json b/.vscode/settings.json index 3ffbce91c..d3116486c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,6 @@ ], "rust-analyzer.imports.granularity.enforce": true, "rust-analyzer.imports.granularity.group": "crate", - "rust-analyzer.imports.group.enable": true -} \ No newline at end of file + "rust-analyzer.imports.group.enable": true, + "rust-analyzer.check.command": "clippy" +} diff --git a/palette/Cargo.toml b/palette/Cargo.toml index acdd40a27..6055a1d8d 100644 --- a/palette/Cargo.toml +++ b/palette/Cargo.toml @@ -25,6 +25,7 @@ edition = "2018" resolver = "2" categories = ["graphics", "multimedia::images", "no-std"] build = "build/main.rs" +rust-version = "1.60.0" [features] default = ["named_from_str", "std", "approx"] diff --git a/palette/benches/matrix.rs b/palette/benches/matrix.rs index ca2811d89..35530fb79 100644 --- a/palette/benches/matrix.rs +++ b/palette/benches/matrix.rs @@ -36,7 +36,7 @@ fn matrix(c: &mut Criterion) { b.iter(|| matrix_inverse(*inverse)) }); group.bench_function("rgb_to_xyz_matrix", |b| { - b.iter(|| rgb_to_xyz_matrix::()) + b.iter(rgb_to_xyz_matrix::) }); } diff --git a/palette/examples/blend.rs b/palette/examples/blend.rs index 1873c06c7..563fb7ebe 100644 --- a/palette/examples/blend.rs +++ b/palette/examples/blend.rs @@ -45,7 +45,7 @@ fn main() { &mut image, x, y, - 0 * ALPHA_STEPS + alpha_step, + alpha_step, tile_width, tile_height, foreground, @@ -56,7 +56,7 @@ fn main() { &mut image, x, y, - 1 * ALPHA_STEPS + alpha_step, + ALPHA_STEPS + alpha_step, tile_width, tile_height, Xyza::from_color(foreground), @@ -72,6 +72,7 @@ fn main() { } } +#[allow(clippy::too_many_arguments)] fn draw_composed_pixels( image: &mut image::RgbaImage, x: u32, @@ -106,11 +107,9 @@ fn draw_composed_pixels( } } -fn compose( - fg: Alpha, - bg: Alpha, - function: fn(Alpha, Alpha) -> Alpha, -) -> image::Rgba +type ComposeFn = fn(Alpha, Alpha) -> Alpha; + +fn compose(fg: Alpha, bg: Alpha, function: ComposeFn) -> image::Rgba where Alpha: Blend + IntoColor, { diff --git a/palette/examples/color_scheme.rs b/palette/examples/color_scheme.rs index 0420334b0..41b21ccf4 100644 --- a/palette/examples/color_scheme.rs +++ b/palette/examples/color_scheme.rs @@ -155,9 +155,9 @@ fn blit_shades(color: LinSrgb, mut canvas: SubImage<&mut RgbImage>) { let primary = Srgb::from_linear(color).into(); //Generate one lighter and two darker versions of the color - let light = Srgb::from_linear(color.lighten(0.1).into()).into(); - let dark1 = Srgb::from_linear(color.darken(0.1).into()).into(); - let dark2 = Srgb::from_linear(color.darken(0.2).into()).into(); + let light = Srgb::from_linear(color.lighten(0.1)).into(); + let dark1 = Srgb::from_linear(color.darken(0.1)).into(); + let dark2 = Srgb::from_linear(color.darken(0.2)).into(); for x in 0..width { for y in 0..height { diff --git a/palette/examples/compose.rs b/palette/examples/compose.rs index 0a7080f97..230efca42 100644 --- a/palette/examples/compose.rs +++ b/palette/examples/compose.rs @@ -56,7 +56,7 @@ fn main() { &mut image, x, y, - 0 * ALPHA_STEPS + alpha_step, + alpha_step, tile_width, tile_height, foreground.into(), @@ -67,7 +67,7 @@ fn main() { &mut image, x, y, - 1 * ALPHA_STEPS + alpha_step, + ALPHA_STEPS + alpha_step, tile_width, tile_height, Xyza::from_color(foreground).into(), @@ -127,6 +127,7 @@ fn main() { } } +#[allow(clippy::too_many_arguments)] fn draw_composed_pixels( image: &mut image::RgbaImage, x: u32, diff --git a/palette/examples/readme_examples.rs b/palette/examples/readme_examples.rs index c59338859..c75a8e5e3 100644 --- a/palette/examples/readme_examples.rs +++ b/palette/examples/readme_examples.rs @@ -177,7 +177,7 @@ fn display_colors(filename: &str, displays: &[DisplayType]) { let mut image = RgbImage::new(WIDTH, displays.len() as u32 * row_height); - for (i, display) in displays.into_iter().enumerate() { + for (i, display) in displays.iter().enumerate() { let image = image.sub_image(0, i as u32 * row_height, WIDTH, row_height); match *display { DisplayType::Discrete(colors) => { diff --git a/palette/src/alpha.rs b/palette/src/alpha.rs index f99763b8e..2de79b367 100644 --- a/palette/src/alpha.rs +++ b/palette/src/alpha.rs @@ -10,6 +10,7 @@ pub use self::alpha::*; #[doc(no_inline)] pub use crate::blend::PreAlpha; // Cross-link for visibility. +#[allow(clippy::module_inception)] mod alpha; /// A trait for color types that can have or be given transparency (alpha channel). diff --git a/palette/src/blend.rs b/palette/src/blend.rs index ef3cb2d15..f23fcd0c8 100644 --- a/palette/src/blend.rs +++ b/palette/src/blend.rs @@ -50,6 +50,7 @@ pub use self::{ pre_alpha::PreAlpha, }; +#[allow(clippy::module_inception)] mod blend; mod blend_with; mod compose; diff --git a/palette/src/cast/from_into_components_traits.rs b/palette/src/cast/from_into_components_traits.rs index 3962ce23f..817c04f9b 100644 --- a/palette/src/cast/from_into_components_traits.rs +++ b/palette/src/cast/from_into_components_traits.rs @@ -581,7 +581,7 @@ where } } -impl<'a, T, C> TryComponentsInto for T +impl TryComponentsInto for T where C: TryFromComponents, { diff --git a/palette/src/color_difference.rs b/palette/src/color_difference.rs index 03c1fa7b2..dbcd81339 100644 --- a/palette/src/color_difference.rs +++ b/palette/src/color_difference.rs @@ -463,11 +463,12 @@ mod test { assert!(c1.has_enhanced_contrast_text(white)); assert!(c1.has_enhanced_contrast_large_text(white)); assert!(c1.has_min_contrast_graphics(white)); - assert!(c1.has_min_contrast_text(black) == false); - assert!(c1.has_min_contrast_large_text(black) == false); - assert!(c1.has_enhanced_contrast_text(black) == false); - assert!(c1.has_enhanced_contrast_large_text(black) == false); - assert!(c1.has_min_contrast_graphics(black) == false); + + assert!(!c1.has_min_contrast_text(black)); + assert!(!c1.has_min_contrast_large_text(black)); + assert!(!c1.has_enhanced_contrast_text(black)); + assert!(!c1.has_enhanced_contrast_large_text(black)); + assert!(!c1.has_min_contrast_graphics(black)); let c1 = Srgb::from_str("#066").unwrap().into_format(); diff --git a/palette/src/convert.rs b/palette/src/convert.rs index 228b1eb49..46f1861f0 100644 --- a/palette/src/convert.rs +++ b/palette/src/convert.rs @@ -196,19 +196,17 @@ //! } //! } //! -//! fn main() { -//! // Start with an Xyz100 color. -//! let xyz = Xyz100 { -//! x: 59, -//! y: 75, -//! z: 42, -//! }; +//! // Start with an Xyz100 color. +//! let xyz = Xyz100 { +//! x: 59, +//! y: 75, +//! z: 42, +//! }; //! -//! // Convert the color to sRGB. -//! let rgb: Srgb = xyz.into_color(); +//! // Convert the color to sRGB. +//! let rgb: Srgb = xyz.into_color(); //! -//! assert_eq!(rgb.into_format(), Srgb::new(196u8, 238, 154)); -//! } +//! assert_eq!(rgb.into_format(), Srgb::new(196u8, 238, 154)); //! ``` //! //! With generic components: diff --git a/palette/src/convert/from_into_color_mut.rs b/palette/src/convert/from_into_color_mut.rs index b76bd9330..c8f27f926 100644 --- a/palette/src/convert/from_into_color_mut.rs +++ b/palette/src/convert/from_into_color_mut.rs @@ -200,6 +200,7 @@ where /// /// This reuses the memory space, and the returned scope guard will restore /// the converted colors to their original type when it's dropped. + #[allow(clippy::wrong_self_convention)] #[must_use] fn into_color_mut(&mut self) -> FromColorMutGuard; } @@ -308,7 +309,7 @@ where .and_then(|mut guard| guard.current.take()); if let Some(restored) = restored { - return restored; + restored } else { unreachable!() } diff --git a/palette/src/convert/from_into_color_unclamped_mut.rs b/palette/src/convert/from_into_color_unclamped_mut.rs index a6a346df6..6ba4cc2f6 100644 --- a/palette/src/convert/from_into_color_unclamped_mut.rs +++ b/palette/src/convert/from_into_color_unclamped_mut.rs @@ -201,6 +201,7 @@ where /// /// This reuses the memory space, and the returned scope guard will restore /// the converted colors to their original type when it's dropped. + #[allow(clippy::wrong_self_convention)] #[must_use] fn into_color_unclamped_mut(&mut self) -> FromColorUnclampedMutGuard; } diff --git a/palette/src/convert/try_from_into_color.rs b/palette/src/convert/try_from_into_color.rs index 50de5cd08..5897a5318 100644 --- a/palette/src/convert/try_from_into_color.rs +++ b/palette/src/convert/try_from_into_color.rs @@ -64,7 +64,6 @@ pub trait TryFromColor: Sized { /// } /// }; /// ``` - #[must_use] fn try_from_color(t: T) -> Result>; } @@ -105,7 +104,6 @@ pub trait TryIntoColor: Sized { /// } /// }; /// ``` - #[must_use] fn try_into_color(self) -> Result>; } diff --git a/palette/src/hues.rs b/palette/src/hues.rs index 0e74bf53b..e25fc75d6 100644 --- a/palette/src/hues.rs +++ b/palette/src/hues.rs @@ -287,31 +287,38 @@ macro_rules! make_hues { } } - impl Into for $name { + impl From<$name> for f64 { #[inline] - fn into(self) -> f64 { - self.0.normalize_signed_angle() + fn from(hue: $name) -> f64 { + hue.0.normalize_signed_angle() } } - impl Into for $name { + impl From<$name> for f64 { #[inline] - fn into(self) -> f32 { - self.0.normalize_signed_angle() + fn from(hue: $name) -> f64 { + hue.0.normalize_signed_angle() as f64 } } - impl Into for $name { + impl From<$name> for f32 { #[inline] - fn into(self) -> f32 { - self.0.normalize_signed_angle() as f32 + fn from(hue: $name) -> f32 { + hue.0.normalize_signed_angle() } } - impl Into for $name { + impl From<$name> for f32 { #[inline] - fn into(self) -> u8 { - self.0 + fn from(hue: $name) -> f32 { + hue.0.normalize_signed_angle() as f32 + } + } + + impl From<$name> for u8 { + #[inline] + fn from(hue: $name) -> u8 { + hue.0 } } @@ -893,7 +900,7 @@ mod test { assert!(degs > -180.0 && degs <= 180.0); let pos_degs = hue.into_positive_degrees(); - assert!(pos_degs >= 0.0 && pos_degs < 360.0); + assert!((0.0..360.0).contains(&pos_degs)); assert_relative_eq!(RgbHue::from(degs), RgbHue::from(pos_degs)); } diff --git a/palette/src/hwb.rs b/palette/src/hwb.rs index 10d86775e..278ebc6e6 100644 --- a/palette/src/hwb.rs +++ b/palette/src/hwb.rs @@ -660,8 +660,8 @@ where #[rustfmt::skip] fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { - let equal_shade = self.whiteness.ulps_eq(&other.whiteness, epsilon.clone(), max_ulps.clone()) - && self.blackness.ulps_eq(&other.blackness, epsilon.clone(), max_ulps.clone()); + let equal_shade = self.whiteness.ulps_eq(&other.whiteness, epsilon.clone(), max_ulps) + && self.blackness.ulps_eq(&other.blackness, epsilon.clone(), max_ulps); // The hue doesn't matter that much when the color is gray, and may fluctuate // due to precision errors. This is a blunt tool, but works for now. diff --git a/palette/src/lch.rs b/palette/src/lch.rs index 921b548de..ca1e6299b 100644 --- a/palette/src/lch.rs +++ b/palette/src/lch.rs @@ -554,11 +554,11 @@ mod test { #[test] fn check_min_max_components() { - assert_relative_eq!(Lch::::min_l(), 0.0); - assert_relative_eq!(Lch::::max_l(), 100.0); - assert_relative_eq!(Lch::::min_chroma(), 0.0); - assert_relative_eq!(Lch::::max_chroma(), 128.0); - assert_relative_eq!(Lch::::max_extended_chroma(), 181.01933598375618); + assert_relative_eq!(Lch::::min_l(), 0.0); + assert_relative_eq!(Lch::::max_l(), 100.0); + assert_relative_eq!(Lch::::min_chroma(), 0.0); + assert_relative_eq!(Lch::::max_chroma(), 128.0); + assert_relative_eq!(Lch::::max_extended_chroma(), 181.01933598375618); } struct_of_arrays_tests!( diff --git a/palette/src/luma.rs b/palette/src/luma.rs index b9e123022..cc9502fe1 100644 --- a/palette/src/luma.rs +++ b/palette/src/luma.rs @@ -1,6 +1,7 @@ //! Types for luma and luminance (grayscale) values. pub mod channels; +#[allow(clippy::module_inception)] mod luma; use crate::encoding::{Gamma, Linear, Srgb}; diff --git a/palette/src/okhsv.rs b/palette/src/okhsv.rs index 1af9fb0f6..d143b52e0 100644 --- a/palette/src/okhsv.rs +++ b/palette/src/okhsv.rs @@ -388,19 +388,19 @@ mod tests { // 1 iteration : 264.0520206380550121, 0.9999910912349018, 0.9999999646150918 // 2 iterations: 264.0520206380550121, 0.9999999869716002, 0.9999999646150844 // 3 iterations: 264.0520206380550121, 0.9999999869716024, 0.9999999646150842 + #[allow(clippy::excessive_precision)] + let expected_hue = OklabHue::new(264.0520206380550121); + let expected_saturation = 0.9999910912349018; + let expected_value = 0.9999999646150918; // compare to the reference implementation values - assert_abs_diff_eq!( - okhsv_blue_64.hue, - OklabHue::new(264.0520206380550121), - epsilon = 1e-12 - ); + assert_abs_diff_eq!(okhsv_blue_64.hue, expected_hue, epsilon = 1e-12); assert_abs_diff_eq!( okhsv_blue_64.saturation, - 0.9999910912349018, + expected_saturation, epsilon = 1e-12 ); - assert_abs_diff_eq!(okhsv_blue_64.value, 0.9999999646150918, epsilon = 1e-12); + assert_abs_diff_eq!(okhsv_blue_64.value, expected_value, epsilon = 1e-12); } #[test] diff --git a/palette/src/random_sampling.rs b/palette/src/random_sampling.rs index 7d5f3c02e..97a63a051 100644 --- a/palette/src/random_sampling.rs +++ b/palette/src/random_sampling.rs @@ -67,6 +67,7 @@ pub(crate) mod test_utils { } fn approximate_gamma(z: f64) -> f64 { + #[allow(clippy::excessive_precision)] const RECIP_E: f64 = 0.36787944117144232159552377016147; // RECIP_E = (E^-1) = (1.0 / E) const TWOPI: f64 = core::f64::consts::TAU; diff --git a/palette/src/random_sampling/cone.rs b/palette/src/random_sampling/cone.rs index 9dea52ba8..be5f6c430 100644 --- a/palette/src/random_sampling/cone.rs +++ b/palette/src/random_sampling/cone.rs @@ -186,6 +186,7 @@ mod test { } #[cfg(feature = "random")] + #[allow(clippy::excessive_precision)] #[test] fn hsl_sampling() { // Sanity check that sampling and inverting from sample are equivalent @@ -216,6 +217,7 @@ mod test { } #[cfg(feature = "random")] + #[allow(clippy::excessive_precision)] #[test] fn hsluv_sampling() { // Sanity check that sampling and inverting from sample are equivalent diff --git a/palette/src/relative_contrast.rs b/palette/src/relative_contrast.rs index ac5d0650a..1fcc0084c 100644 --- a/palette/src/relative_contrast.rs +++ b/palette/src/relative_contrast.rs @@ -154,11 +154,12 @@ mod test { assert!(c1.has_enhanced_contrast_text(white)); assert!(c1.has_enhanced_contrast_large_text(white)); assert!(c1.has_min_contrast_graphics(white)); - assert!(c1.has_min_contrast_text(black) == false); - assert!(c1.has_min_contrast_large_text(black) == false); - assert!(c1.has_enhanced_contrast_text(black) == false); - assert!(c1.has_enhanced_contrast_large_text(black) == false); - assert!(c1.has_min_contrast_graphics(black) == false); + + assert!(!c1.has_min_contrast_text(black)); + assert!(!c1.has_min_contrast_large_text(black)); + assert!(!c1.has_enhanced_contrast_text(black)); + assert!(!c1.has_enhanced_contrast_large_text(black)); + assert!(!c1.has_min_contrast_graphics(black)); let c1 = Srgb::from_str("#066").unwrap().into_format(); diff --git a/palette/src/rgb.rs b/palette/src/rgb.rs index ab41e5b10..7ace35d41 100644 --- a/palette/src/rgb.rs +++ b/palette/src/rgb.rs @@ -70,6 +70,7 @@ use crate::{ pub use self::rgb::{FromHexError, Iter, Rgb, Rgba}; pub mod channels; +#[allow(clippy::module_inception)] mod rgb; /// Non-linear sRGB, the most common RGB input/output format. diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index 16ba062c7..00a0213e7 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -1126,7 +1126,7 @@ impl From<&'static str> for FromHexError { impl core::fmt::Display for FromHexError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &*self { + match self { FromHexError::ParseIntError(e) => write!(f, "{}", e), FromHexError::HexFormatError(s) => write!( f, @@ -1145,7 +1145,7 @@ impl core::fmt::Display for FromHexError { #[cfg(feature = "std")] impl std::error::Error for FromHexError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match &*self { + match self { FromHexError::HexFormatError(_s) => None, FromHexError::RgbaHexFormatError(_s) => None, FromHexError::ParseIntError(e) => Some(e), diff --git a/palette/src/serde/alpha_deserializer.rs b/palette/src/serde/alpha_deserializer.rs index 966926994..1ebc48a17 100644 --- a/palette/src/serde/alpha_deserializer.rs +++ b/palette/src/serde/alpha_deserializer.rs @@ -352,7 +352,7 @@ where where T: serde::de::SeqAccess<'de>, { - let color = if self.field_count == None { + let color = if self.field_count.is_none() { self.inner.visit_unit()? } else { self.inner.visit_seq(&mut seq)? diff --git a/palette/src/stimulus.rs b/palette/src/stimulus.rs index d6e27cd8f..db0b102d9 100644 --- a/palette/src/stimulus.rs +++ b/palette/src/stimulus.rs @@ -355,8 +355,7 @@ mod test { fn uint_to_float() { fn into_stimulus_old(n: u8) -> f32 { let max = core::u8::MAX as f32; - let scaled = n as f32 / max; - scaled as f32 + n as f32 / max } for n in (0..=255).step_by(5) { @@ -368,8 +367,7 @@ mod test { fn uint_to_double() { fn into_stimulus_old(n: u8) -> f64 { let max = core::u8::MAX as f64; - let scaled = n as f64 / max; - scaled as f64 + n as f64 / max } for n in (0..=255).step_by(5) { diff --git a/palette/src/xyz.rs b/palette/src/xyz.rs index e0eb4f393..791d9590c 100644 --- a/palette/src/xyz.rs +++ b/palette/src/xyz.rs @@ -228,10 +228,10 @@ where Yxy: IntoColorUnclamped>, { fn from_color_unclamped(color: Rgb) -> Self { - let transform_matrix = S::Space::rgb_to_xyz_matrix().map_or_else( - || rgb_to_xyz_matrix::(), - |matrix| matrix_map(matrix, T::Scalar::from_f64), - ); + let transform_matrix = S::Space::rgb_to_xyz_matrix() + .map_or_else(rgb_to_xyz_matrix::, |matrix| { + matrix_map(matrix, T::Scalar::from_f64) + }); multiply_rgb_to_xyz(transform_matrix, color.into_linear()) } } diff --git a/palette/tests/color_checker_data/babel.rs b/palette/tests/color_checker_data/babel.rs index f67c21c5c..d75c7281c 100644 --- a/palette/tests/color_checker_data/babel.rs +++ b/palette/tests/color_checker_data/babel.rs @@ -55,17 +55,17 @@ impl_from_color!(Yxy); impl_from_color!(Xyz); impl_from_color!(Lab); -impl Into<[BabelData; 2]> for BabelData +impl From> for [BabelData; 2] where V: IntoScalarArray<2>, Xyz: Into<[Xyz; 2]>, Yxy: Into<[Yxy; 2]>, Lab: Into<[Lab; 2]>, { - fn into(self) -> [BabelData; 2] { - let [xyz0, xyz1]: [_; 2] = self.xyz.into(); - let [yxy0, yxy1]: [_; 2] = self.yxy.into(); - let [lab0, lab1]: [_; 2] = self.lab.into(); + fn from(color_data: BabelData) -> Self { + let [xyz0, xyz1]: [_; 2] = color_data.xyz.into(); + let [yxy0, yxy1]: [_; 2] = color_data.yxy.into(); + let [lab0, lab1]: [_; 2] = color_data.lab.into(); [ BabelData { diff --git a/palette/tests/color_checker_data/color_checker.rs b/palette/tests/color_checker_data/color_checker.rs index e38bb5aa2..2c2703cb1 100644 --- a/palette/tests/color_checker_data/color_checker.rs +++ b/palette/tests/color_checker_data/color_checker.rs @@ -55,17 +55,17 @@ impl_from_color!(Yxy); impl_from_color!(Xyz); impl_from_color!(Lab); -impl Into<[ColorCheckerData; 2]> for ColorCheckerData +impl From> for [ColorCheckerData; 2] where V: IntoScalarArray<2>, Xyz: Into<[Xyz; 2]>, Yxy: Into<[Yxy; 2]>, Lab: Into<[Lab; 2]>, { - fn into(self) -> [ColorCheckerData; 2] { - let [xyz0, xyz1]: [_; 2] = self.xyz.into(); - let [yxy0, yxy1]: [_; 2] = self.yxy.into(); - let [lab0, lab1]: [_; 2] = self.lab.into(); + fn from(color_data: ColorCheckerData) -> Self { + let [xyz0, xyz1]: [_; 2] = color_data.xyz.into(); + let [yxy0, yxy1]: [_; 2] = color_data.yxy.into(); + let [lab0, lab1]: [_; 2] = color_data.lab.into(); [ ColorCheckerData { diff --git a/palette/tests/color_checker_data/load_data.rs b/palette/tests/color_checker_data/load_data.rs index e63efdb20..014a0dae3 100644 --- a/palette/tests/color_checker_data/load_data.rs +++ b/palette/tests/color_checker_data/load_data.rs @@ -1,4 +1,3 @@ -use csv; use serde_derive::Deserialize; use super::babel::BabelData; diff --git a/palette/tests/convert/data_cie_15_2004.rs b/palette/tests/convert/data_cie_15_2004.rs index e430093ed..4e6719078 100644 --- a/palette/tests/convert/data_cie_15_2004.rs +++ b/palette/tests/convert/data_cie_15_2004.rs @@ -7,7 +7,6 @@ Tests XYZ and YXY conversion */ use approx::assert_relative_eq; -use csv; use serde_derive::Deserialize; use palette::{convert::IntoColorUnclamped, num::IntoScalarArray, white_point::D65, Xyz, Yxy}; @@ -57,15 +56,15 @@ macro_rules! impl_from_color_pointer { impl_from_color_pointer!(Xyz); impl_from_color_pointer!(Yxy); -impl Into<[Cie2004; 4]> for Cie2004 +impl From> for [Cie2004; 4] where V: IntoScalarArray<4>, Xyz: Into<[Xyz; 4]>, Yxy: Into<[Yxy; 4]>, { - fn into(self) -> [Cie2004; 4] { - let [xyz0, xyz1, xyz2, xyz3]: [_; 4] = self.xyz.into(); - let [yxy0, yxy1, yxy2, yxy3]: [_; 4] = self.yxy.into(); + fn from(color_data: Cie2004) -> Self { + let [xyz0, xyz1, xyz2, xyz3]: [_; 4] = color_data.xyz.into(); + let [yxy0, yxy1, yxy2, yxy3]: [_; 4] = color_data.yxy.into(); [ Cie2004 { diff --git a/palette/tests/convert/data_ciede_2000.rs b/palette/tests/convert/data_ciede_2000.rs index 1b22ded16..6556a2267 100644 --- a/palette/tests/convert/data_ciede_2000.rs +++ b/palette/tests/convert/data_ciede_2000.rs @@ -9,8 +9,6 @@ Note: Test uses `f64` because `f32` failed Travis CI builds on Linux for Lch on MacOS and Windows passed the tests so be wary when using f32 on Linux. */ -use csv; - use approx::assert_relative_eq; use serde_derive::Deserialize; diff --git a/palette/tests/convert/data_color_mine.rs b/palette/tests/convert/data_color_mine.rs index ec8b0cc18..585fd9454 100644 --- a/palette/tests/convert/data_color_mine.rs +++ b/palette/tests/convert/data_color_mine.rs @@ -1,7 +1,6 @@ /* List of color from www.colormine.org */ -use csv; use approx::assert_relative_eq; use lazy_static::lazy_static; @@ -273,23 +272,23 @@ where } } -impl Into<[ColorMine; 2]> for ColorMine +impl From> for [ColorMine; 2] where S: IntoScalarArray<2, Scalar = F>, F: Real + Zero + One + Default, D65: WhitePoint, Yxy: FromColorUnclamped>, { - fn into(self) -> [ColorMine; 2] { - let [xyz0, xyz1]: [Xyz<_, F>; 2] = self.xyz.into(); - let [yxy0, yxy1]: [Yxy<_, F>; 2] = self.yxy.into(); - let [lab0, lab1]: [Lab<_, F>; 2] = self.lab.into(); - let [lch0, lch1]: [Lch<_, F>; 2] = self.lch.into(); - let [linear_rgb0, linear_rgb1]: [LinSrgb; 2] = self.linear_rgb.into(); - let [rgb0, rgb1]: [Srgb; 2] = self.rgb.into(); - let [hsl0, hsl1]: [Hsl<_, F>; 2] = self.hsl.into(); - let [hsv0, hsv1]: [Hsv<_, F>; 2] = self.hsv.into(); - let [hwb0, hwb1]: [Hwb<_, F>; 2] = self.hwb.into(); + fn from(color_data: ColorMine) -> Self { + let [xyz0, xyz1]: [Xyz<_, F>; 2] = color_data.xyz.into(); + let [yxy0, yxy1]: [Yxy<_, F>; 2] = color_data.yxy.into(); + let [lab0, lab1]: [Lab<_, F>; 2] = color_data.lab.into(); + let [lch0, lch1]: [Lch<_, F>; 2] = color_data.lch.into(); + let [linear_rgb0, linear_rgb1]: [LinSrgb; 2] = color_data.linear_rgb.into(); + let [rgb0, rgb1]: [Srgb; 2] = color_data.rgb.into(); + let [hsl0, hsl1]: [Hsl<_, F>; 2] = color_data.hsl.into(); + let [hsv0, hsv1]: [Hsv<_, F>; 2] = color_data.hsv.into(); + let [hwb0, hwb1]: [Hwb<_, F>; 2] = color_data.hwb.into(); [ ColorMine { diff --git a/palette/tests/hsluv_dataset/mod.rs b/palette/tests/hsluv_dataset/mod.rs index be35ed521..84cf01265 100644 --- a/palette/tests/hsluv_dataset/mod.rs +++ b/palette/tests/hsluv_dataset/mod.rs @@ -1 +1,2 @@ +#[allow(clippy::module_inception)] pub mod hsluv_dataset; diff --git a/palette/tests/pointer_dataset/pointer_data.rs b/palette/tests/pointer_dataset/pointer_data.rs index fc53ef532..51bf201be 100644 --- a/palette/tests/pointer_dataset/pointer_data.rs +++ b/palette/tests/pointer_dataset/pointer_data.rs @@ -12,7 +12,6 @@ Note: The xyz and yxy conversions do not use the updated conversion formula. So */ use approx::assert_relative_eq; -use csv; use lazy_static::lazy_static; use serde_derive::Deserialize; @@ -90,15 +89,15 @@ macro_rules! impl_from_color_pointer { impl_from_color_pointer!(Lch); impl_from_color_pointer!(Lab); -impl Into<[PointerData; 2]> for PointerData +impl From> for [PointerData; 2] where V: IntoScalarArray<2>, Lch: Into<[Lch; 2]>, Lab: Into<[Lab; 2]>, { - fn into(self) -> [PointerData; 2] { - let [lch0, lch1]: [_; 2] = self.lch.into(); - let [lab0, lab1]: [_; 2] = self.lab.into(); + fn from(color_data: PointerData) -> Self { + let [lch0, lch1]: [_; 2] = color_data.lch.into(); + let [lab0, lab1]: [_; 2] = color_data.lab.into(); [ PointerData { diff --git a/palette_derive/Cargo.toml b/palette_derive/Cargo.toml index d3e74a7cf..7703241c2 100644 --- a/palette_derive/Cargo.toml +++ b/palette_derive/Cargo.toml @@ -11,6 +11,7 @@ keywords = ["palette", "derive", "macros"] license = "MIT OR Apache-2.0" edition = "2018" resolver = "2" +rust-version = "1.60.0" [lib] proc-macro = true diff --git a/palette_derive/src/cast/array_cast.rs b/palette_derive/src/cast/array_cast.rs index d3c45f312..9f83cf18f 100644 --- a/palette_derive/src/cast/array_cast.rs +++ b/palette_derive/src/cast/array_cast.rs @@ -62,7 +62,7 @@ pub fn derive(tokens: TokenStream) -> std::result::Result std::result::Result, ) -> Result> { let included_colors = COLOR_TYPES.iter().filter(|&&color| !skip.contains(color)); - let linear_path = util::path(&["encoding", "Linear"], meta.internal); + let linear_path = util::path(["encoding", "Linear"], meta.internal); let mut parameters = Vec::new(); @@ -156,15 +156,14 @@ fn prepare_from_impl( if used_input.white_point { match white_point_source { Some(WhitePointSource::WhitePoint) => { - let white_point_path = - util::path(&["white_point", "WhitePoint"], meta.internal); + let white_point_path = util::path(["white_point", "WhitePoint"], meta.internal); generics .make_where_clause() .predicates .push(parse_quote!(#white_point: #white_point_path<#component>)) } Some(WhitePointSource::RgbStandard) => { - let rgb_standard_path = util::path(&["rgb", "RgbStandard"], meta.internal); + let rgb_standard_path = util::path(["rgb", "RgbStandard"], meta.internal); let rgb_standard = meta.rgb_standard.as_ref(); generics .make_where_clause() @@ -172,7 +171,7 @@ fn prepare_from_impl( .push(parse_quote!(#rgb_standard: #rgb_standard_path)); } Some(WhitePointSource::LumaStandard) => { - let luma_standard_path = util::path(&["luma", "LumaStandard"], meta.internal); + let luma_standard_path = util::path(["luma", "LumaStandard"], meta.internal); let luma_standard = meta.luma_standard.as_ref(); generics .make_where_clause() @@ -205,8 +204,8 @@ fn generate_from_implementations( meta: &TypeItemAttributes, all_parameters: &[FromImplParameters], ) -> Vec { - let from_trait_path = util::path(&["convert", "FromColorUnclamped"], meta.internal); - let into_trait_path = util::path(&["convert", "IntoColorUnclamped"], meta.internal); + let from_trait_path = util::path(["convert", "FromColorUnclamped"], meta.internal); + let into_trait_path = util::path(["convert", "IntoColorUnclamped"], meta.internal); let (_, type_generics, _) = generics.split_for_impl(); @@ -282,9 +281,9 @@ fn generate_from_alpha_implementation( generics: &Generics, meta: &TypeItemAttributes, ) -> TokenStream2 { - let from_trait_path = util::path(&["convert", "FromColorUnclamped"], meta.internal); - let into_trait_path = util::path(&["convert", "IntoColorUnclamped"], meta.internal); - let alpha_path = util::path(&["Alpha"], meta.internal); + let from_trait_path = util::path(["convert", "FromColorUnclamped"], meta.internal); + let into_trait_path = util::path(["convert", "IntoColorUnclamped"], meta.internal); + let alpha_path = util::path(["Alpha"], meta.internal); let mut impl_generics = generics.clone(); impl_generics.params.push(parse_quote!(_C)); @@ -316,9 +315,9 @@ fn generate_from_alpha_implementation_with_internal( alpha_property: &IdentOrIndex, alpha_type: &Type, ) -> TokenStream2 { - let from_trait_path = util::path(&["convert", "FromColorUnclamped"], meta.internal); - let into_trait_path = util::path(&["convert", "IntoColorUnclamped"], meta.internal); - let alpha_path = util::path(&["Alpha"], meta.internal); + let from_trait_path = util::path(["convert", "FromColorUnclamped"], meta.internal); + let into_trait_path = util::path(["convert", "IntoColorUnclamped"], meta.internal); + let alpha_path = util::path(["Alpha"], meta.internal); let (_, type_generics, _) = generics.split_for_impl(); let mut impl_generics = generics.clone(); diff --git a/palette_derive/src/convert/util.rs b/palette_derive/src/convert/util.rs index ed2b7b769..4422e27bc 100644 --- a/palette_derive/src/convert/util.rs +++ b/palette_derive/src/convert/util.rs @@ -17,8 +17,8 @@ pub fn white_point_type( .map(|white_point| (white_point.clone(), Some(WhitePointSource::WhitePoint))) .or_else(|| { rgb_standard.map(|rgb_standard| { - let rgb_standard_path = util::path(&["rgb", "RgbStandard"], internal); - let rgb_space_path = util::path(&["rgb", "RgbSpace"], internal); + let rgb_standard_path = util::path(["rgb", "RgbStandard"], internal); + let rgb_space_path = util::path(["rgb", "RgbSpace"], internal); ( parse_quote!(<<#rgb_standard as #rgb_standard_path>::Space as #rgb_space_path>::WhitePoint), Some(WhitePointSource::RgbStandard), @@ -27,7 +27,7 @@ pub fn white_point_type( }) .or_else(|| { luma_standard.map(|luma_standard| { - let luma_standard_path = util::path(&["luma", "LumaStandard"], internal); + let luma_standard_path = util::path(["luma", "LumaStandard"], internal); ( parse_quote!(<#luma_standard as #luma_standard_path>::WhitePoint), Some(WhitePointSource::LumaStandard), @@ -59,7 +59,7 @@ pub fn get_convert_color_type( match color { "Luma" => { - let luma_standard_path = util::path(&["luma", "LumaStandard"], internal); + let luma_standard_path = util::path(["luma", "LumaStandard"], internal); if let Some(luma_standard) = luma_standard { ( @@ -82,8 +82,8 @@ pub fn get_convert_color_type( } } "Rgb" | "Hsl" | "Hsv" | "Hwb" => { - let rgb_standard_path = util::path(&["rgb", "RgbStandard"], internal); - let rgb_space_path = util::path(&["rgb", "RgbSpace"], internal); + let rgb_standard_path = util::path(["rgb", "RgbStandard"], internal); + let rgb_space_path = util::path(["rgb", "RgbSpace"], internal); if let Some(rgb_standard) = rgb_standard { ( diff --git a/palette_derive/src/meta/mod.rs b/palette_derive/src/meta/mod.rs index 0d05dea0b..ab01c9b00 100644 --- a/palette_derive/src/meta/mod.rs +++ b/palette_derive/src/meta/mod.rs @@ -178,10 +178,8 @@ pub enum IdentOrIndex { impl PartialEq for IdentOrIndex { fn eq(&self, other: &IdentOrIndex) -> bool { match (self, other) { - (&IdentOrIndex::Index(ref this), &IdentOrIndex::Index(ref other)) => { - this.index == other.index - } - (&IdentOrIndex::Ident(ref this), &IdentOrIndex::Ident(ref other)) => this == other, + (IdentOrIndex::Index(this), IdentOrIndex::Index(other)) => this.index == other.index, + (IdentOrIndex::Ident(this), IdentOrIndex::Ident(other)) => this == other, _ => false, } }