Skip to content

Commit

Permalink
Clippy/fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 4, 2025
1 parent 8018d00 commit 2e810a3
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 deletions.
1 change: 1 addition & 0 deletions imagequant-sys/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub unsafe extern "C" fn liq_result_set_progress_callback(result: &mut liq_resul
result.inner.set_progress_callback(move |f| if callback(f, user_info) == 0 { ControlFlow::Break} else { ControlFlow::Continue});
}

#[allow(clippy::cast_ptr_alignment)]
unsafe fn attr_to_liq_attr_ptr(ptr: &Attributes) -> &liq_attr {
let liq_attr = std::ptr::NonNull::<liq_attr>::dangling();
let outer_addr = std::ptr::addr_of!(*liq_attr.as_ptr()) as isize;
Expand Down
4 changes: 2 additions & 2 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ impl Attributes {

#[inline(always)]
pub(crate) fn verbose_print(&self, msg: impl AsRef<str>) {
fn _print(a: &Attributes, msg: &str) {
fn print_(a: &Attributes, msg: &str) {
if let Some(f) = &a.log_callback {
f(a, msg);
}
}
_print(self, msg.as_ref());
print_(self, msg.as_ref());
}

#[inline]
Expand Down
7 changes: 5 additions & 2 deletions src/hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Histogram {
}

#[inline(always)]
fn posterize_mask(&self) -> u32 {
const fn posterize_mask(&self) -> u32 {
let channel_mask = 255 << self.posterize_bits;
u32::from_ne_bytes([channel_mask, channel_mask, channel_mask, channel_mask])
}
Expand Down Expand Up @@ -414,7 +414,10 @@ impl std::hash::Hasher for U32Hasher {

/// ignores the index
#[derive(PartialEq, Debug)]
pub(crate) struct HashColor { pub rgba: RGBA, pub index: PalIndex }
pub(crate) struct HashColor {
pub rgba: RGBA,
pub index: PalIndex,
}

#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for HashColor {
Expand Down
6 changes: 3 additions & 3 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ impl<'pixels> Image<'pixels> {
/// Width of the image in pixels
#[must_use]
#[inline(always)]
pub fn width(&self) -> usize {
pub const fn width(&self) -> usize {
self.px.width as _
}

/// Height of the image in pixels
#[must_use]
#[inline(always)]
pub fn height(&self) -> usize {
pub const fn height(&self) -> usize {
self.px.height as _
}

Expand Down Expand Up @@ -331,7 +331,7 @@ impl<'pixels> Image<'pixels> {
let pixels_rows = match PixelsSource::for_pixels(pixels, width, height, stride) {
Ok(p) => p,
Err(e) => {
attr.verbose_print(format!("Buffer length is {} bytes, which is not enough for {}×{}×4 RGBA bytes", pixels_len*4, stride, height));
attr.verbose_print(format!("Buffer length is {} bytes, which is not enough for {}×{}×4 RGBA bytes", pixels_len * 4, stride, height));
return Err(e);
},
};
Expand Down
16 changes: 8 additions & 8 deletions src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ impl Kmeans {

// chunk size is a trade-off between parallelization and overhead
hist.items.par_chunks_mut(256).for_each({
let tls = &tls; move |batch| {
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Self::new(len))));
if let Ok(ref mut kmeans) = *kmeans.0.borrow_mut() {
kmeans.iterate_batch(batch, &n, colors, adjust_weight);
let tls = &tls;
move |batch| {
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Self::new(len))));
if let Ok(ref mut kmeans) = *kmeans.0.borrow_mut() {
kmeans.iterate_batch(batch, &n, colors, adjust_weight);
}
}
}});
});

let diff = tls.into_iter()
.map(|c| c.0.into_inner())
.reduce(Self::try_merge)
.transpose()?
.map_or(0., |kmeans| {
kmeans.finalize(palette) / total
});
.map_or(0., |kmeans| kmeans.finalize(palette) / total);

replace_unused_colors(palette, hist)?;
Ok(diff)
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ fn sizes() {

#[doc(hidden)]
pub fn _unstable_internal_kmeans_bench() -> impl FnMut() {
use crate::pal::PalF;
use crate::pal::PalPop;
use crate::pal::{PalF, PalPop};

let attr = new();
let mut h = hist::Histogram::new(&attr);
Expand Down Expand Up @@ -294,15 +293,19 @@ impl Eq for OrdFloat<f32> {

impl Ord for OrdFloat<f32> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal) }
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal)
}
}

impl Eq for OrdFloat<f64> {
}

impl Ord for OrdFloat<f64> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal) }
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal)
}
}

impl OrdFloat<f32> {
Expand All @@ -329,16 +332,16 @@ fn test_fixed_colors() {
}).collect::<Vec<_>>();
h.add_colors(&tmp, 0.).unwrap();
for f in 200..255 {
h.add_fixed_color(RGBA::new(f,f,f,255), 0.).unwrap();
h.add_fixed_color(RGBA::new(f, f, f, 255), 0.).unwrap();
}
let mut r = h.quantize(&attr).unwrap();
let pal = r.palette();

for (i, c) in (200..255).enumerate() {
assert_eq!(pal[i], RGBA::new(c,c,c,255));
assert_eq!(pal[i], RGBA::new(c, c, c, 255));
}

for c in 0..128 {
assert!(pal[55..].iter().any(|&p| p == RGBA::new(c,c,c,255)));
assert!(pal[55..].iter().any(|&p| p == RGBA::new(c, c, c, 255)));
}
}
8 changes: 4 additions & 4 deletions src/pal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ impl f_pixel {

#[cfg(all(target_feature = "neon", target_arch = "aarch64"))]
#[inline(always)]
pub fn diff(&self, other: &f_pixel) -> f32 {
pub fn diff(&self, other: &Self) -> f32 {
unsafe {
use std::arch::aarch64::*;

let px = vld1q_f32((self as *const f_pixel).cast::<f32>());
let py = vld1q_f32((other as *const f_pixel).cast::<f32>());
let px = vld1q_f32((self as *const Self).cast::<f32>());
let py = vld1q_f32((other as *const Self).cast::<f32>());

// y.a - x.a
let mut alphas = vsubq_f32(py, px);
Expand Down Expand Up @@ -324,7 +324,7 @@ impl PalF {
}

#[inline]
fn posterize_channel(color: u8, bits: u8) -> u8 {
const fn posterize_channel(color: u8, bits: u8) -> u8 {
if bits == 0 {
color
} else {
Expand Down
6 changes: 1 addition & 5 deletions src/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,7 @@ impl QuantizationResult {

// true == abort
pub(crate) fn remap_progress(&self, percent: f32) -> bool {
if let Some(cb) = &self.progress_callback {
cb(percent) == ControlFlow::Break
} else {
false
}
self.progress_callback.as_ref().map_or(false, |cb| cb(percent) == ControlFlow::Break)
}

/// Remap image into a palette + indices.
Expand Down
15 changes: 9 additions & 6 deletions src/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) enum PixelsSource<'pixels, 'rows> {
Callback(Box<RowCallback<'rows>>),
}

impl<'pixels, 'rows> PixelsSource<'pixels, 'rows> {
impl<'pixels> PixelsSource<'pixels, '_> {
pub(crate) fn for_pixels(pixels: SeaCow<'pixels, RGBA>, width: u32, height: u32, stride: u32) -> Result<Self, Error> {
if stride < width || height == 0 || width == 0 {
return Err(Error::ValueOutOfRange);
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Clone for DynamicRows<'_, '_> {
}
let pixels = SeaCow::boxed(out.into_boxed_slice());
PixelsSource::for_pixels(pixels, self.width, self.height, self.width).unwrap()
}
},
},
gamma: self.gamma,
}
Expand All @@ -79,7 +79,7 @@ pub(crate) struct DynamicRowsIter<'parent, 'pixels, 'rows> {
temp_f_row: Option<Box<[MaybeUninit<f_pixel>]>>,
}

impl<'a, 'pixels, 'rows> DynamicRowsIter<'a, 'pixels, 'rows> {
impl DynamicRowsIter<'_, '_, '_> {
#[must_use]
pub fn row_f<'px>(&'px mut self, temp_row: &mut [MaybeUninit<RGBA>], row: usize) -> &'px [f_pixel] {
debug_assert_eq!(temp_row.len(), self.px.width as usize);
Expand Down Expand Up @@ -241,19 +241,22 @@ impl<'pixels, 'rows> DynamicRows<'pixels, 'rows> {

pub fn free_histogram_inputs(&mut self) {
if self.f_pixels.is_some() {
self.pixels = PixelsSource::Pixels { rows: SeaCow::borrowed(&[]), pixels: None };
self.pixels = PixelsSource::Pixels {
rows: SeaCow::borrowed(&[]),
pixels: None,
};
}
}

#[inline(always)]
#[must_use]
pub fn width(&self) -> usize {
pub const fn width(&self) -> usize {
self.width as usize
}

#[inline(always)]
#[must_use]
pub fn height(&self) -> usize {
pub const fn height(&self) -> usize {
self.height as usize
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/seacow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T> SeaCow<'static, T> {
impl<'a, T> SeaCow<'a, T> {
#[inline]
#[must_use]
pub fn borrowed(data: &'a [T]) -> Self {
pub const fn borrowed(data: &'a [T]) -> Self {
Self { inner: SeaCowInner::Borrowed(data) }
}

Expand Down Expand Up @@ -86,7 +86,7 @@ enum SeaCowInner<'a, T> {
}

#[cfg(feature = "_internal_c_ffi")]
impl<'a, T> Drop for SeaCowInner<'a, T> {
impl<T> Drop for SeaCowInner<'_, T> {
fn drop(&mut self) {
if let Self::Owned { ptr, free_fn, .. } = self {
unsafe {
Expand All @@ -96,7 +96,7 @@ impl<'a, T> Drop for SeaCowInner<'a, T> {
}
}

impl<'a, T> SeaCow<'a, T> {
impl<T> SeaCow<'_, T> {
#[must_use]
pub fn as_slice(&self) -> &[T] {
match &self.inner {
Expand All @@ -120,17 +120,18 @@ pub(crate) struct RowBitmapMut<'a, T> {
}
unsafe impl<T: Send + Sync> Send for RowBitmapMut<'_, T> {}

impl<'a, T> RowBitmapMut<'a, MaybeUninit<T>> {
impl<T> RowBitmapMut<'_, MaybeUninit<T>> {
#[inline]
pub(crate) unsafe fn assume_init<'maybeowned>(&'maybeowned mut self) -> RowBitmap<'maybeowned, T> {
#[allow(clippy::transmute_ptr_to_ptr)]
RowBitmap {
width: self.width,
rows: std::mem::transmute::<&'maybeowned [PointerMut<MaybeUninit<T>>], &'maybeowned [Pointer<T>]>(self.rows.borrow_mut()),
}
}
}

impl<'a, T> RowBitmap<'a, T> {
impl<T> RowBitmap<'_, T> {
pub fn rows(&self) -> impl Iterator<Item = &[T]> {
let width = self.width;
self.rows.iter().map(move |row| {
Expand All @@ -145,7 +146,7 @@ enum MutCow<'a, T: ?Sized> {
Borrowed(&'a mut T),
}

impl<'a, T: ?Sized> MutCow<'a, T> {
impl<T: ?Sized> MutCow<'_, T> {
#[must_use]
pub fn borrow_mut(&mut self) -> &mut T {
match self {
Expand Down

0 comments on commit 2e810a3

Please sign in to comment.