From 6fd1752b251cef0325e13eb22f7991ce0f3688be Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Mon, 11 Jul 2016 18:18:11 +0530 Subject: [PATCH] extend lifetime on binary_search_by_key of SliceExt trait --- src/libcollections/slice.rs | 8 +++---- src/libcore/slice.rs | 16 ++++++------- src/test/run-pass/slice_binary_search.rs | 29 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 src/test/run-pass/slice_binary_search.rs diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 1f8eea56fc6..462b16f0331 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -950,8 +950,8 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn binary_search_by(&self, f: F) -> Result - where F: FnMut(&T) -> Ordering + pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result + where F: FnMut(&'a T) -> Ordering { core_slice::SliceExt::binary_search_by(self, f) } @@ -986,8 +986,8 @@ impl [T] { /// ``` #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] #[inline] - pub fn binary_search_by_key(&self, b: &B, f: F) -> Result - where F: FnMut(&T) -> B, + pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result + where F: FnMut(&'a T) -> B, B: Ord { core_slice::SliceExt::binary_search_by_key(self, b, f) diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index d8a11581c3b..3141c289e93 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -105,11 +105,11 @@ pub trait SliceExt { fn binary_search(&self, x: &Self::Item) -> Result where Self::Item: Ord; #[stable(feature = "core", since = "1.6.0")] - fn binary_search_by(&self, f: F) -> Result - where F: FnMut(&Self::Item) -> Ordering; + fn binary_search_by<'a, F>(&'a self, f: F) -> Result + where F: FnMut(&'a Self::Item) -> Ordering; #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] - fn binary_search_by_key(&self, b: &B, f: F) -> Result - where F: FnMut(&Self::Item) -> B, + fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result + where F: FnMut(&'a Self::Item) -> B, B: Ord; #[stable(feature = "core", since = "1.6.0")] fn len(&self) -> usize; @@ -301,8 +301,8 @@ impl SliceExt for [T] { self as *const [T] as *const T } - fn binary_search_by(&self, mut f: F) -> Result where - F: FnMut(&T) -> Ordering + fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result + where F: FnMut(&'a T) -> Ordering { let mut base = 0usize; let mut s = self; @@ -514,8 +514,8 @@ impl SliceExt for [T] { } #[inline] - fn binary_search_by_key(&self, b: &B, mut f: F) -> Result - where F: FnMut(&Self::Item) -> B, + fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result + where F: FnMut(&'a Self::Item) -> B, B: Ord { self.binary_search_by(|k| f(k).cmp(b)) diff --git a/src/test/run-pass/slice_binary_search.rs b/src/test/run-pass/slice_binary_search.rs new file mode 100644 index 00000000000..80b370d58fc --- /dev/null +++ b/src/test/run-pass/slice_binary_search.rs @@ -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 or the MIT license +// , 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)); +}