Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 7, 2024
1 parent 45e464a commit 32a082b
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rgb::*;
// Run using: cargo run --features=serde --example serde

fn main() {
let color = Rgb { r:255_u8, g:0, b:100 };
let color = Rgb { r: 255_u8, g: 0, b: 100 };
println!("{}", serde_json::to_string(&color).unwrap());

let color: Rgb<u8> = serde_json::from_str("{\"r\":10,\"g\":20,\"b\":30}").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/bytemuck.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Abgr, Argb, Bgr, Bgra, Gray_v09, GrayA, Grb, Rgb, Rgba, Rgbw};
use crate::{Abgr, Argb, Bgr, Bgra, GrayA, Gray_v09, Grb, Rgb, Rgba, Rgbw};

pub use ::bytemuck::*;

Expand Down
5 changes: 1 addition & 4 deletions src/core_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ trait_impls_with_alpha!(GrayA, 2, [v, a], "graya({}, {})", "graya(#{:0w$X}{:0w$X
use crate::formats::gray_alpha::GrayAlpha_v08;
trait_impls_with_alpha!(GrayAlpha_v08, 2, [0, 1], "graya_v0.8({}, {})", "graya_v0.8(#{:0w$X}{:0w$X})", "graya_v0.8(#{:0w$x}{:0w$x})");


#[cfg(test)]
#[test]
fn test_16_fmt() {
Expand All @@ -478,12 +477,10 @@ fn test_16_fmt() {
assert_eq!("argb(#000112340003FFFF)", &std::format!("{:X}", a));
}


#[test]
#[allow(deprecated)]
fn convert_array() {
use crate::{BGR8, BGRA8};
use crate::{RGB8, RGBA8};
use crate::{BGR8, BGRA8, RGB8, RGBA8};

assert_eq!(RGB8::from([1, 2, 3]), RGB8::new(1, 2, 3));
assert_eq!(Into::<[u8; 3]>::into(RGB8::new(1, 2, 3)), [1, 2, 3]);
Expand Down
4 changes: 1 addition & 3 deletions src/formats/gray_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +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 as *const GrayA<T, A>) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/from.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Abgr, Argb, Bgr, Bgra, Gray_v09, GrayA, Grb, Rgb, Rgba};
use crate::{Abgr, Argb, Bgr, Bgra, GrayA, Gray_v09, Grb, Rgb, Rgba};

macro_rules! without_alpha {
($from_type:ident, $self_type:ident, {$($bit:tt),*}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/inherent_impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Abgr, Argb, Bgr, Bgra, Gray_v09, GrayA, Grb, Rgb, Rgba, Rgbw};
use crate::{Abgr, Argb, Bgr, Bgra, GrayA, Gray_v09, Grb, Rgb, Rgba, Rgbw};

macro_rules! inherent_impls {
($name:ident, $new_fn:ident, [$($field:tt $var:ident),*]) => {
Expand Down
3 changes: 1 addition & 2 deletions src/legacy/internal/pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ fn shared_impl() {
}

impl<Pixel: Clone + crate::Pod> SharedPixelBuffer<Pixel>
where
[Pixel]: crate::ComponentBytes<u8>,
where [Pixel]: crate::ComponentBytes<u8>
{
pub fn as_bytes(&self) -> &[u8] {
self.data.as_slice().as_bytes()
Expand Down
4 changes: 1 addition & 3 deletions src/legacy/internal/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::*;
impl<T> BGR<T> {
/// Convenience function for creating a new pixel
/// Warning: The order of arguments is R,G,B
#[deprecated(
note = "This function has a misleading order of arguments. Use BGR{} literal instead"
)]
#[deprecated(note = "This function has a misleading order of arguments. Use BGR{} literal instead")]
#[inline(always)]
pub const fn new(r: T, g: T, b: T) -> Self {
Self { b, g, r }
Expand Down
2 changes: 1 addition & 1 deletion src/num_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Abgr, Argb, Bgr, Bgra, Gray_v09, GrayA, Grb, Rgb, Rgba, Rgbw};
use crate::{Abgr, Argb, Bgr, Bgra, GrayA, Gray_v09, Grb, Rgb, Rgba, Rgbw};

/// Re-exports from [the `num-traits` crate](https://lib.rs/crates/num-traits).
pub use num_traits::ops::checked::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub};
Expand Down
6 changes: 2 additions & 4 deletions src/pixel_traits/arraylike.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::{
borrow::{Borrow, BorrowMut},
ops::{Index, IndexMut},
};
use core::borrow::{Borrow, BorrowMut};
use core::ops::{Index, IndexMut};

/// A trait used when returning arrays from the two pixel traits due to the lack of the const
/// generic expressions feature on stable rust.
Expand Down
8 changes: 4 additions & 4 deletions src/pixel_traits/has_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub trait HasAlpha: HetPixel {
///
/// # Examples
/// ```
/// use rgb::{Rgba, HasAlpha};
/// use rgb::{HasAlpha, Rgba};
///
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// assert_eq!(HasAlpha::alpha(&rgba), 50);
///
Expand All @@ -42,9 +42,9 @@ pub trait HasAlpha: HetPixel {
///
/// # Examples
/// ```
/// use rgb::{Rgba, HasAlpha};
/// use rgb::{HasAlpha, Rgba};
///
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let alpha = rgba.alpha_mut();
///
Expand Down
35 changes: 18 additions & 17 deletions src/pixel_traits/het_pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// assert_eq!(rgb.color_array(), [0, 10, 100]);
/// assert_eq!(rgba.color_array(), [0, 10, 100]);
Expand All @@ -91,8 +91,8 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let mut rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// *rgb.color_array_mut()[1] = 40;
/// *rgba.color_array_mut()[2] = 40;
Expand All @@ -111,8 +111,8 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let mut rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// assert_eq!(rgb.alpha_opt(), None);
/// assert_eq!(rgba.alpha_opt(), Some(50));
Expand All @@ -128,8 +128,8 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let mut rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let f = |a: Option<&mut u8>| {
/// if let Some(a) = a {
Expand All @@ -153,7 +153,8 @@ pub trait HetPixel: Copy {
/// # Examples
///
/// ```
/// use rgb::{HetPixel, Rgb, Rgba, error::TryFromColorsAlphaError};
/// use rgb::error::TryFromColorsAlphaError;
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let mut values2 = [0_u8, 10];
/// let mut values4 = [0_u8, 10, 100, 40];
Expand Down Expand Up @@ -181,15 +182,15 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let f = |color: u8| {
/// u16::from(color) * 10
/// };
///
/// assert_eq!(rgb.map_colors(f), Rgb {r: 0, g: 100, b: 1000});
/// assert_eq!(rgba.map_colors(f), Rgba {r: 0, g: 100, b: 1000, a: 50});
/// assert_eq!(rgb.map_colors(f), Rgb { r: 0, g: 100, b: 1000 });
/// assert_eq!(rgba.map_colors(f), Rgba { r: 0, g: 100, b: 1000, a: 50 });
/// ```
#[doc(alias = "map_c")]
#[doc(alias = "map_gray")]
Expand All @@ -209,15 +210,15 @@ pub trait HetPixel: Copy {
/// ```
/// use rgb::{HetPixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let f = |color: u8| {
/// color / 2
/// };
///
/// assert_eq!(rgb.map_colors_same(f), Rgb {r: 0, g: 5, b: 50});
/// assert_eq!(rgba.map_colors_same(f), Rgba {r: 0, g: 5, b: 50, a: 50});
/// assert_eq!(rgb.map_colors_same(f), Rgb { r: 0, g: 5, b: 50 });
/// assert_eq!(rgba.map_colors_same(f), Rgba { r: 0, g: 5, b: 50, a: 50 });
/// ```
fn map_colors_same(&self, f: impl FnMut(Self::ColorComponent) -> Self::ColorComponent) -> Self;

Expand Down
34 changes: 17 additions & 17 deletions src/pixel_traits/pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub trait Pixel:
/// ```
/// use rgb::{Pixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// assert_eq!(rgb.to_array(), [0, 10, 100]);
/// assert_eq!(rgba.to_array(), [0, 10, 100, 50]);
Expand All @@ -53,8 +53,7 @@ pub trait Pixel:
#[doc(alias = "into_array")]
#[doc(alias = "component_array")]
fn to_array(&self) -> Self::ComponentArray<Self::Component>
where
Self::ComponentArray<Self::Component>: Copy;
where Self::ComponentArray<Self::Component>: Copy;

/// Casts a reference of the pixel to an array reference of the pixel's
/// components.
Expand Down Expand Up @@ -100,8 +99,8 @@ pub trait Pixel:
/// ```
/// use rgb::{Pixel, Rgb, Rgba};
///
/// let mut rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let mut rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let mut rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let mut rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// *rgb.each_mut()[1] = 40;
/// *rgba.each_mut()[2] = 40;
Expand All @@ -120,16 +119,17 @@ pub trait Pixel:
/// # Examples
///
/// ```
/// use rgb::{Pixel, Rgb, Rgba, error::TryFromComponentsError};
/// use rgb::error::TryFromComponentsError;
/// use rgb::{Pixel, Rgb, Rgba};
///
/// let mut values2 = [0_u8, 10];
/// let mut values4 = [0_u8, 10, 100, 40];
///
/// assert_eq!(Rgb::try_from_components(values2), Err(TryFromComponentsError));
/// assert_eq!(Rgba::try_from_components(values2), Err(TryFromComponentsError));
///
/// assert_eq!(Rgb::try_from_components(values4), Ok(Rgb {r: 0, g: 10, b: 100}));
/// assert_eq!(Rgba::try_from_components(values4), Ok(Rgba {r: 0, g: 10, b: 100, a: 40}));
/// assert_eq!(Rgb::try_from_components(values4), Ok(Rgb { r: 0, g: 10, b: 100 }));
/// assert_eq!(Rgba::try_from_components(values4), Ok(Rgba { r: 0, g: 10, b: 100, a: 40 }));
/// ```
fn try_from_components(
components: impl IntoIterator<Item = Self::Component>,
Expand All @@ -145,15 +145,15 @@ pub trait Pixel:
/// ```
/// use rgb::{Pixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let f = |color: u8| {
/// u16::from(color) * 10
/// };
///
/// assert_eq!(rgb.map(f), Rgb {r: 0, g: 100, b: 1000});
/// assert_eq!(rgba.map(f), Rgba {r: 0, g: 100, b: 1000, a: 500});
/// assert_eq!(rgb.map(f), Rgb { r: 0, g: 100, b: 1000 });
/// assert_eq!(rgba.map(f), Rgba { r: 0, g: 100, b: 1000, a: 500 });
/// ```
fn map<U>(&self, f: impl FnMut(Self::Component) -> U) -> Self::SelfType<U, U> where U: Copy;

Expand All @@ -167,15 +167,15 @@ pub trait Pixel:
/// ```
/// use rgb::{Pixel, Rgb, Rgba};
///
/// let rgb = Rgb {r: 0_u8, g: 10, b: 100};
/// let rgba = Rgba {r: 0_u8, g: 10, b: 100, a: 50};
/// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
/// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
///
/// let f = |color: u8| {
/// color / 2
/// };
///
/// assert_eq!(rgb.map_same(f), Rgb {r: 0, g: 5, b: 50});
/// assert_eq!(rgba.map_same(f), Rgba {r: 0, g: 5, b: 50, a: 25});
/// assert_eq!(rgb.map_same(f), Rgb { r: 0, g: 5, b: 50 });
/// assert_eq!(rgba.map_same(f), Rgba { r: 0, g: 5, b: 50, a: 25 });
/// ```
fn map_same(&self, f: impl FnMut(Self::Component) -> Self::Component) -> Self;
}
Expand Down
5 changes: 2 additions & 3 deletions src/tuples.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::formats::gray::Gray_v08;
use crate::formats::gray::Gray_v09;
use crate::formats::gray::{Gray_v08, Gray_v09};
use crate::formats::gray_alpha::GrayAlpha_v08;
use crate::{Abgr, Argb, Bgr, Bgra, Grb, GrayA, Rgb, Rgba, Rgbw};
use crate::{Abgr, Argb, Bgr, Bgra, GrayA, Grb, Rgb, Rgba, Rgbw};

macro_rules! tuple_conversion {
($name:ident, 1, [$($bit:tt:$num:tt),*]) => {
Expand Down

0 comments on commit 32a082b

Please sign in to comment.