From 99541bef25c28a057181f775b7b208d5060c5c5f Mon Sep 17 00:00:00 2001 From: Chris Stankus Date: Thu, 27 Apr 2017 16:43:18 -0500 Subject: [PATCH 1/4] Fix slice binary search signature mismatch The signature for `std::slice::binary_search` is now parameterized over `Borrow`, as in `core::SliceExt` --- src/libcollections/slice.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 2eef132374e58..5f9ae5d83a5b6 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1046,8 +1046,9 @@ impl [T] { /// assert!(match r { Ok(1...4) => true, _ => false, }); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn binary_search(&self, x: &T) -> Result - where T: Ord + pub fn binary_search(&self, x: &Q) -> Result + where T: Borrow, + Q: Ord { core_slice::SliceExt::binary_search(self, x) } @@ -1123,9 +1124,10 @@ impl [T] { /// ``` #[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 + pub fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result where F: FnMut(&'a T) -> B, - B: Ord + B: Borrow, + Q: Ord { core_slice::SliceExt::binary_search_by_key(self, b, f) } From 84993c6664d578b1a38efa5965719d03830b634f Mon Sep 17 00:00:00 2001 From: Chris Stankus Date: Mon, 1 May 2017 22:19:37 -0500 Subject: [PATCH 2/4] binary_search_by_key comparator returns reference --- src/libcollections/slice.rs | 4 ++-- src/libcore/slice/mod.rs | 8 ++++---- src/test/run-pass/slice_binary_search.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 5f9ae5d83a5b6..44dc9f80503a8 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1125,8 +1125,8 @@ impl [T] { #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] #[inline] pub fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, f: F) -> Result - where F: FnMut(&'a T) -> B, - B: Borrow, + where F: FnMut(&'a T) -> &'a B, + B: Borrow + 'a, Q: Ord { core_slice::SliceExt::binary_search_by_key(self, b, f) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 87dfdfe57b65c..fb453640bf26f 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -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 - where F: FnMut(&'a Self::Item) -> B, - B: Borrow, + where F: FnMut(&'a Self::Item) -> &'a B, + B: Borrow + 'a, Q: Ord; #[stable(feature = "core", since = "1.6.0")] @@ -612,8 +612,8 @@ impl SliceExt for [T] { #[inline] fn binary_search_by_key<'a, B, F, Q: ?Sized>(&'a self, b: &Q, mut f: F) -> Result - where F: FnMut(&'a Self::Item) -> B, - B: Borrow, + where F: FnMut(&'a Self::Item) -> &'a B, + B: Borrow + 'a, Q: Ord { self.binary_search_by(|k| f(k).borrow().cmp(b)) diff --git a/src/test/run-pass/slice_binary_search.rs b/src/test/run-pass/slice_binary_search.rs index 80b370d58fc53..5c4a4df48b317 100644 --- a/src/test/run-pass/slice_binary_search.rs +++ b/src/test/run-pass/slice_binary_search.rs @@ -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)); } From 0cf33244fcb90f5329a3e3e66bac83c647fd5da1 Mon Sep 17 00:00:00 2001 From: Chris Stankus Date: Wed, 3 May 2017 19:25:51 -0500 Subject: [PATCH 3/4] Fix type inference in binary_search test --- src/libcollections/tests/slice.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcollections/tests/slice.rs b/src/libcollections/tests/slice.rs index c3e5304fb2b35..3e1d0ba5e63d1 100644 --- a/src/libcollections/tests/slice.rs +++ b/src/libcollections/tests/slice.rs @@ -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); From 8016849bb4ba4b7321d48dadcd57c6958d3bb8d5 Mon Sep 17 00:00:00 2001 From: Chris Stankus Date: Thu, 4 May 2017 10:56:37 -0500 Subject: [PATCH 4/4] Fix slice::binary_search_by_key doc tests --- src/libcollections/slice.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 44dc9f80503a8..1e1ecaf843f9a 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1116,10 +1116,10 @@ impl [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")]