Skip to content

Commit 6508c1d

Browse files
committed
Clippy
1 parent 029f43c commit 6508c1d

11 files changed

+81
-81
lines changed

src/blocksplitter.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn find_largest_splittable_block(
8989

9090
let mut last = 0;
9191

92-
for &item in splitpoints.iter() {
92+
for &item in splitpoints {
9393
if done[last] == 0 && item - last > longest {
9494
found = Some((last, item));
9595
longest = item - last;
@@ -136,12 +136,12 @@ fn print_block_split_points(lz77: &Lz77Store, lz77splitpoints: &[usize]) {
136136
"block split points: {} (hex: {})",
137137
splitpoints
138138
.iter()
139-
.map(|&sp| format!("{}", sp))
139+
.map(|&sp| format!("{sp}"))
140140
.collect::<Vec<_>>()
141141
.join(" "),
142142
splitpoints
143143
.iter()
144-
.map(|&sp| format!("{:x}", sp))
144+
.map(|&sp| format!("{sp:x}"))
145145
.collect::<Vec<_>>()
146146
.join(" ")
147147
);
@@ -160,7 +160,7 @@ pub fn blocksplit_lz77(lz77: &Lz77Store, maxblocks: u16, splitpoints: &mut Vec<u
160160
let mut lstart = 0;
161161
let mut lend = lz77.size();
162162

163-
while maxblocks != 0 && numblocks < maxblocks as u32 {
163+
while maxblocks != 0 && numblocks < u32::from(maxblocks) {
164164
debug_assert!(lstart < lend);
165165
let find_minimum_result = find_minimum(
166166
|i| estimate_cost(lz77, lstart, i) + estimate_cost(lz77, i, lend),
@@ -179,7 +179,7 @@ pub fn blocksplit_lz77(lz77: &Lz77Store, maxblocks: u16, splitpoints: &mut Vec<u
179179
done[lstart] = 1;
180180
} else {
181181
splitpoints.push(llpos);
182-
splitpoints.sort();
182+
splitpoints.sort_unstable();
183183
numblocks += 1;
184184
}
185185

src/cache.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub struct ZopfliLongestMatchCache {
1919
}
2020

2121
impl ZopfliLongestMatchCache {
22-
pub fn new(blocksize: usize) -> ZopfliLongestMatchCache {
23-
ZopfliLongestMatchCache {
22+
pub fn new(blocksize: usize) -> Self {
23+
Self {
2424
/* length > 0 and dist 0 is invalid combination, which indicates on purpose
2525
that this cache value is not filled in yet. */
2626
length: vec![1; blocksize],
@@ -52,7 +52,7 @@ impl ZopfliLongestMatchCache {
5252
if self.sublen[start + 1] == 0 && self.sublen[start + 2] == 0 {
5353
return 0; // No sublen cached.
5454
}
55-
self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] as u32 + 3
55+
u32::from(self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)]) + 3
5656
}
5757

5858
/// Stores sublen array in the cache.
@@ -100,8 +100,8 @@ impl ZopfliLongestMatchCache {
100100

101101
for j in 0..ZOPFLI_CACHE_LENGTH {
102102
let length = self.sublen[start + (j * 3)] as usize + 3;
103-
let dist = self.sublen[start + (j * 3 + 1)] as u16
104-
+ 256 * self.sublen[start + (j * 3 + 2)] as u16;
103+
let dist = u16::from(self.sublen[start + (j * 3 + 1)])
104+
+ 256 * u16::from(self.sublen[start + (j * 3 + 2)]);
105105

106106
let mut i = prevlength;
107107
while i <= length {
@@ -178,7 +178,7 @@ impl Cache for ZopfliLongestMatchCache {
178178
|| (sublen.is_some() && max_sublen >= limit as u32);
179179

180180
if limit_ok_for_cache && cache_available {
181-
if sublen.is_none() || length_lmcpos as u32 <= max_sublen {
181+
if sublen.is_none() || u32::from(length_lmcpos) <= max_sublen {
182182
let length = cmp::min(length_lmcpos, limit as u16);
183183
let distance;
184184
if let Some(ref mut subl) = *sublen {

src/deflate.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<W: Write> DeflateEncoder<W> {
4646
/// Creates a new Zopfli DEFLATE encoder that will operate according to the
4747
/// specified options.
4848
pub fn new(options: Options, btype: BlockType, sink: W) -> Self {
49-
DeflateEncoder {
49+
Self {
5050
options,
5151
btype,
5252
have_chunk: false,
@@ -399,7 +399,7 @@ fn calculate_block_symbol_size_small(
399399
match item {
400400
LitLen::Literal(litlens_i) => {
401401
debug_assert!(litlens_i < 259);
402-
result += ll_lengths[litlens_i as usize]
402+
result += ll_lengths[litlens_i as usize];
403403
}
404404
LitLen::LengthDist(litlens_i, dists_i) => {
405405
debug_assert!(litlens_i < 259);
@@ -829,7 +829,7 @@ fn add_lz77_block<W: Write>(
829829
return add_non_compressed_block(final_block, in_data, pos, end, bitwise_writer);
830830
}
831831

832-
bitwise_writer.add_bit(final_block as u8)?;
832+
bitwise_writer.add_bit(u8::from(final_block))?;
833833

834834
let (ll_lengths, d_lengths) = match btype {
835835
BlockType::Uncompressed => unreachable!(),
@@ -1034,7 +1034,7 @@ fn add_lz77_data<W: Write>(
10341034
)?;
10351035
bitwise_writer.add_huffman_bits(d_symbols[ds], d_lengths[ds])?;
10361036
bitwise_writer.add_bits(
1037-
get_dist_extra_bits_value(dist) as u32,
1037+
u32::from(get_dist_extra_bits_value(dist)),
10381038
get_dist_extra_bits(dist) as u32,
10391039
)?;
10401040
testlength += litlen;
@@ -1067,7 +1067,7 @@ fn add_lz77_block_auto_type<W: Write>(
10671067
let mut fixedstore = Lz77Store::new();
10681068
if lstart == lend {
10691069
/* Smallest empty block is represented by fixed block */
1070-
bitwise_writer.add_bits(final_block as u32, 1)?;
1070+
bitwise_writer.add_bits(u32::from(final_block), 1)?;
10711071
bitwise_writer.add_bits(1, 2)?; /* btype 01 */
10721072
bitwise_writer.add_bits(0, 7)?; /* end symbol has code 0000000 */
10731073
return Ok(());
@@ -1158,7 +1158,7 @@ fn add_all_blocks<W: Write>(
11581158
bitwise_writer: &mut BitwiseWriter<W>,
11591159
) -> Result<(), Error> {
11601160
let mut last = 0;
1161-
for &item in splitpoints.iter() {
1161+
for &item in splitpoints {
11621162
add_lz77_block_auto_type(false, in_data, lz77, last, item, 0, bitwise_writer)?;
11631163
last = item;
11641164
}
@@ -1285,7 +1285,7 @@ fn add_non_compressed_block<W: Write>(
12851285
let blocksize = chunk.len();
12861286
let nlen = !blocksize;
12871287

1288-
bitwise_writer.add_bit((final_block && is_final) as u8)?;
1288+
bitwise_writer.add_bit(u8::from(final_block && is_final))?;
12891289
/* BTYPE 00 */
12901290
bitwise_writer.add_bit(0)?;
12911291
bitwise_writer.add_bit(0)?;
@@ -1311,16 +1311,16 @@ struct BitwiseWriter<W> {
13111311
}
13121312

13131313
impl<W: Write> BitwiseWriter<W> {
1314-
fn new(out: W) -> BitwiseWriter<W> {
1315-
BitwiseWriter {
1314+
const fn new(out: W) -> Self {
1315+
Self {
13161316
bit: 0,
13171317
bp: 0,
13181318
len: 0,
13191319
out,
13201320
}
13211321
}
13221322

1323-
fn bytes_written(&self) -> usize {
1323+
const fn bytes_written(&self) -> usize {
13241324
self.len + if self.bp > 0 { 1 } else { 0 }
13251325
}
13261326

@@ -1399,7 +1399,7 @@ mod test {
13991399

14001400
set_counts_to_count(&mut counts, count, i, stride);
14011401

1402-
assert_eq!(counts, vec![0, 1, 2, 100, 100, 100, 100, 100, 8, 9])
1402+
assert_eq!(counts, vec![0, 1, 2, 100, 100, 100, 100, 100, 8, 9]);
14031403
}
14041404

14051405
#[test]

src/hash.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ pub struct ZopfliHash {
6464
}
6565

6666
impl ZopfliHash {
67-
pub fn new() -> Box<ZopfliHash> {
67+
pub fn new() -> Box<Self> {
6868
const LAYOUT: Layout = Layout::new::<ZopfliHash>();
6969

70-
let ptr = NonNull::new(unsafe { alloc(LAYOUT) } as *mut ZopfliHash)
70+
let ptr = NonNull::new(unsafe { alloc(LAYOUT) }.cast::<Self>())
7171
.unwrap_or_else(|| handle_alloc_error(LAYOUT));
7272

7373
unsafe {
@@ -133,11 +133,11 @@ impl ZopfliHash {
133133
/// must be made on consecutive input characters. Since the hash value exists out
134134
/// of multiple input bytes, a few warmups with this function are needed initially.
135135
fn update_val(&mut self, c: u8) {
136-
self.hash1.val = ((self.hash1.val << HASH_SHIFT) ^ c as u16) & HASH_MASK;
136+
self.hash1.val = ((self.hash1.val << HASH_SHIFT) ^ u16::from(c)) & HASH_MASK;
137137
}
138138

139139
pub fn update(&mut self, array: &[u8], pos: usize) {
140-
let hash_value = array.get(pos + ZOPFLI_MIN_MATCH - 1).cloned().unwrap_or(0);
140+
let hash_value = array.get(pos + ZOPFLI_MIN_MATCH - 1).copied().unwrap_or(0);
141141
self.update_val(hash_value);
142142

143143
let hpos = pos & ZOPFLI_WINDOW_MASK;
@@ -171,7 +171,7 @@ impl ZopfliHash {
171171
Which::Hash1 => self.hash1.prev_and_hashval[index].hashval,
172172
Which::Hash2 => self.hash2.prev_and_hashval[index].hashval,
173173
};
174-
hashval.map_or(-1, |hv| hv as i32)
174+
hashval.map_or(-1, i32::from)
175175
}
176176

177177
pub fn val(&self, which: Which) -> u16 {

src/katajainen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn length_limited_code_lengths(frequencies: &[usize], max_bits: usize) -> Ve
131131
thing.extract_bit_lengths(max_bits, num_freqs)
132132
}
133133

134-
impl<'a> Thing<'a> {
134+
impl Thing<'_> {
135135
fn boundary_pm(&mut self, index: usize) {
136136
let num_symbols = self.leaves.len();
137137

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ pub struct Options {
114114
}
115115

116116
impl Default for Options {
117-
fn default() -> Options {
118-
Options {
117+
fn default() -> Self {
118+
Self {
119119
iteration_count: NonZeroU64::new(15).unwrap(),
120120
iterations_without_improvement: NonZeroU64::new(u64::MAX).unwrap(),
121121
maximum_block_splits: 15,

src/lz77.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub enum LitLen {
1818
}
1919

2020
impl LitLen {
21-
pub fn size(&self) -> usize {
21+
pub const fn size(&self) -> usize {
2222
match *self {
23-
LitLen::Literal(_) => 1,
24-
LitLen::LengthDist(len, _) => len as usize,
23+
Self::Literal(_) => 1,
24+
Self::LengthDist(len, _) => len as usize,
2525
}
2626
}
2727
}
@@ -45,8 +45,8 @@ pub struct Lz77Store {
4545
}
4646

4747
impl Lz77Store {
48-
pub fn new() -> Lz77Store {
49-
Lz77Store {
48+
pub const fn new() -> Self {
49+
Self {
5050
litlens: vec![],
5151

5252
pos: vec![],
@@ -131,7 +131,7 @@ impl Lz77Store {
131131

132132
/// Does LZ77 using an algorithm similar to gzip, with lazy matching, rather than
133133
/// with the slow but better "squeeze" implementation.
134-
/// The result is placed in the Lz77Store.
134+
/// The result is placed in the `Lz77Store`.
135135
/// If instart is larger than 0, it uses values before instart as starting
136136
/// dictionary.
137137
pub fn greedy<C: Cache>(&mut self, lmc: &mut C, in_data: &[u8], instart: usize, inend: usize) {
@@ -165,20 +165,20 @@ impl Lz77Store {
165165
find_longest_match(lmc, &h, arr, i, inend, instart, ZOPFLI_MAX_MATCH, &mut None);
166166
dist = longest_match.distance;
167167
leng = longest_match.length;
168-
lengthscore = get_length_score(leng as i32, dist as i32);
168+
lengthscore = get_length_score(i32::from(leng), i32::from(dist));
169169

170170
/* Lazy matching. */
171171
prevlengthscore = get_length_score(prev_length as i32, prev_match as i32);
172172
if match_available {
173173
match_available = false;
174174
if lengthscore > prevlengthscore + 1 {
175-
self.lit_len_dist(arr[i - 1] as u16, 0, i - 1);
175+
self.lit_len_dist(u16::from(arr[i - 1]), 0, i - 1);
176176
if (lengthscore as usize) >= ZOPFLI_MIN_MATCH
177177
&& (leng as usize) < ZOPFLI_MAX_MATCH
178178
{
179179
match_available = true;
180-
prev_length = leng as u32;
181-
prev_match = dist as u32;
180+
prev_length = u32::from(leng);
181+
prev_match = u32::from(dist);
182182
i += 1;
183183
continue;
184184
}
@@ -201,8 +201,8 @@ impl Lz77Store {
201201
&& (leng as usize) < ZOPFLI_MAX_MATCH
202202
{
203203
match_available = true;
204-
prev_length = leng as u32;
205-
prev_match = dist as u32;
204+
prev_length = u32::from(leng);
205+
prev_match = u32::from(dist);
206206
i += 1;
207207
continue;
208208
}
@@ -214,7 +214,7 @@ impl Lz77Store {
214214
self.lit_len_dist(leng, dist, i);
215215
} else {
216216
leng = 1;
217-
self.lit_len_dist(arr[i] as u16, 0, i);
217+
self.lit_len_dist(u16::from(arr[i]), 0, i);
218218
}
219219
for _ in 1..leng {
220220
debug_assert!(i < inend);
@@ -276,7 +276,7 @@ impl Lz77Store {
276276
self.lit_len_dist(length, dist, pos);
277277
} else {
278278
length = 1;
279-
self.lit_len_dist(arr[pos] as u16, 0, pos);
279+
self.lit_len_dist(u16::from(arr[pos]), 0, pos);
280280
}
281281

282282
debug_assert!(pos + (length as usize) <= inend);
@@ -385,8 +385,8 @@ pub struct LongestMatch {
385385
}
386386

387387
impl LongestMatch {
388-
pub fn new(limit: usize) -> Self {
389-
LongestMatch {
388+
pub const fn new(limit: usize) -> Self {
389+
Self {
390390
distance: 0,
391391
length: 0,
392392
from_cache: false,
@@ -401,7 +401,7 @@ impl LongestMatch {
401401
/// `scan` is the position to compare; `match` is the earlier position to compare.
402402
/// `end` is the last possible byte, beyond which to stop looking.
403403
/// `safe_end` is a few (8) bytes before end, for comparing multiple bytes at once.
404-
fn get_match(array: &[u8], scan_offset: usize, match_offset: usize, end: usize) -> usize {
404+
const fn get_match(array: &[u8], scan_offset: usize, match_offset: usize, end: usize) -> usize {
405405
let mut scan_offset = scan_offset;
406406
let mut match_offset = match_offset;
407407
// /* 8 checks at once per array bounds check (usize is 64-bit). */
@@ -505,7 +505,7 @@ fn find_longest_match_loop(
505505

506506
debug_assert!(p < ZOPFLI_WINDOW_SIZE);
507507
debug_assert_eq!(p, h.prev_at(pp, which_hash));
508-
debug_assert_eq!(h.hash_val_at(p, which_hash), h.val(which_hash) as i32);
508+
debug_assert_eq!(h.hash_val_at(p, which_hash), i32::from(h.val(which_hash)));
509509

510510
if dist > 0 {
511511
debug_assert!(pos < size);
@@ -545,7 +545,7 @@ fn find_longest_match_loop(
545545
/* Switch to the other hash once this will be more efficient. */
546546
if which_hash == Which::Hash1
547547
&& bestlength >= h.same[hpos] as usize
548-
&& h.val(Which::Hash2) as i32 == h.hash_val_at(p, Which::Hash2)
548+
&& i32::from(h.val(Which::Hash2)) == h.hash_val_at(p, Which::Hash2)
549549
{
550550
/* Now use the hash that encodes the length and first byte. */
551551
which_hash = Which::Hash2;
@@ -587,7 +587,7 @@ fn find_longest_match_loop(
587587
/// rather unpredictable way
588588
/// -the first zopfli run, so it affects the chance of the first run being closer
589589
/// to the optimal output
590-
fn get_length_score(length: i32, distance: i32) -> i32 {
590+
const fn get_length_score(length: i32, distance: i32) -> i32 {
591591
// At 1024, the distance uses 9+ extra bits and this seems to be the sweet spot
592592
// on tested files.
593593
if distance > 1024 {

src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ fn main() {
2222

2323
for filename in env::args().skip(1) {
2424
let file = File::open(&filename)
25-
.unwrap_or_else(|why| panic!("couldn't open {}: {}", filename, why));
25+
.unwrap_or_else(|why| panic!("couldn't open {filename}: {why}"));
2626
let filesize = file.metadata().map(|x| x.len()).unwrap() as usize;
2727

28-
let out_filename = format!("{}{}", filename, extension);
28+
let out_filename = format!("{filename}{extension}");
2929

3030
// Attempt to create the output file, panic if the output file could not be opened
3131
let out_file = File::create(&out_filename)
32-
.unwrap_or_else(|why| panic!("couldn't create output file {}: {}", out_filename, why));
32+
.unwrap_or_else(|why| panic!("couldn't create output file {out_filename}: {why}"));
3333
let mut out_file = WriteStatistics::new(out_file);
3434

3535
zopfli::compress(options, output_type, &file, &mut out_file).unwrap_or_else(|why| {
36-
panic!("couldn't write to output file {}: {}", out_filename, why)
36+
panic!("couldn't write to output file {out_filename}: {why}")
3737
});
3838

3939
let out_size = out_file.count;
@@ -52,8 +52,8 @@ struct WriteStatistics<W> {
5252
}
5353

5454
impl<W> WriteStatistics<W> {
55-
fn new(inner: W) -> Self {
56-
WriteStatistics { inner, count: 0 }
55+
const fn new(inner: W) -> Self {
56+
Self { inner, count: 0 }
5757
}
5858
}
5959

0 commit comments

Comments
 (0)