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.
This commit is contained in:
Alex Crichton 2016-12-20 11:16:17 -08:00 committed by GitHub
commit 7fe44f77f4

View file

@ -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))
}
}