Skip to content

Commit 50ecdc3

Browse files
Use iterator to pick best entry
1 parent 5bc9884 commit 50ecdc3

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/codecs/ico/decoder.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,16 @@ fn read_entry<R: Read>(r: &mut R) -> ImageResult<DirEntry> {
197197
/// do exist. Since we can't make an educated guess which one is best, picking
198198
/// the first one is a reasonable default.
199199
fn best_entry(entries: Vec<DirEntry>) -> ImageResult<DirEntry> {
200-
let mut best = *entries.first().ok_or(DecoderError::NoEntries)?;
201-
let mut best_score = (0, 0);
202-
203-
for entry in entries {
204-
let score = (
205-
entry.bits_per_pixel,
206-
u32::from(entry.real_width()) * u32::from(entry.real_height()),
207-
);
208-
if score > best_score {
209-
best = entry;
210-
best_score = score;
211-
}
212-
}
213-
Ok(best)
200+
entries
201+
.into_iter()
202+
.rev() // ties should pick the first entry, not the last
203+
.max_by_key(|entry| {
204+
(
205+
entry.bits_per_pixel,
206+
u32::from(entry.real_width()) * u32::from(entry.real_height()),
207+
)
208+
})
209+
.ok_or(DecoderError::NoEntries.into())
214210
}
215211

216212
impl DirEntry {

0 commit comments

Comments
 (0)