diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2016-12-20 11:16:17 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-20 11:16:17 -0800 |
| commit | 7fe44f77f4580b60cf9f22e0ab35156c0ce12824 (patch) | |
| tree | f6e5960fc1e9cb3610f4070eda309122324c6207 | |
| parent | 94ae2a2e6791e0c4ab6fba836b2b09a07f2d3c8a (diff) | |
| parent | c470d4a415fe6f091170cf7e55926e252252d0f9 (diff) | |
| download | rust-7fe44f77f4580b60cf9f22e0ab35156c0ce12824.tar.gz rust-7fe44f77f4580b60cf9f22e0ab35156c0ce12824.zip | |
Rollup merge of #37761 - christophebiocca:borrow-stdlib-fn-refactor, r=alexcrichton
Use Borrow for binary_search and contains methods in the standard library Fixes all standard library methods in #32822 that can be fixed without backwards compatibility issues.
| -rw-r--r-- | src/libcore/slice.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index a4a90e7a9da..e0a49e2ae45 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -33,6 +33,7 @@ // * The `raw` and `bytes` submodules. // * Boilerplate trait implementations. +use borrow::Borrow; use cmp::Ordering::{self, Less, Equal, Greater}; use cmp; use fmt; @@ -100,15 +101,17 @@ pub trait SliceExt { #[stable(feature = "core", since = "1.6.0")] fn as_ptr(&self) -> *const Self::Item; #[stable(feature = "core", since = "1.6.0")] - fn binary_search(&self, x: &Self::Item) -> Result<usize, usize> - where Self::Item: Ord; + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> + where Self::Item: Borrow<Q>, + Q: Ord; #[stable(feature = "core", since = "1.6.0")] fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> Ordering; #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> B, - B: Ord; + B: Borrow<Q>, + Q: Ord; #[stable(feature = "core", since = "1.6.0")] fn len(&self) -> usize; #[stable(feature = "core", since = "1.6.0")] @@ -493,8 +496,8 @@ impl<T> SliceExt for [T] { m >= n && needle == &self[m-n..] } - fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { - self.binary_search_by(|p| p.cmp(x)) + fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize> where T: Borrow<Q>, Q: Ord { + self.binary_search_by(|p| p.borrow().cmp(x)) } #[inline] @@ -522,11 +525,12 @@ impl<T> SliceExt for [T] { } #[inline] - fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> + fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result<usize, usize> where F: FnMut(&'a Self::Item) -> B, - B: Ord + B: Borrow<Q>, + Q: Ord { - self.binary_search_by(|k| f(k).cmp(b)) + self.binary_search_by(|k| f(k).borrow().cmp(b)) } } |
