Skip to content

Commit

Permalink
Rollup merge of #95743 - yaahc:binary-search-clarification, r=Mark-Si…
Browse files Browse the repository at this point in the history
…mulacrum

Update binary_search example to instead redirect to partition_point

Inspired by discussion in the tracking issue for `Result::into_ok_or_err`: #82223 (comment)

People are surprised by us not providing a `Result<T, T> -> T` conversion, and the main culprit for this confusion seems to be the `binary_search` API. We should instead redirect people to the equivalent API that implicitly does that `Result<T, T> -> T` conversion internally which should obviate the need for the `into_ok_or_err` function and give us time to work towards a more general solution that applies to all enums rather than just `Result` such as making or_patterns usable for situations like this via postfix `match`.

I choose to duplicate the example rather than simply moving it from `binary_search` to partition point because most of the confusion seems to arise when people are looking at `binary_search`. It makes sense to me to have the example presented immediately rather than requiring people to click through to even realize there is an example. If I had to put it in only one place I'd leave it in `binary_search` and remove it from `partition_point` but it seems pretty obviously relevant to `partition_point` so I figured the best option would be to duplicate it.
  • Loading branch information
matthiaskrgr authored Apr 11, 2022
2 parents 48a9e10 + 0eb0d89 commit e25bc30
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
18 changes: 16 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2593,14 +2593,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
///
/// If you want to insert an item to a sorted deque, while maintaining
/// sort order:
/// sort order, consider using [`partition_point`]:
///
/// ```
/// use std::collections::VecDeque;
///
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let num = 42;
/// let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
/// let idx = deque.partition_point(|&x| x < num);
/// // The above is equivalent to `let idx = deque.binary_search(&num).unwrap_or_else(|x| x);`
/// deque.insert(idx, num);
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
Expand Down Expand Up @@ -2744,6 +2745,19 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// assert!(deque.iter().take(i).all(|&x| x < 5));
/// assert!(deque.iter().skip(i).all(|&x| !(x < 5)));
/// ```
///
/// If you want to insert an item to a sorted deque, while maintaining
/// sort order:
///
/// ```
/// use std::collections::VecDeque;
///
/// let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
/// let num = 42;
/// let idx = deque.partition_point(|&x| x < num);
/// deque.insert(idx, num);
/// assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
#[stable(feature = "vecdeque_binary_search", since = "1.54.0")]
pub fn partition_point<P>(&self, mut pred: P) -> usize
where
Expand Down
16 changes: 14 additions & 2 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,12 +2331,13 @@ impl<T> [T] {
/// ```
///
/// If you want to insert an item to a sorted vector, while maintaining
/// sort order:
/// sort order, consider using [`partition_point`]:
///
/// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 42;
/// let idx = s.binary_search(&num).unwrap_or_else(|x| x);
/// let idx = s.partition_point(|&x| x < num);
/// // The above is equivalent to `let idx = s.binary_search(&num).unwrap_or_else(|x| x);`
/// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
Expand Down Expand Up @@ -3743,6 +3744,17 @@ impl<T> [T] {
/// assert!(v[..i].iter().all(|&x| x < 5));
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
/// ```
///
/// If you want to insert an item to a sorted vector, while maintaining
/// sort order:
///
/// ```
/// let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
/// let num = 42;
/// let idx = s.partition_point(|&x| x < num);
/// s.insert(idx, num);
/// assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
/// ```
#[stable(feature = "partition_point", since = "1.52.0")]
#[must_use]
pub fn partition_point<P>(&self, mut pred: P) -> usize
Expand Down

0 comments on commit e25bc30

Please sign in to comment.