Skip to content

Commit 90d7338

Browse files
committed
Generalise slice::contains over PartialEq
This allows `contains` to be used on slices to search for elements that satisfy a partial equality with the slice item type, rather than an identity. This closes #42671. Note that string literals need to be referenced in this implementation due to `str` not implementing `Sized`. It’s likely this will have to go through a Crater run to test for breakage (see #43020).
1 parent 250b492 commit 90d7338

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/liballoc/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ impl<T> [T] {
980980
/// assert!(!v.contains(&50));
981981
/// ```
982982
#[stable(feature = "rust1", since = "1.0.0")]
983-
pub fn contains(&self, x: &T) -> bool
984-
where T: PartialEq
983+
pub fn contains<U>(&self, x: &U) -> bool
984+
where T: PartialEq<U>
985985
{
986986
core_slice::SliceExt::contains(self, x)
987987
}

src/liballoc/tests/slice.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,19 @@ fn test_shrink_to_fit() {
10511051
assert_eq!(xs, (0..100).collect::<Vec<_>>());
10521052
}
10531053

1054+
#[test]
1055+
fn test_contains() {
1056+
let numbers = [1, 2, 3];
1057+
assert!(numbers.contains(&2));
1058+
assert!(!numbers.contains(&4));
1059+
1060+
let strings = vec![String::from("AB"), String::from("CD")];
1061+
assert!(strings.contains(&String::from("AB")));
1062+
assert!(!strings.contains(&String::from("A")));
1063+
assert!(strings.contains(&"AB"));
1064+
assert!(!strings.contains(&"BC"));
1065+
}
1066+
10541067
#[test]
10551068
fn test_starts_with() {
10561069
assert!(b"foobar".starts_with(b"foo"));

src/libcore/slice/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub trait SliceExt {
192192
fn as_mut_ptr(&mut self) -> *mut Self::Item;
193193

194194
#[stable(feature = "core", since = "1.6.0")]
195-
fn contains(&self, x: &Self::Item) -> bool where Self::Item: PartialEq;
195+
fn contains<U>(&self, x: &U) -> bool where Self::Item: PartialEq<U>;
196196

197197
#[stable(feature = "core", since = "1.6.0")]
198198
fn starts_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
@@ -618,8 +618,8 @@ impl<T> SliceExt for [T] {
618618
}
619619

620620
#[inline]
621-
fn contains(&self, x: &T) -> bool where T: PartialEq {
622-
self.iter().any(|elt| *x == *elt)
621+
fn contains<U>(&self, x: &U) -> bool where T: PartialEq<U> {
622+
self.iter().any(|e| e == x)
623623
}
624624

625625
#[inline]

0 commit comments

Comments
 (0)