Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 7, 2024
1 parent 32a082b commit dd0c2fe
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ within scope.

This crate offers the following traits:

### HetPixel
### `HetPixel`

The most foundational pixel trait implemented by every pixel type.

Expand Down Expand Up @@ -138,7 +138,7 @@ let rgba = rgba.map_same(|c| c * 2);
assert_eq!(rgba, Rgba::<u16> {r: 0, g: 0, b: 510, a: 0});
```

### GainAlpha
### `GainAlpha`

A way to add alpha to a pixel type in various ways.

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn main() {
println!("{}", serde_json::to_string(&color).unwrap());

let color: Rgb<u8> = serde_json::from_str("{\"r\":10,\"g\":20,\"b\":30}").unwrap();
println!("{}", color);
println!("{color}");
}
2 changes: 1 addition & 1 deletion src/core_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ fn test_16_fmt() {
extern crate std;

let a = Argb::<u16>::new_argb(1, 0x1234, 3, 65535);
assert_eq!("argb(#000112340003FFFF)", &std::format!("{:X}", a));
assert_eq!("argb(#000112340003FFFF)", &std::format!("{a:X}"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/formats/gray_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<T, A> Deref for GrayAlpha_v08<T, A> {

/// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type.
fn deref(&self) -> &GrayA<T, A> {
unsafe { &*(self as *const Self as *const GrayA<T, A>) }
unsafe { &*(self as *const Self).cast::<GrayA<T, A>>() }
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/legacy/alt.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<T, A> GrayAlpha<T, A> {
/// Provide a mutable view of only `Gray` component (leaving out alpha).
#[inline(always)]
pub fn gray_mut(&mut self) -> &mut Gray<T> {
unsafe { &mut *(self as *mut _ as *mut _) }
unsafe { &mut *(self as *mut Self).cast() }
}
}

Expand All @@ -97,7 +97,7 @@ impl<T: Copy, A: Clone> GrayAlpha<T, A> {

/// 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)
}

Expand Down Expand Up @@ -136,14 +136,14 @@ impl<T> ComponentSlice<T> for GrayAlpha<T> {
#[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::<T>(), 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::<T>(), 2)
}
}
}
Expand Down Expand Up @@ -196,15 +196,15 @@ impl<T> ComponentSlice<T> for [Gray<T>] {
impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u8> {
#[inline(always)]
fn from(other: Gray<T>) -> Self {
GrayAlpha(other.0, 0xFF)
Self(other.0, 0xFF)
}
}

/// Assumes 65535 is opaque
impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u16> {
#[inline(always)]
fn from(other: Gray<T>) -> Self {
GrayAlpha(other.0, 0xFFFF)
Self(other.0, 0xFFFF)
}
}

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/internal/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
#[must_use] pub fn checked_add(self, rhs: $ty<$field_ty>) -> Option<Self> {
Some($ty {
$(
$field: self.$field.checked_add(rhs.$field)?,
Expand All @@ -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<Self> {
#[must_use] pub fn checked_sub(self, rhs: $ty<$field_ty>) -> Option<Self> {
Some($ty {
$(
$field: self.$field.checked_sub(rhs.$field)?,
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/internal/rgb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::pixel::*;
use crate::alt::*;
use crate::*;
use super::pixel::{ColorComponentMap, ComponentSlice};

impl<T> BGR<T> {
/// Convenience function for creating a new pixel
Expand Down Expand Up @@ -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());
}
Expand Down
39 changes: 20 additions & 19 deletions src/legacy/internal/rgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::pixel::*;
use crate::alt::*;
use crate::*;
use super::pixel::{ColorComponentMap, ComponentSlice};

impl<T, A> Rgba<T, A> {
#[inline(always)]
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<T> Argb<T> {
/// 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 }
}
}

Expand All @@ -50,7 +50,7 @@ impl<T, A> Argb<T, A> {
/// 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 }
}
}

Expand All @@ -60,7 +60,7 @@ impl<T> Abgr<T> {
/// 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 }
}
}

Expand All @@ -70,7 +70,7 @@ impl<T, A> Abgr<T, A> {
/// 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 }
}
}

Expand Down Expand Up @@ -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 }
}

Expand Down Expand Up @@ -235,7 +235,7 @@ impl<T, A> Rgba<T, A> {
/// Useful to change color without changing opacity.
#[inline(always)]
pub fn rgb_mut(&mut self) -> &mut Rgb<T> {
unsafe { &mut *(self as *mut _ as *mut Rgb<T>) }
unsafe { &mut *(self as *mut Self).cast::<Rgb<T>>() }
}
}

Expand All @@ -245,14 +245,14 @@ impl<T, A> BGRA<T, A> {
#[inline(always)]
#[deprecated(note = "This function will change. Use bgr_mut()")]
pub fn rgb_mut(&mut self) -> &mut Bgr<T> {
unsafe { &mut *(self as *mut _ as *mut Bgr<T>) }
unsafe { &mut *(self as *mut Self).cast::<Bgr<T>>() }
}

/// 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<T> {
unsafe { &mut *(self as *mut _ as *mut Bgr<T>) }
unsafe { &mut *(self as *mut Self).cast::<Bgr<T>>() }
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dd0c2fe

Please sign in to comment.