From dd0c2fea0e8c031a54afd45f5733d9c00ece2001 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 7 Aug 2024 15:11:18 +0100 Subject: [PATCH] Clippy --- README.md | 6 +++--- examples/serde.rs | 2 +- src/core_traits.rs | 2 +- src/formats/gray_alpha.rs | 2 +- src/legacy/alt.rs | 16 +++++++-------- src/legacy/internal/ops.rs | 4 ++-- src/legacy/internal/rgb.rs | 3 ++- src/legacy/internal/rgba.rs | 39 +++++++++++++++++++------------------ src/legacy/mod.rs | 2 +- src/lib.rs | 2 +- 10 files changed, 40 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 50b1dab..99daf65 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ within scope. This crate offers the following traits: -### HetPixel +### `HetPixel` The most foundational pixel trait implemented by every pixel type. @@ -138,7 +138,7 @@ let rgba = rgba.map_same(|c| c * 2); assert_eq!(rgba, Rgba:: {r: 0, g: 0, b: 510, a: 0}); ``` -### GainAlpha +### `GainAlpha` A way to add alpha to a pixel type in various ways. @@ -152,7 +152,7 @@ assert_eq!(Rgb {r: 0, g: 0, b: 0}.with_alpha(255), expected); assert_eq!(Rgba {r: 0, g: 0, b: 0, a: 0}.with_alpha(255), expected); ``` -### HasAlpha +### `HasAlpha` A trait only implemented on pixels that have an alpha component. diff --git a/examples/serde.rs b/examples/serde.rs index 4baf997..73ffa41 100644 --- a/examples/serde.rs +++ b/examples/serde.rs @@ -7,5 +7,5 @@ fn main() { println!("{}", serde_json::to_string(&color).unwrap()); let color: Rgb = serde_json::from_str("{\"r\":10,\"g\":20,\"b\":30}").unwrap(); - println!("{}", color); + println!("{color}"); } diff --git a/src/core_traits.rs b/src/core_traits.rs index 531ff68..31b887f 100644 --- a/src/core_traits.rs +++ b/src/core_traits.rs @@ -474,7 +474,7 @@ fn test_16_fmt() { extern crate std; let a = Argb::::new_argb(1, 0x1234, 3, 65535); - assert_eq!("argb(#000112340003FFFF)", &std::format!("{:X}", a)); + assert_eq!("argb(#000112340003FFFF)", &std::format!("{a:X}")); } #[test] diff --git a/src/formats/gray_alpha.rs b/src/formats/gray_alpha.rs index 08c01e6..a3959c5 100644 --- a/src/formats/gray_alpha.rs +++ b/src/formats/gray_alpha.rs @@ -30,7 +30,7 @@ impl Deref for GrayAlpha_v08 { /// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type. fn deref(&self) -> &GrayA { - unsafe { &*(self as *const Self as *const GrayA) } + unsafe { &*(self as *const Self).cast::>() } } } diff --git a/src/legacy/alt.rs b/src/legacy/alt.rs index dc337f0..ec904f2 100644 --- a/src/legacy/alt.rs +++ b/src/legacy/alt.rs @@ -1,4 +1,4 @@ -use crate::legacy::internal::pixel::*; +use crate::legacy::internal::pixel::{ColorComponentMap, ComponentSlice}; use core::slice; pub use crate::formats::gray::Gray_v08 as Gray; @@ -83,7 +83,7 @@ impl GrayAlpha { /// Provide a mutable view of only `Gray` component (leaving out alpha). #[inline(always)] pub fn gray_mut(&mut self) -> &mut Gray { - unsafe { &mut *(self as *mut _ as *mut _) } + unsafe { &mut *(self as *mut Self).cast() } } } @@ -97,7 +97,7 @@ impl GrayAlpha { /// Create a new `GrayAlpha` with the new alpha value, but same gray value #[inline(always)] - pub fn with_alpha(&self, a: A) -> Self { + pub const fn with_alpha(&self, a: A) -> Self { Self(self.0, a) } @@ -136,14 +136,14 @@ impl ComponentSlice for GrayAlpha { #[inline(always)] fn as_slice(&self) -> &[T] { unsafe { - slice::from_raw_parts(self as *const Self as *const T, 2) + slice::from_raw_parts((self as *const Self).cast::(), 2) } } #[inline(always)] fn as_mut_slice(&mut self) -> &mut [T] { unsafe { - slice::from_raw_parts_mut(self as *mut Self as *mut T, 2) + slice::from_raw_parts_mut((self as *mut Self).cast::(), 2) } } } @@ -196,7 +196,7 @@ impl ComponentSlice for [Gray] { impl From> for GrayAlpha { #[inline(always)] fn from(other: Gray) -> Self { - GrayAlpha(other.0, 0xFF) + Self(other.0, 0xFF) } } @@ -204,7 +204,7 @@ impl From> for GrayAlpha { impl From> for GrayAlpha { #[inline(always)] fn from(other: Gray) -> Self { - GrayAlpha(other.0, 0xFFFF) + Self(other.0, 0xFFFF) } } @@ -226,7 +226,7 @@ fn gray() { let g: GRAY8 = 200.into(); let g = g.map(|c| c / 2); - assert_eq!(110, g.v + 10); + assert_eq!(110, g.0 + 10); assert_eq!(110, 10 + Gray(100).as_ref()); let ga: GRAYA8 = GrayAlpha(1, 2); diff --git a/src/legacy/internal/ops.rs b/src/legacy/internal/ops.rs index efcd38f..7c9969c 100644 --- a/src/legacy/internal/ops.rs +++ b/src/legacy/internal/ops.rs @@ -13,7 +13,7 @@ macro_rules! impl_struct_checked { /// `px.checked_add(px)` #[inline(always)] #[cfg_attr(feature = "num-traits", deprecated(note = "import `num_traits::CheckedAdd` instead, disable rgb's checked_fns feature"))] - pub fn checked_add(self, rhs: $ty<$field_ty>) -> Option { + #[must_use] pub fn checked_add(self, rhs: $ty<$field_ty>) -> Option { Some($ty { $( $field: self.$field.checked_add(rhs.$field)?, @@ -24,7 +24,7 @@ macro_rules! impl_struct_checked { /// `px.checked_sub(px)` #[inline(always)] #[cfg_attr(feature = "num-traits", deprecated(note = "import `num_traits::CheckedSub` instead, disable rgb's checked_fns feature"))] - pub fn checked_sub(self, rhs: $ty<$field_ty>) -> Option { + #[must_use] pub fn checked_sub(self, rhs: $ty<$field_ty>) -> Option { Some($ty { $( $field: self.$field.checked_sub(rhs.$field)?, diff --git a/src/legacy/internal/rgb.rs b/src/legacy/internal/rgb.rs index bdd109c..71a33e2 100644 --- a/src/legacy/internal/rgb.rs +++ b/src/legacy/internal/rgb.rs @@ -1,6 +1,6 @@ -use super::pixel::*; use crate::alt::*; use crate::*; +use super::pixel::{ColorComponentMap, ComponentSlice}; impl BGR { /// Convenience function for creating a new pixel @@ -177,6 +177,7 @@ mod rgb_test { #[cfg(feature = "as-bytes")] { + use crate::ComponentBytes; let v = vec![RGB::new(1u8, 2, 3), RGB::new(4, 5, 6)]; assert_eq!(&[1, 2, 3, 4, 5, 6], v.as_bytes()); } diff --git a/src/legacy/internal/rgba.rs b/src/legacy/internal/rgba.rs index d2f717a..1f769b4 100644 --- a/src/legacy/internal/rgba.rs +++ b/src/legacy/internal/rgba.rs @@ -1,6 +1,6 @@ -use super::pixel::*; use crate::alt::*; use crate::*; +use super::pixel::{ColorComponentMap, ComponentSlice}; impl Rgba { #[inline(always)] @@ -40,7 +40,7 @@ impl Argb { /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")] pub const fn new(r: T, g: T, b: T, a: T) -> Self { - Self { r, g, b, a } + Self { a, r, g, b } } } @@ -50,7 +50,7 @@ impl Argb { /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")] pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { - Self { r, g, b, a } + Self { a, r, g, b } } } @@ -60,7 +60,7 @@ impl Abgr { /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")] pub const fn new(r: T, g: T, b: T, a: T) -> Self { - Self { r, g, b, a } + Self { a, b, g, r } } } @@ -70,7 +70,7 @@ impl Abgr { /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")] pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { - Self { r, g, b, a } + Self { a, b, g, r } } } @@ -122,7 +122,7 @@ macro_rules! impl_rgba { /// Create a new RGBA with the new alpha value, but same RGB values #[inline(always)] - pub fn with_alpha(&self, a: A) -> Self { + pub const fn with_alpha(&self, a: A) -> Self { Self { r: self.r, g: self.g, b: self.b, a } } @@ -235,7 +235,7 @@ impl Rgba { /// Useful to change color without changing opacity. #[inline(always)] pub fn rgb_mut(&mut self) -> &mut Rgb { - unsafe { &mut *(self as *mut _ as *mut Rgb) } + unsafe { &mut *(self as *mut Self).cast::>() } } } @@ -245,14 +245,14 @@ impl BGRA { #[inline(always)] #[deprecated(note = "This function will change. Use bgr_mut()")] pub fn rgb_mut(&mut self) -> &mut Bgr { - unsafe { &mut *(self as *mut _ as *mut Bgr) } + unsafe { &mut *(self as *mut Self).cast::>() } } /// Provide a mutable view of only RGB components (leaving out alpha). /// Useful to change color without changing opacity. #[inline(always)] pub fn bgr_mut(&mut self) -> &mut Bgr { - unsafe { &mut *(self as *mut _ as *mut Bgr) } + unsafe { &mut *(self as *mut Self).cast::>() } } } @@ -333,7 +333,7 @@ impl_alpha_conv! {Rgb, Argb} fn rgba_test() { use crate::Pixel; - let neg = RGBA::new(1,2,3i32,1000).map(|x| -x); + let neg = Rgba::new(1,2,3i32,1000).map(|x| -x); assert_eq!(neg.r, -1); assert_eq!(neg.rgb().r, -1); assert_eq!(neg.g, -2); @@ -343,16 +343,16 @@ fn rgba_test() { assert_eq!(neg.a, -1000); assert_eq!(neg.map_alpha(|x| x+1).a, -999); assert_eq!(neg, neg.as_slice().iter().copied().collect()); - assert!(neg < RGBA::new(0,0,0,0)); + assert!(neg < Rgba::new(0,0,0,0)); - let neg = RGBA::new(1u8,2,3,4).map_rgb(|c| -(c as i16)); + let neg = Rgba::new(1u8,2,3,4).map_rgb(|c| -i16::from(c)); assert_eq!(-1i16, neg.r); assert_eq!(4i16, neg.a); - let neg = RGBA::new(1u8,2,3,4).map_c(|c| -(c as i16)); + let neg = Rgba::new(1u8,2,3,4).map_c(|c| -i16::from(c)); assert_eq!(-1i16, neg.r); assert_eq!(4u8, neg.a); - let mut px = RGBA{r:1,g:2,b:3,a:4}; + let mut px = Rgba{r:1,g:2,b:3,a:4}; px.as_mut_slice()[3] = 100; assert_eq!(1, px.rgb_mut().r); assert_eq!(2, px.rgb_mut().g); @@ -362,7 +362,7 @@ fn rgba_test() { #[cfg(feature = "as-bytes")] { - let v = vec![RGBA::new(1u8,2,3,4), RGBA::new(5,6,7,8)]; + let v = [Rgba::new(1u8,2,3,4), Rgba::new(5,6,7,8)]; assert_eq!(&[1,2,3,4,5,6,7,8], v.as_bytes()); } } @@ -396,16 +396,16 @@ fn bgra_test() { assert_eq!(neg.bgr().b, -3); assert_eq!(neg.a, -1000); assert_eq!(&[-3, -2, -1, -1000], neg.as_slice()); - assert!(neg < BGRA::new(0, 0, 0, 0)); + assert!(neg < Bgra::new(0, 0, 0, 0)); - let neg = BGRA::new(1u8, 2u8, 3u8, 4u8).map_rgb(|c| -(c as i16)); + let neg = Bgra::new(1u8, 2u8, 3u8, 4u8).map_rgb(|c| -i16::from(c)); assert_eq!(-1i16, neg.r); assert_eq!(4i16, neg.a); - let neg = BGRA::new(1u8, 2u8, 3u8, 4u8).map_c(|c| -(c as i16)); + let neg = Bgra::new(1u8, 2u8, 3u8, 4u8).map_c(|c| -i16::from(c)); assert_eq!(-1i16, neg.r); assert_eq!(4u8, neg.a); - let mut px = BGRA{r:1,g:2,b:3,a:-9}.with_alpha(4); + let mut px = Bgra{r:1,g:2,b:3,a:-9}.with_alpha(4); px.as_mut_slice()[3] = 100; assert_eq!(1, px.bgr_mut().r); assert_eq!(2, px.bgr_mut().g); @@ -415,6 +415,7 @@ fn bgra_test() { #[cfg(feature = "as-bytes")] { + use crate::ComponentBytes; let v = vec![BGRA::new(3u8, 2, 1, 4), BGRA::new(7, 6, 5, 8)]; assert_eq!(&[1, 2, 3, 4, 5, 6, 7, 8], v.as_bytes()); } diff --git a/src/legacy/mod.rs b/src/legacy/mod.rs index fa359df..7f9dcbd 100644 --- a/src/legacy/mod.rs +++ b/src/legacy/mod.rs @@ -115,7 +115,7 @@ fn bytes() { assert_eq!(&[1,2,3,4], rgba_bytes); assert_eq!(&[rgba], rgba_bytes.as_rgba()); rgba_bytes[3] = 99; - assert_eq!(RGBA8::new(1,2,3,99), rgba_arr.as_bytes().into_iter().cloned().collect()); + assert_eq!(RGBA8::new(1,2,3,99), rgba_arr.as_bytes().iter().copied().collect()); } let rgb = RGB16::new(1,2,3); diff --git a/src/lib.rs b/src/lib.rs index 08bf892..6b9b51e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ pub mod prelude { pub use crate::Pixel; } -/// TryFrom errors +/// `TryFrom` errors pub mod error { pub use crate::pixel_traits::het_pixel::TryFromColorsAlphaError; pub use crate::pixel_traits::pixel::TryFromComponentsError;