Fix a few clippy lints

This commit is contained in:
Ivan Smirnov 2021-12-02 15:55:56 +00:00
parent 784f7952de
commit 0825c0aa3a
5 changed files with 44 additions and 38 deletions

View file

@ -57,7 +57,7 @@ where
let mut px = Pixel::new().with_a(0xff); let mut px = Pixel::new().with_a(0xff);
let mut run = 0_u16; let mut run = 0_u16;
for px_out in pixels.iter_mut() { for px_out in &mut pixels {
if run != 0 { if run != 0 {
run -= 1; run -= 1;
*px_out = px; *px_out = px;
@ -149,27 +149,27 @@ where
// Safety: this is safe because we have previously set all the lengths ourselves // Safety: this is safe because we have previously set all the lengths ourselves
let ptr = pixels.as_mut_ptr(); let ptr = pixels.as_mut_ptr();
mem::forget(pixels); mem::forget(pixels);
Vec::from_raw_parts(ptr as *mut _, n_pixels * N, n_pixels * N) Vec::from_raw_parts(ptr.cast(), n_pixels * N, n_pixels * N)
}; };
Ok(bytes) Ok(bytes)
} }
pub trait MaybeChannels { pub trait MaybeChannels {
fn maybe_channels(&self) -> Option<u8>; fn maybe_channels(self) -> Option<u8>;
} }
impl MaybeChannels for u8 { impl MaybeChannels for u8 {
#[inline] #[inline]
fn maybe_channels(&self) -> Option<u8> { fn maybe_channels(self) -> Option<u8> {
Some(*self) Some(self)
} }
} }
impl MaybeChannels for Option<u8> { impl MaybeChannels for Option<u8> {
#[inline] #[inline]
fn maybe_channels(&self) -> Option<u8> { fn maybe_channels(self) -> Option<u8> {
*self self
} }
} }

View file

@ -16,7 +16,7 @@ struct WriteBuf {
} }
impl WriteBuf { impl WriteBuf {
pub unsafe fn new(ptr: *mut u8) -> Self { pub const unsafe fn new(ptr: *mut u8) -> Self {
Self { start: ptr, current: ptr } Self { start: ptr, current: ptr }
} }
@ -50,10 +50,10 @@ impl WriteBuf {
fn encode_diff_canonical<const N: usize>( fn encode_diff_canonical<const N: usize>(
px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf, px: Pixel<N>, px_prev: Pixel<N>, buf: &mut WriteBuf,
) -> Option<(bool, bool, bool, bool)> { ) -> Option<(bool, bool, bool, bool)> {
let vr = (px.r() as i16) - (px_prev.r() as i16); let vr = i16::from(px.r()) - i16::from(px_prev.r());
let vg = (px.g() as i16) - (px_prev.g() as i16); let vg = i16::from(px.g()) - i16::from(px_prev.g());
let vb = (px.b() as i16) - (px_prev.b() as i16); let vb = i16::from(px.b()) - i16::from(px_prev.b());
let va = (px.a_or(0) as i16) - (px_prev.a_or(0) as i16); let va = i16::from(px.a_or(0)) - i16::from(px_prev.a_or(0));
let (vr_16, vg_16, vb_16, va_16) = (vr + 16, vg + 16, vb + 16, va + 16); let (vr_16, vg_16, vb_16, va_16) = (vr + 16, vg + 16, vb + 16, va + 16);
if vr_16 | vg_16 | vb_16 | va_16 | 31 == 31 { if vr_16 | vg_16 | vb_16 | va_16 | 31 == 31 {
@ -123,7 +123,7 @@ fn encode_diff_wrapping<const N: usize>(
} }
} }
pub(crate) fn qoi_encode_impl<const CHANNELS: usize, const CANONICAL: bool>( fn qoi_encode_impl<const CHANNELS: usize, const CANONICAL: bool>(
out: &mut [u8], data: &[u8], width: u32, height: u32, colorspace: ColorSpace, out: &mut [u8], data: &[u8], width: u32, height: u32, colorspace: ColorSpace,
) -> Result<usize> ) -> Result<usize>
where where
@ -143,7 +143,7 @@ where
let pixels = unsafe { let pixels = unsafe {
// Safety: we've verified that n_pixels * N == data.len() // Safety: we've verified that n_pixels * N == data.len()
slice::from_raw_parts::<Pixel<CHANNELS>>(data.as_ptr() as _, n_pixels) slice::from_raw_parts::<Pixel<CHANNELS>>(data.as_ptr().cast(), n_pixels)
}; };
let mut buf = unsafe { let mut buf = unsafe {
@ -151,11 +151,8 @@ where
WriteBuf::new(out.as_mut_ptr()) WriteBuf::new(out.as_mut_ptr())
}; };
let mut header = Header::default(); let header =
header.width = width; Header { width, height, channels: CHANNELS as u8, colorspace, ..Header::default() };
header.height = height;
header.channels = CHANNELS as u8;
header.colorspace = colorspace;
buf.write(header.to_bytes()); buf.write(header.to_bytes());
let mut index = [Pixel::new(); 64]; let mut index = [Pixel::new(); 64];
@ -226,7 +223,7 @@ where
} }
#[inline] #[inline]
pub(crate) fn encode_to_buf_impl<const CANONICAL: bool>( pub fn encode_to_buf_impl<const CANONICAL: bool>(
out: &mut [u8], data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace, out: &mut [u8], data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace,
) -> Result<usize> { ) -> Result<usize> {
match channels { match channels {
@ -237,7 +234,7 @@ pub(crate) fn encode_to_buf_impl<const CANONICAL: bool>(
} }
#[inline] #[inline]
pub(crate) fn encode_to_vec_impl<const CANONICAL: bool>( pub fn encode_to_vec_impl<const CANONICAL: bool>(
data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace, data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let mut out = Vec::with_capacity(encode_size_required(width, height, channels)); let mut out = Vec::with_capacity(encode_size_required(width, height, channels));
@ -254,10 +251,7 @@ pub(crate) fn encode_to_vec_impl<const CANONICAL: bool>(
pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize { pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize {
let (width, height) = (width as usize, height as usize); let (width, height) = (width as usize, height as usize);
let n_pixels = width.saturating_mul(height); let n_pixels = width.saturating_mul(height);
return QOI_HEADER_SIZE QOI_HEADER_SIZE + n_pixels.saturating_mul(usize::from(channels)) + n_pixels + QOI_PADDING
+ n_pixels.saturating_mul(usize::from(channels))
+ n_pixels
+ QOI_PADDING;
} }
#[inline] #[inline]

View file

@ -28,10 +28,10 @@ impl Default for Header {
#[inline(always)] #[inline(always)]
const fn u32_to_be(v: u32) -> [u8; 4] { const fn u32_to_be(v: u32) -> [u8; 4] {
[ [
((0xff000000 & v) >> 24) as u8, ((0xff00_0000 & v) >> 24) as u8,
((0xff0000 & v) >> 16) as u8, ((0x00ff_0000 & v) >> 16) as u8,
((0xff00 & v) >> 8) as u8, ((0xff00 & v) >> 8) as u8,
(0xff & v) as u8, (0x00ff & v) as u8,
] ]
} }
@ -56,13 +56,13 @@ impl Header {
#[inline] #[inline]
pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self { pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self {
let mut out = Self::default(); Self {
out.magic = u32_from_be(&v[..4]); magic: u32_from_be(&v[..4]),
out.width = u32_from_be(&v[4..8]); width: u32_from_be(&v[4..8]),
out.height = u32_from_be(&v[8..12]); height: u32_from_be(&v[8..12]),
out.channels = v[12]; channels: v[12],
out.colorspace = v[13].into(); colorspace: v[13].into(),
out }
} }
#[inline] #[inline]
@ -71,7 +71,7 @@ impl Header {
} }
#[inline] #[inline]
pub fn validate(&self) -> Result<()> { pub const fn validate(&self) -> Result<()> {
if unlikely(self.magic != QOI_MAGIC) { if unlikely(self.magic != QOI_MAGIC) {
return Err(Error::InvalidMagic { magic: self.magic }); return Err(Error::InvalidMagic { magic: self.magic });
} else if unlikely(self.height == 0 || self.width == 0) { } else if unlikely(self.height == 0 || self.width == 0) {

View file

@ -1,3 +1,15 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(
clippy::inline_always,
clippy::struct_excessive_bools,
clippy::fn_params_excessive_bools,
clippy::similar_names,
clippy::missing_errors_doc,
clippy::must_use_candidate,
clippy::never_loop,
clippy::module_name_repetitions
)]
mod colorspace; mod colorspace;
mod decode; mod decode;
mod encode; mod encode;

View file

@ -3,7 +3,7 @@
pub const fn likely(b: bool) -> bool { pub const fn likely(b: bool) -> bool {
// borrowed from `likely_stable` crate // borrowed from `likely_stable` crate
#[allow(clippy::needless_bool)] #[allow(clippy::needless_bool)]
if (1i32).checked_div(if b { 1 } else { 0 }).is_some() { if 1_i32.checked_div(if b { 1 } else { 0 }).is_some() {
true true
} else { } else {
false false
@ -15,7 +15,7 @@ pub const fn likely(b: bool) -> bool {
pub const fn unlikely(b: bool) -> bool { pub const fn unlikely(b: bool) -> bool {
// borrowed from `likely_stable` crate // borrowed from `likely_stable` crate
#[allow(clippy::needless_bool)] #[allow(clippy::needless_bool)]
if (1i32).checked_div(if b { 0 } else { 1 }).is_none() { if 1_i32.checked_div(if b { 0 } else { 1 }).is_none() {
true true
} else { } else {
false false