-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Conversation
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 rust-lang#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 rust-lang#43020).
r? @bluss (rust_highfive has picked a reviewer for you, use r? to override) |
Investigating the issue with |
The issue here is that some crates define I'm not sure of the best way to address this problem: I think we certainly want to warn/error against non-symmetric partial equalities (or implement them automatically), but that would take time. Can you think of any ways to get around this that wouldn't involve updating the incorrect crates? EDIT: On the other hand, if a crate has previously used |
I think I may have found a way to make this work... A reduction of what this implementation ran into: (playground link) trait Contains<T>
where
T: ?Sized,
{
fn contains<U>(&self, x: &U) -> bool
where
T: PartialEq<U>,
U: ?Sized,
{
true
}
}
impl<'a, T> Contains<T> for &'a [T] {}
#[test]
fn test() {
let a: &str = "string";
let v: &[&str] = &[a];
Contains::contains(&v, a);
}
In these types, we have fn contains<'a, U>(&self, x: &'a U) -> bool
where T: PartialEq<&'a U> The other choice is to go the other way and make the reference part of the fn contains<U>&self, x: U) -> bool
where T: PartialEq<U> I think this is the solution that we want to use here, assuming it works, but one of the two definitely should. @varkor ? On
The only impl that doesn't involve non- |
@CAD97: In order to conserve the behaviour of type signature of Regarding the question about the nonexistence of |
OK, I missed some of the complexity here, due to only working with my minimized example. Here's an updated small example: playground. |
After implementing |
@bors try |
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).
☀️ Test successful - status-travis |
@aidanhs @Mark-Simulacrum This needs a crater run (new= |
Crater run started. |
#46713 has broken this implementation. I'll figure out what to do if the Crater run has a positive outcome, because a fix is non-trivial. |
Crater run failed and started again (workaround for the issue has been put in place). |
Okay, it's quite clear that this causes a ton of type inference issues. Breakage is probably not worth the benefit. I'll close and mark #42671 as unlikely to be fixed if there are no other reservations? |
Just to showcase why naked unqalified
This code would want this PR, to use A whole large part of the crater issues are exactly like that, and it's a shame they are blocking themselves from improving. There are other less simple cases though. |
I suppose default type parameter fallback would allow this by setting |
I'm going to go ahead and close this PR, looks like another approach is being discussed over at the original issue though. |
This allows
contains
to be used on slices to search for elements thatsatisfy 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 implementingSized
. It’s likely this will have to go through a Crater run to testfor breakage (see #43020).