|
33 | 33 | // * The `raw` and `bytes` submodules. |
34 | 34 | // * Boilerplate trait implementations. |
35 | 35 |
|
| 36 | +use borrow::Borrow; |
36 | 37 | use cmp::Ordering::{self, Less, Equal, Greater}; |
37 | 38 | use cmp; |
38 | 39 | use fmt; |
@@ -94,15 +95,17 @@ pub trait SliceExt { |
94 | 95 | #[stable(feature = "core", since = "1.6.0")] |
95 | 96 | fn as_ptr(&self) -> *const Self::Item; |
96 | 97 | #[stable(feature = "core", since = "1.6.0")] |
97 | | - fn binary_search(&self, x: &Self::Item) -> Result<usize, usize> |
98 | | - where Self::Item: Ord; |
| 98 | + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> |
| 99 | + where Self::Item: Borrow<Q>, |
| 100 | + Q: Ord; |
99 | 101 | #[stable(feature = "core", since = "1.6.0")] |
100 | 102 | fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> |
101 | 103 | where F: FnMut(&'a Self::Item) -> Ordering; |
102 | 104 | #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] |
103 | | - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> |
| 105 | + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result<usize, usize> |
104 | 106 | where F: FnMut(&'a Self::Item) -> B, |
105 | | - B: Ord; |
| 107 | + B: Borrow<Q>, |
| 108 | + Q: Ord; |
106 | 109 | #[stable(feature = "core", since = "1.6.0")] |
107 | 110 | fn len(&self) -> usize; |
108 | 111 | #[stable(feature = "core", since = "1.6.0")] |
@@ -477,8 +480,8 @@ impl<T> SliceExt for [T] { |
477 | 480 | m >= n && needle == &self[m-n..] |
478 | 481 | } |
479 | 482 |
|
480 | | - fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { |
481 | | - self.binary_search_by(|p| p.cmp(x)) |
| 483 | + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> where T: Borrow<Q>, Q: Ord { |
| 484 | + self.binary_search_by(|p| p.borrow().cmp(x)) |
482 | 485 | } |
483 | 486 |
|
484 | 487 | #[inline] |
@@ -506,11 +509,12 @@ impl<T> SliceExt for [T] { |
506 | 509 | } |
507 | 510 |
|
508 | 511 | #[inline] |
509 | | - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> |
| 512 | + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result<usize, usize> |
510 | 513 | where F: FnMut(&'a Self::Item) -> B, |
511 | | - B: Ord |
| 514 | + B: Borrow<Q>, |
| 515 | + Q: Ord |
512 | 516 | { |
513 | | - self.binary_search_by(|k| f(k).cmp(b)) |
| 517 | + self.binary_search_by(|k| f(k).borrow().cmp(b)) |
514 | 518 | } |
515 | 519 | } |
516 | 520 |
|
|
0 commit comments