Skip to content

Commit 2e810a3

Browse files
committed
Clippy/fmt
1 parent 8018d00 commit 2e810a3

File tree

10 files changed

+50
-43
lines changed

10 files changed

+50
-43
lines changed

imagequant-sys/src/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ pub unsafe extern "C" fn liq_result_set_progress_callback(result: &mut liq_resul
189189
result.inner.set_progress_callback(move |f| if callback(f, user_info) == 0 { ControlFlow::Break} else { ControlFlow::Continue});
190190
}
191191

192+
#[allow(clippy::cast_ptr_alignment)]
192193
unsafe fn attr_to_liq_attr_ptr(ptr: &Attributes) -> &liq_attr {
193194
let liq_attr = std::ptr::NonNull::<liq_attr>::dangling();
194195
let outer_addr = std::ptr::addr_of!(*liq_attr.as_ptr()) as isize;

src/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ impl Attributes {
258258

259259
#[inline(always)]
260260
pub(crate) fn verbose_print(&self, msg: impl AsRef<str>) {
261-
fn _print(a: &Attributes, msg: &str) {
261+
fn print_(a: &Attributes, msg: &str) {
262262
if let Some(f) = &a.log_callback {
263263
f(a, msg);
264264
}
265265
}
266-
_print(self, msg.as_ref());
266+
print_(self, msg.as_ref());
267267
}
268268

269269
#[inline]

src/hist.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl Histogram {
227227
}
228228

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

415415
/// ignores the index
416416
#[derive(PartialEq, Debug)]
417-
pub(crate) struct HashColor { pub rgba: RGBA, pub index: PalIndex }
417+
pub(crate) struct HashColor {
418+
pub rgba: RGBA,
419+
pub index: PalIndex,
420+
}
418421

419422
#[allow(clippy::derived_hash_with_manual_eq)]
420423
impl Hash for HashColor {

src/image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ impl<'pixels> Image<'pixels> {
217217
/// Width of the image in pixels
218218
#[must_use]
219219
#[inline(always)]
220-
pub fn width(&self) -> usize {
220+
pub const fn width(&self) -> usize {
221221
self.px.width as _
222222
}
223223

224224
/// Height of the image in pixels
225225
#[must_use]
226226
#[inline(always)]
227-
pub fn height(&self) -> usize {
227+
pub const fn height(&self) -> usize {
228228
self.px.height as _
229229
}
230230

@@ -331,7 +331,7 @@ impl<'pixels> Image<'pixels> {
331331
let pixels_rows = match PixelsSource::for_pixels(pixels, width, height, stride) {
332332
Ok(p) => p,
333333
Err(e) => {
334-
attr.verbose_print(format!("Buffer length is {} bytes, which is not enough for {}×{}×4 RGBA bytes", pixels_len*4, stride, height));
334+
attr.verbose_print(format!("Buffer length is {} bytes, which is not enough for {}×{}×4 RGBA bytes", pixels_len * 4, stride, height));
335335
return Err(e);
336336
},
337337
};

src/kmeans.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ impl Kmeans {
6666

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

7678
let diff = tls.into_iter()
7779
.map(|c| c.0.into_inner())
7880
.reduce(Self::try_merge)
7981
.transpose()?
80-
.map_or(0., |kmeans| {
81-
kmeans.finalize(palette) / total
82-
});
82+
.map_or(0., |kmeans| kmeans.finalize(palette) / total);
8383

8484
replace_unused_colors(palette, hist)?;
8585
Ok(diff)

src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ fn sizes() {
243243

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

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

295294
impl Ord for OrdFloat<f32> {
296295
#[inline]
297-
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal) }
296+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
297+
self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal)
298+
}
298299
}
299300

300301
impl Eq for OrdFloat<f64> {
301302
}
302303

303304
impl Ord for OrdFloat<f64> {
304305
#[inline]
305-
fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal) }
306+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
307+
self.0.partial_cmp(&other.0).unwrap_or(std::cmp::Ordering::Equal)
308+
}
306309
}
307310

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

337340
for (i, c) in (200..255).enumerate() {
338-
assert_eq!(pal[i], RGBA::new(c,c,c,255));
341+
assert_eq!(pal[i], RGBA::new(c, c, c, 255));
339342
}
340343

341344
for c in 0..128 {
342-
assert!(pal[55..].iter().any(|&p| p == RGBA::new(c,c,c,255)));
345+
assert!(pal[55..].iter().any(|&p| p == RGBA::new(c, c, c, 255)));
343346
}
344347
}

src/pal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ impl f_pixel {
5252

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

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

6262
// y.a - x.a
6363
let mut alphas = vsubq_f32(py, px);
@@ -324,7 +324,7 @@ impl PalF {
324324
}
325325

326326
#[inline]
327-
fn posterize_channel(color: u8, bits: u8) -> u8 {
327+
const fn posterize_channel(color: u8, bits: u8) -> u8 {
328328
if bits == 0 {
329329
color
330330
} else {

src/quant.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,7 @@ impl QuantizationResult {
223223

224224
// true == abort
225225
pub(crate) fn remap_progress(&self, percent: f32) -> bool {
226-
if let Some(cb) = &self.progress_callback {
227-
cb(percent) == ControlFlow::Break
228-
} else {
229-
false
230-
}
226+
self.progress_callback.as_ref().map_or(false, |cb| cb(percent) == ControlFlow::Break)
231227
}
232228

233229
/// Remap image into a palette + indices.

src/rows.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) enum PixelsSource<'pixels, 'rows> {
1616
Callback(Box<RowCallback<'rows>>),
1717
}
1818

19-
impl<'pixels, 'rows> PixelsSource<'pixels, 'rows> {
19+
impl<'pixels> PixelsSource<'pixels, '_> {
2020
pub(crate) fn for_pixels(pixels: SeaCow<'pixels, RGBA>, width: u32, height: u32, stride: u32) -> Result<Self, Error> {
2121
if stride < width || height == 0 || width == 0 {
2222
return Err(Error::ValueOutOfRange);
@@ -67,7 +67,7 @@ impl Clone for DynamicRows<'_, '_> {
6767
}
6868
let pixels = SeaCow::boxed(out.into_boxed_slice());
6969
PixelsSource::for_pixels(pixels, self.width, self.height, self.width).unwrap()
70-
}
70+
},
7171
},
7272
gamma: self.gamma,
7373
}
@@ -79,7 +79,7 @@ pub(crate) struct DynamicRowsIter<'parent, 'pixels, 'rows> {
7979
temp_f_row: Option<Box<[MaybeUninit<f_pixel>]>>,
8080
}
8181

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

242242
pub fn free_histogram_inputs(&mut self) {
243243
if self.f_pixels.is_some() {
244-
self.pixels = PixelsSource::Pixels { rows: SeaCow::borrowed(&[]), pixels: None };
244+
self.pixels = PixelsSource::Pixels {
245+
rows: SeaCow::borrowed(&[]),
246+
pixels: None,
247+
};
245248
}
246249
}
247250

248251
#[inline(always)]
249252
#[must_use]
250-
pub fn width(&self) -> usize {
253+
pub const fn width(&self) -> usize {
251254
self.width as usize
252255
}
253256

254257
#[inline(always)]
255258
#[must_use]
256-
pub fn height(&self) -> usize {
259+
pub const fn height(&self) -> usize {
257260
self.height as usize
258261
}
259262
}

src/seacow.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<T> SeaCow<'static, T> {
3636
impl<'a, T> SeaCow<'a, T> {
3737
#[inline]
3838
#[must_use]
39-
pub fn borrowed(data: &'a [T]) -> Self {
39+
pub const fn borrowed(data: &'a [T]) -> Self {
4040
Self { inner: SeaCowInner::Borrowed(data) }
4141
}
4242

@@ -86,7 +86,7 @@ enum SeaCowInner<'a, T> {
8686
}
8787

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

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

123-
impl<'a, T> RowBitmapMut<'a, MaybeUninit<T>> {
123+
impl<T> RowBitmapMut<'_, MaybeUninit<T>> {
124124
#[inline]
125125
pub(crate) unsafe fn assume_init<'maybeowned>(&'maybeowned mut self) -> RowBitmap<'maybeowned, T> {
126+
#[allow(clippy::transmute_ptr_to_ptr)]
126127
RowBitmap {
127128
width: self.width,
128129
rows: std::mem::transmute::<&'maybeowned [PointerMut<MaybeUninit<T>>], &'maybeowned [Pointer<T>]>(self.rows.borrow_mut()),
129130
}
130131
}
131132
}
132133

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

148-
impl<'a, T: ?Sized> MutCow<'a, T> {
149+
impl<T: ?Sized> MutCow<'_, T> {
149150
#[must_use]
150151
pub fn borrow_mut(&mut self) -> &mut T {
151152
match self {

0 commit comments

Comments
 (0)