-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add slice::contains_ref to supplement slice::contains #74044
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
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
@sfackler This is a triage bump. |
☔ The latest upstream changes (presumably #73265) made this pull request unmergeable. Please resolve the merge conflicts. |
215d435
to
6751769
Compare
r? @scottmcm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is one of those things that's almost all API decisions, so while I have no technical issues with the code, I don't feel like this is something I can approve myself, even for unstable.
So ping @rust-lang/libs, as I didn't notice any comments from them about this approach in #42671, and marking waiting-on-team.
A handful of ponderings:
- Is this the right name?
[T]::contains
also takes a ref, so it's not obvious to me thatcontains_ref
is the right distinguisher. - Is the
PartialEq<Other>
approach the best one, given that it's a new method without the inference-breakage restrictions of the existingcontains
? For example, is there a way this can support simpler callers, like allowing.contains_bikeshed(0)
instead of needing the caller to pass&0
?
/// assert!(v.contains_ref("Hello")); | ||
/// assert!(!v.contains_ref("Rust")); | ||
/// ``` | ||
#[unstable(feature = "contains_ref", issue = "none")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this gets a "seems plausible" from libs, please do create a tracking issue for this: https://github.com/rust-lang/rust/issues/new?labels=C-tracking-issue&template=tracking_issue.md&title=Tracking+Issue+for+slice::contains_ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remember it !
Does this example does what you want @scottmcm ? struct MyNum(u8);
struct MyOtherNum(u8);
impl PartialEq<u8> for MyNum {
fn eq(&self, other: &u8) -> bool {
self.0 == *other
}
}
impl PartialEq<MyOtherNum> for MyNum {
fn eq(&self, other: &MyOtherNum) -> bool {
self.0 == other.0
}
}
impl PartialEq<&u8> for MyNum {
fn eq(&self, other: &&u8) -> bool {
self.0 == **other
}
}
impl PartialEq<&MyOtherNum> for MyNum {
fn eq(&self, other: &&MyOtherNum) -> bool {
self.0 == other.0
}
}
// Same functionality as the `contains_ref` method in the PR.
fn search<T, U>(sl: &[T], other: U) -> bool
// ^ takes `U` instead of `&U`
where
T: PartialEq<U>,
// No `U: ?Sized` here
{
sl.iter().any(|e| e == &other)
// ^ uses a `&` here
}
fn main() {
let v = [String::from("Hello"), String::from("world")];
assert!(search(&v, "Hello"));
assert!(!search(&v, "Rust"));
let v = [0, 2];
assert!(search(&v, 0));
assert!(!search(&v, 1));
let v = [MyNum(0), MyNum(2)];
assert!(search(&v, 0));
assert!(!search(&v, 1));
assert!(search(&v, &0));
assert!(!search(&v, &1));
assert!(search(&v, MyOtherNum(0)));
assert!(!search(&v, MyOtherNum(2)));
assert!(search(&v, &MyOtherNum(0)));
assert!(!search(&v, &MyOtherNum(2)));
} It needs the implementation for fn do_something<T, U>(sl: &[T], other: &U)
where
T: PartialEq<U>,
{
search(sl, other);
// ^^^^^ expected type parameter `U`, found `&U`
// but `U` is not `Copy`.
// ...
} |
I don't think the name |
I agree with that, I just don't have any idea for a better name. |
I also think the name is problematic, but also can't think of a better one. Unless we find a nice name, I would prefer not to add this API: |
Feel free to close if there is no idea for a better name. It may be worth closing the original issue too in this case since the |
Triage: What's the status here? Should this be closed? |
Fixes #42671.
This adds the last function proposed in #42671:
I simply used
self.iter().any(|e| e == x)
as a first implementation, I don't know if there is a more optimised version possible.I put an
unstable
flag, should I create a tracking issue for this ? I don't know if this is a "big enough" feature for it ?@rustbot modify labels: A-collections,C-enhancement,T-doc,T-libs