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

Generalise slice::contains over PartialEq #46934

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@ impl<T> [T] {
/// assert!(!v.contains(&50));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq
pub fn contains<U>(&self, x: &U) -> bool
where U: ?Sized, T: PartialEq<U>
{
core_slice::SliceExt::contains(self, x)
}
Expand Down
14 changes: 14 additions & 0 deletions src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,20 @@ fn test_shrink_to_fit() {
assert_eq!(xs, (0..100).collect::<Vec<_>>());
}

#[test]
fn test_contains() {
let numbers = [1, 2, 3];
assert!(numbers.contains(&2));
assert!(!numbers.contains(&4));

let strings = vec![String::from("AB"), String::from("CD")];
assert!(strings.contains(&String::from("AB")));
assert!(!strings.contains(&String::from("A")));
assert!(strings.contains(&"AB"));
assert!(!strings.contains(&"BC"));
assert!(strings.contains("AB"));
}

#[test]
fn test_starts_with() {
assert!(b"foobar".starts_with(b"foo"));
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub trait SliceExt {
fn as_mut_ptr(&mut self) -> *mut Self::Item;

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

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

#[inline]
fn contains(&self, x: &T) -> bool where T: PartialEq {
self.iter().any(|elt| *x == *elt)
fn contains<U>(&self, x: &U) -> bool where U: ?Sized, T: PartialEq<U> {
self.iter().any(|e| e == x)
}

#[inline]
Expand Down
16 changes: 16 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,22 @@ mod traits {
fn ne(&self, other: &str) -> bool { !(*self).eq(other) }
}

#[unstable(feature = "str_str_ref_partialeq", issue = "46934")]
impl<'a> PartialEq<&'a str> for str {
#[inline]
fn eq(&self, other: &&'a str) -> bool { self == *other }
#[inline]
fn ne(&self, other: &&'a str) -> bool { self != *other }
}

#[unstable(feature = "str_str_ref_partialeq", issue = "46934")]
impl<'a> PartialEq<str> for &'a str {
#[inline]
fn eq(&self, other: &str) -> bool { *self == other }
#[inline]
fn ne(&self, other: &str) -> bool { *self != other }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for str {}

Expand Down