|
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;
|
@@ -100,15 +101,17 @@ pub trait SliceExt {
|
100 | 101 | #[stable(feature = "core", since = "1.6.0")]
|
101 | 102 | fn as_ptr(&self) -> *const Self::Item;
|
102 | 103 | #[stable(feature = "core", since = "1.6.0")]
|
103 |
| - fn binary_search(&self, x: &Self::Item) -> Result<usize, usize> |
104 |
| - where Self::Item: Ord; |
| 104 | + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> |
| 105 | + where Self::Item: Borrow<Q>, |
| 106 | + Q: Ord; |
105 | 107 | #[stable(feature = "core", since = "1.6.0")]
|
106 | 108 | fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
|
107 | 109 | where F: FnMut(&'a Self::Item) -> Ordering;
|
108 | 110 | #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
|
109 |
| - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> |
| 111 | + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result<usize, usize> |
110 | 112 | where F: FnMut(&'a Self::Item) -> B,
|
111 |
| - B: Ord; |
| 113 | + B: Borrow<Q>, |
| 114 | + Q: Ord; |
112 | 115 | #[stable(feature = "core", since = "1.6.0")]
|
113 | 116 | fn len(&self) -> usize;
|
114 | 117 | #[stable(feature = "core", since = "1.6.0")]
|
@@ -493,8 +496,8 @@ impl<T> SliceExt for [T] {
|
493 | 496 | m >= n && needle == &self[m-n..]
|
494 | 497 | }
|
495 | 498 |
|
496 |
| - fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { |
497 |
| - self.binary_search_by(|p| p.cmp(x)) |
| 499 | + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> where T: Borrow<Q>, Q: Ord { |
| 500 | + self.binary_search_by(|p| p.borrow().cmp(x)) |
498 | 501 | }
|
499 | 502 |
|
500 | 503 | #[inline]
|
@@ -522,11 +525,12 @@ impl<T> SliceExt for [T] {
|
522 | 525 | }
|
523 | 526 |
|
524 | 527 | #[inline]
|
525 |
| - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> |
| 528 | + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result<usize, usize> |
526 | 529 | where F: FnMut(&'a Self::Item) -> B,
|
527 |
| - B: Ord |
| 530 | + B: Borrow<Q>, |
| 531 | + Q: Ord |
528 | 532 | {
|
529 |
| - self.binary_search_by(|k| f(k).cmp(b)) |
| 533 | + self.binary_search_by(|k| f(k).borrow().cmp(b)) |
530 | 534 | }
|
531 | 535 | }
|
532 | 536 |
|
|
0 commit comments