Skip to content

Commit 45e5a64

Browse files
k_smallest: Work around suboptimality in peek_mut
See rust-lang/rust#75974 Co-authored-by: nicoo <nicoo@debian.org>
1 parent f31b617 commit 45e5a64

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/k_smallest.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(mut iter: I, k: usize) -
88

99
for i in iter {
1010
debug_assert_eq!(heap.len(), k);
11-
// Guaranteed not-None, since we keep exactly k>0 elements in the heap.
12-
let mut lorgest = heap.peek_mut().unwrap();
1311
// Equivalent to heap.push(min(i, heap.pop())) but more efficient.
14-
if *lorgest > i { *lorgest = i; }
12+
// This should be done with a single `.peek_mut().unwrap()` but
13+
// `PeekMut` sifts-down unconditionally on Rust 1.46.0 and prior.
14+
if *heap.peek().unwrap() > i {
15+
*heap.peek_mut().unwrap() = i;
16+
}
1517
}
1618

1719
heap

0 commit comments

Comments
 (0)