Skip to content

Commit c7a787a

Browse files
committed
liballoc: VecDeque: Simplify binary_search_by()
1 parent e0506d1 commit c7a787a

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

library/alloc/src/collections/vec_deque.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -2516,24 +2516,13 @@ impl<T> VecDeque<T> {
25162516
where
25172517
F: FnMut(&'a T) -> Ordering,
25182518
{
2519-
if self.is_empty() {
2520-
return Err(0);
2521-
}
2522-
25232519
let (front, back) = self.as_slices();
25242520

2525-
match back.first().map(|elem| f(elem)) {
2526-
Some(Ordering::Equal) => return Ok(front.len()),
2527-
Some(Ordering::Less) => {
2528-
return back[1..]
2529-
.binary_search_by(f)
2530-
.map(|idx| idx + front.len() + 1)
2531-
.map_err(|idx| idx + front.len() + 1);
2532-
}
2533-
_ => {}
2521+
if let Some(Ordering::Less | Ordering::Equal) = back.first().map(|elem| f(elem)) {
2522+
back.binary_search_by(f).map(|idx| idx + front.len()).map_err(|idx| idx + front.len())
2523+
} else {
2524+
front.binary_search_by(f)
25342525
}
2535-
2536-
front.binary_search_by(f)
25372526
}
25382527

25392528
/// Binary searches this sorted `VecDeque` with a key extraction function.

0 commit comments

Comments
 (0)