Skip to content

Commit bccbf9d

Browse files
committed
VecDeque: binary_search_by(): return right away if hit found at back.first() rust-lang#78021
1 parent 60158f4 commit bccbf9d

File tree

1 file changed

+4
-1
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+4
-1
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2481,8 +2481,11 @@ impl<T> VecDeque<T> {
24812481
F: FnMut(&'a T) -> Ordering,
24822482
{
24832483
let (front, back) = self.as_slices();
2484+
let cmp_back = back.first().map(|elem| f(elem));
24842485

2485-
if let Some(Ordering::Less | Ordering::Equal) = back.first().map(|elem| f(elem)) {
2486+
if let Some(Ordering::Equal) = cmp_back {
2487+
Ok(front.len())
2488+
} else if let Some(Ordering::Less) = cmp_back {
24862489
back.binary_search_by(f).map(|idx| idx + front.len()).map_err(|idx| idx + front.len())
24872490
} else {
24882491
front.binary_search_by(f)

0 commit comments

Comments
 (0)