diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 20e462d7d..0c896aa2f 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -1,7 +1,16 @@ on: + push: + branches: + - master + paths: + - '**.rs' pull_request: paths: - '**.rs' + # `workflow_dispatch` allows CodSpeed to trigger backtest + # performance analysis in order to generate initial data. + workflow_dispatch: + name: Benchmark pull requests jobs: runBenchmark: @@ -11,9 +20,17 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: dtolnay/rust-toolchain@stable - - uses: boa-dev/criterion-compare-action@v3 + - name: Setup rust toolchain, cache and cargo-codspeed binary + uses: moonrepo/setup-rust@v0 + with: + channel: stable + cache-target: release + bins: cargo-codspeed + - name: Build the benchmark target(s) + run: cargo codspeed build -p palette --features wide + - name: Run the benchmarks + uses: CodSpeedHQ/action@v1 with: - token: ${{ secrets.GITHUB_TOKEN }} - branchName: ${{ github.base_ref }} - features: "wide" + run: cargo codspeed run + working-directory: palette # mimics how tests work, so the colors CSV can be found + token: ${{ secrets.CODSPEED_TOKEN }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1bcf4b555..8997d5ac7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,34 +11,58 @@ on: name: Continuous integration jobs: - compile_and_test: - name: Compile and test + compile_and_test_msrv: + name: Compile and test MSRV strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - toolchain: [1.60.0, stable, beta, nightly] runs-on: ${{ matrix.os }} env: RUSTFLAGS: -D warnings steps: - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@master + - uses: dtolnay/rust-toolchain@1.60.0 with: - toolchain: ${{ matrix.toolchain }} - targets: thumbv6m-none-eabi + 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 test -v - - name: Test features + 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 shell: bash working-directory: palette run: bash ../scripts/test_features.sh - - name: "Test #[no_std]" - if: ${{ runner.os == 'Linux' && matrix.toolchain == 'nightly' }} + check_stable_beta_nightly: + name: Quick check + strategy: + matrix: + toolchain: [beta, stable, nightly] + runs-on: ubuntu-latest + env: + RUSTFLAGS: -D warnings + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.toolchain }} + components: clippy + - name: Check all features + run: cargo clippy -v -p palette --all-features + no_std: + name: "Test #[no_std]" + runs-on: ubuntu-latest + env: + RUSTFLAGS: -D warnings + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@nightly + with: + targets: thumbv6m-none-eabi + - name: "Build with #[no_std]" run: cargo build -v --package no_std_test --features nightly --target thumbv6m-none-eabi miri: name: Miri tests @@ -61,7 +85,9 @@ jobs: name: ci if: success() needs: - - compile_and_test + - compile_and_test_msrv + - check_stable_beta_nightly + - no_std - miri runs-on: ubuntu-latest steps: diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 000000000..34d2e3e1c --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,32 @@ +on: + pull_request: + branches: + - master + paths: + - '**.rs' + push: + branches: + - master + paths: + - '**.rs' + +name: Test coverage + +jobs: + coverage: + runs-on: ubuntu-latest + env: + CARGO_TERM_COLOR: always + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: dtolnay/rust-toolchain@nightly + - uses: taiki-e/install-action@cargo-llvm-cov + - name: Collect code coverage + run: cargo +nightly llvm-cov --all-features --workspace --exclude no_std_test --codecov --doctests --output-path codecov.json + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: codecov.json 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/blend/equations.rs b/palette/src/blend/equations.rs index 91596d10e..d33bed032 100644 --- a/palette/src/blend/equations.rs +++ b/palette/src/blend/equations.rs @@ -109,8 +109,8 @@ where .apply_to(source.clone(), destination.clone()); ( - alpha_src_param.mul_constant(source.alpha.clone()), - alpha_dst_param.mul_constant(destination.alpha.clone()), + alpha_src_param.mul_constant(source.alpha), + alpha_dst_param.mul_constant(destination.alpha), ) }; 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/hsl.rs b/palette/src/hsl.rs index 2e7a904bc..629566a06 100644 --- a/palette/src/hsl.rs +++ b/palette/src/hsl.rs @@ -397,7 +397,7 @@ where // branch we should be in. This makes the sum of all three combine as // expected. let red_m = lazy_select! { - if x.clone() => y.clone().select(red.clone(), -red), + if x => y.clone().select(red.clone(), -red), else => T::zero(), }; let green_m = lazy_select! { diff --git a/palette/src/hsv.rs b/palette/src/hsv.rs index 63a560d67..b05b8edd1 100644 --- a/palette/src/hsv.rs +++ b/palette/src/hsv.rs @@ -392,7 +392,7 @@ where // branch we should be in. This makes the sum of all three combine as // expected. let red_m = lazy_select! { - if x.clone() => y.clone().select(red.clone(), -red), + if x => y.clone().select(red.clone(), -red), else => T::zero(), }; let green_m = lazy_select! { 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 ::std::result::Result> { let DeriveInput { ident, - generics: original_generics, + generics, data, attrs, .. } = syn::parse(item).map_err(|error| vec![error])?; - let generics = original_generics.clone(); let mut item_meta: TypeItemAttributes = parse_namespaced_attributes(attrs)?; @@ -93,7 +92,7 @@ fn prepare_from_impl( white_point_source: Option, ) -> 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 +155,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 +170,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 +203,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 +280,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 +314,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, } } diff --git a/scripts/test_features.sh b/scripts/test_features.sh index 0497cb1b7..06d8e0def 100644 --- a/scripts/test_features.sh +++ b/scripts/test_features.sh @@ -36,10 +36,10 @@ echo -e "features: $features\n" #Test without any optional feature echo testing with --no-default-features --features "$required_features" -cargo test --no-default-features --features "$required_features" +cargo test --tests --no-default-features --features "$required_features" #Isolated test of each optional feature for feature in $features; do echo testing with --no-default-features --features "\"$feature $required_features\"" - cargo test --no-default-features --features "$feature $required_features" + cargo test --tests --no-default-features --features "$feature $required_features" done