Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,9 @@ impl<T> [T] {
/// assert!(match r { Ok(1...4) => true, _ => false, });
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
where T: Ord
pub fn binary_search<Q: ?Sized>(&self, x: &Q) -> Result<usize, usize>
where T: Borrow<Q>,
Q: Ord
{
core_slice::SliceExt::binary_search(self, x)
}
Expand Down Expand Up @@ -1115,17 +1116,18 @@ impl<T> [T] {
/// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
/// (1, 21), (2, 34), (4, 55)];
///
/// assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
/// assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7));
/// assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13));
/// let r = s.binary_search_by_key(&1, |&(a,b)| b);
/// assert_eq!(s.binary_search_by_key(&13, |&(a, ref b)| b), Ok(9));
/// assert_eq!(s.binary_search_by_key(&4, |&(a, ref b)| b), Err(7));
/// assert_eq!(s.binary_search_by_key(&100, |&(a, ref b)| b), Err(13));
/// let r = s.binary_search_by_key(&1, |&(a, ref b)| b);
/// assert!(match r { Ok(1...4) => true, _ => false, });
/// ```
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
#[inline]
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> B,
B: Ord
pub fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> &'a B,
B: Borrow<Q> + 'a,
Q: Ord
{
core_slice::SliceExt::binary_search_by_key(self, b, f)
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ fn test_binary_search() {
assert_eq!([2].binary_search(&5).ok(), None);
assert_eq!([2].binary_search(&2).ok(), Some(0));

assert_eq!([].binary_search(&1).ok(), None);
assert_eq!([].binary_search(&5).ok(), None);
assert_eq!([0;0].binary_search(&1).ok(), None);
assert_eq!([0;0].binary_search(&5).ok(), None);

assert!([1, 1, 1, 1, 1].binary_search(&1).ok() != None);
assert!([1, 1, 1, 1, 2].binary_search(&1).ok() != None);
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ pub trait SliceExt {

#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
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: Borrow<Q>,
where F: FnMut(&'a Self::Item) -> &'a B,
B: Borrow<Q> + 'a,
Q: Ord;

#[stable(feature = "core", since = "1.6.0")]
Expand Down Expand Up @@ -612,8 +612,8 @@ impl<T> SliceExt for [T] {

#[inline]
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: Borrow<Q>,
where F: FnMut(&'a Self::Item) -> &'a B,
B: Borrow<Q> + 'a,
Q: Ord
{
self.binary_search_by(|k| f(k).borrow().cmp(b))
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/slice_binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ fn main() {
];

let key: &str = "def";
let r = xs.binary_search_by_key(&key, |e| &e.topic);
let r = xs.binary_search_by_key(key, |e| &e.topic);
assert_eq!(Ok(1), r.map(|i| i));
}