Skip to content

Commit 58c5716

Browse files
authoredAug 9, 2016
Auto merge of #34762 - creativcoder:slice-ext, r=alexcrichton
extend lifetime on binary_search_by_key of SliceExt trait Fixes #34683.
2 parents 080e0e0 + 6fd1752 commit 58c5716

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed
 

‎src/libcollections/slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,8 @@ impl<T> [T] {
973973
/// ```
974974
#[stable(feature = "rust1", since = "1.0.0")]
975975
#[inline]
976-
pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
977-
where F: FnMut(&T) -> Ordering
976+
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
977+
where F: FnMut(&'a T) -> Ordering
978978
{
979979
core_slice::SliceExt::binary_search_by(self, f)
980980
}
@@ -1009,8 +1009,8 @@ impl<T> [T] {
10091009
/// ```
10101010
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
10111011
#[inline]
1012-
pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
1013-
where F: FnMut(&T) -> B,
1012+
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
1013+
where F: FnMut(&'a T) -> B,
10141014
B: Ord
10151015
{
10161016
core_slice::SliceExt::binary_search_by_key(self, b, f)

‎src/libcore/slice.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ pub trait SliceExt {
105105
fn binary_search(&self, x: &Self::Item) -> Result<usize, usize>
106106
where Self::Item: Ord;
107107
#[stable(feature = "core", since = "1.6.0")]
108-
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
109-
where F: FnMut(&Self::Item) -> Ordering;
108+
fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
109+
where F: FnMut(&'a Self::Item) -> Ordering;
110110
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
111-
fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
112-
where F: FnMut(&Self::Item) -> B,
111+
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
112+
where F: FnMut(&'a Self::Item) -> B,
113113
B: Ord;
114114
#[stable(feature = "core", since = "1.6.0")]
115115
fn len(&self) -> usize;
@@ -301,8 +301,8 @@ impl<T> SliceExt for [T] {
301301
self as *const [T] as *const T
302302
}
303303

304-
fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize> where
305-
F: FnMut(&T) -> Ordering
304+
fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
305+
where F: FnMut(&'a T) -> Ordering
306306
{
307307
let mut base = 0usize;
308308
let mut s = self;
@@ -514,8 +514,8 @@ impl<T> SliceExt for [T] {
514514
}
515515

516516
#[inline]
517-
fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
518-
where F: FnMut(&Self::Item) -> B,
517+
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
518+
where F: FnMut(&'a Self::Item) -> B,
519519
B: Ord
520520
{
521521
self.binary_search_by(|k| f(k).cmp(b))
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test binary_search_by_key lifetime. Issue #34683
12+
13+
#[derive(Debug)]
14+
struct Assignment {
15+
topic: String,
16+
partition: i32,
17+
}
18+
19+
fn main() {
20+
let xs = vec![
21+
Assignment { topic: "abc".into(), partition: 1 },
22+
Assignment { topic: "def".into(), partition: 2 },
23+
Assignment { topic: "ghi".into(), partition: 3 },
24+
];
25+
26+
let key: &str = "def";
27+
let r = xs.binary_search_by_key(&key, |e| &e.topic);
28+
assert_eq!(Ok(1), r.map(|i| i));
29+
}

0 commit comments

Comments
 (0)