Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend lifetime on binary_search_by_key of SliceExt trait #34762

Merged
merged 1 commit into from
Aug 9, 2016
Merged
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
8 changes: 4 additions & 4 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> Ordering
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> Ordering
{
core_slice::SliceExt::binary_search_by(self, f)
}
Expand Down Expand Up @@ -986,8 +986,8 @@ impl<T> [T] {
/// ```
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
#[inline]
pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> B,
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
{
core_slice::SliceExt::binary_search_by_key(self, b, f)
Expand Down
16 changes: 8 additions & 8 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ pub trait SliceExt {
fn binary_search(&self, x: &Self::Item) -> Result<usize, usize>
where Self::Item: Ord;
#[stable(feature = "core", since = "1.6.0")]
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&Self::Item) -> Ordering;
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<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&Self::Item) -> B,
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&'a Self::Item) -> B,
B: Ord;
#[stable(feature = "core", since = "1.6.0")]
fn len(&self) -> usize;
Expand Down Expand Up @@ -301,8 +301,8 @@ impl<T> SliceExt for [T] {
self as *const [T] as *const T
}

fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize> where
F: FnMut(&T) -> Ordering
fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> Ordering
{
let mut base = 0usize;
let mut s = self;
Expand Down Expand Up @@ -514,8 +514,8 @@ impl<T> SliceExt for [T] {
}

#[inline]
fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
where F: FnMut(&Self::Item) -> B,
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
where F: FnMut(&'a Self::Item) -> B,
B: Ord
{
self.binary_search_by(|k| f(k).cmp(b))
Expand Down
29 changes: 29 additions & 0 deletions src/test/run-pass/slice_binary_search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test binary_search_by_key lifetime. Issue #34683

#[derive(Debug)]
struct Assignment {
topic: String,
partition: i32,
}

fn main() {
let xs = vec![
Assignment { topic: "abc".into(), partition: 1 },
Assignment { topic: "def".into(), partition: 2 },
Assignment { topic: "ghi".into(), partition: 3 },
];

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