From 088ab4f0cb6bbd97b743122702d320bcec8732de Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Tue, 26 Dec 2023 15:22:00 -0500 Subject: [PATCH] Remove num_traits dependency (#30) --- Cargo.lock.msrv | 10 ---------- Cargo.toml | 1 - src/loop_filter.rs | 2 +- src/lossless.rs | 23 +++++++++++------------ src/vp8.rs | 9 ++++----- 5 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Cargo.lock.msrv b/Cargo.lock.msrv index 91cfa9e..0e5ee11 100644 --- a/Cargo.lock.msrv +++ b/Cargo.lock.msrv @@ -70,15 +70,6 @@ dependencies = [ "simd-adler32", ] -[[package]] -name = "num-traits" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" -dependencies = [ - "autocfg", -] - [[package]] name = "paste" version = "1.0.14" @@ -164,7 +155,6 @@ name = "webp" version = "0.1.0" dependencies = [ "byteorder", - "num-traits", "paste", "png", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 36280a5..77cd254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ publish = false [dependencies] byteorder = "1.4.3" -num-traits = "0.2.16" thiserror = "1.0.47" [dev-dependencies] diff --git a/src/loop_filter.rs b/src/loop_filter.rs index 9412cd5..fe08504 100644 --- a/src/loop_filter.rs +++ b/src/loop_filter.rs @@ -2,7 +2,7 @@ #[inline] fn c(val: i32) -> i32 { - num_traits::clamp(val, -128, 127) + val.max(-128).min(127) } //unsigned to signed diff --git a/src/lossless.rs b/src/lossless.rs index 12007fc..99e1a9a 100644 --- a/src/lossless.rs +++ b/src/lossless.rs @@ -3,11 +3,7 @@ //! [Lossless spec](https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification) //! -use std::{ - convert::TryInto, - io::Read, - ops::{AddAssign, Shl}, -}; +use std::{convert::TryInto, io::Read, mem}; use crate::decoder::DecodingError; @@ -644,18 +640,18 @@ impl BitReader { self.buf = buf; } - pub(crate) fn read_bits(&mut self, num: u8) -> Result - where - T: num_traits::Unsigned + Shl + AddAssign + From, - { - let mut value: T = T::zero(); + pub(crate) fn read_bits>(&mut self, num: u8) -> Result { + debug_assert!(num as usize <= 8 * mem::size_of::()); + debug_assert!(num <= 16); + + let mut value = 0; for i in 0..num { if self.buf.len() <= self.index { return Err(DecodingError::BitStreamError); } let bit_true = self.buf[self.index] & (1 << self.bit_count) != 0; - value += T::from(bit_true) << i; + value += u16::from(bit_true) << i; self.bit_count = if self.bit_count == 7 { self.index += 1; 0 @@ -664,7 +660,10 @@ impl BitReader { }; } - Ok(value) + match value.try_into() { + Ok(value) => Ok(value), + Err(_) => unreachable!("Value too large to fit in type"), + } } } diff --git a/src/vp8.rs b/src/vp8.rs index 0d1c93b..fba5e63 100644 --- a/src/vp8.rs +++ b/src/vp8.rs @@ -13,7 +13,6 @@ //! use byteorder::{LittleEndian, ReadBytesExt}; -use num_traits::clamp; use std::cmp; use std::convert::TryInto; use std::default::Default; @@ -1147,11 +1146,11 @@ impl Vp8Decoder { fn read_quantization_indices(&mut self) { fn dc_quant(index: i32) -> i16 { - DC_QUANT[clamp(index, 0, 127) as usize] + DC_QUANT[index.max(0).min(127) as usize] } fn ac_quant(index: i32) -> i16 { - AC_QUANT[clamp(index, 0, 127) as usize] + AC_QUANT[index.max(0).min(127) as usize] } let yac_abs = self.b.read_literal(7); @@ -2064,13 +2063,13 @@ impl Vp8Decoder { } } - filter_level = clamp(filter_level, 0, 63); + filter_level = filter_level.max(0).min(63); if macroblock.luma_mode == LumaMode::B { filter_level += self.mode_delta[0]; } - let filter_level = clamp(filter_level, 0, 63) as u8; + let filter_level = filter_level.max(0).min(63) as u8; //interior limit let mut interior_limit = filter_level;