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

slice: allow [T]::contains to accept any Q where T: Borrow<Q> #43020

Closed
wants to merge 1 commit into from

Conversation

sunshowers
Copy link
Contributor

This is most useful to allow matching string literals against a
[String], or bytestring literals against a [Vec<u8>].

Since Vec<T> derefs to [T], this also applies to Vec<String> and
Vec<Vec<u8>>.

This implements https://internals.rust-lang.org/t/vec-contains-should-accept-anything-that-t-implements-borrow-for/5455 -- I'm really not sure about the stability guarantees required here, so some guidance would be really appreciated. Thanks :)

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

This is most useful to allow matching string literals against a
`[String]`, or bytestring literals against a `[Vec<u8>]`.

Since `Vec<T>` derefs to `[T]`, this also applies to `Vec<String>` and
`Vec<Vec<u8>>`.
@sunshowers sunshowers changed the title slice: allow [T]::contains to accept any Borrow<T> slice: allow [T]::contains to accept any Q where T: Borrow<Q> Jul 3, 2017
@sunshowers
Copy link
Contributor Author

BTW, something similar can be done for binary_search too.

@sunshowers
Copy link
Contributor Author

Hmm, the error in https://travis-ci.org/rust-lang/rust/jobs/249466135 is interesting -- looks like maybe we should implement Borrow<&&T> for &T?

@sunshowers
Copy link
Contributor Author

One option here would be to have something like contains_borrow that takes in Borrow, and keep the existing contains the same.

The other option might be to implement contains both when T: Borrow<Q> and when Q: Borrow<T> (is this even correct, and can this be done through specialization?)

Thoughts?

@golddranks
Copy link
Contributor

I just bumped into this today. I've got a Vec<String>, and I'd like to use .contains("static string"). So a big thumbs up for this!

@@ -110,7 +110,7 @@ impl<'a, 'gcx, 'tcx> DefIdForest {
where I: IntoIterator<Item=DefIdForest>
{
let mut ret = DefIdForest::empty();
let mut next_ret = SmallVec::new();
let mut next_ret: SmallVec<[DefId; 1]> = SmallVec::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like an unrelated change? Unless inference is failing, as I suspect it is, in which case I'm not sure we'd be able to land this -- too much breakage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, inference was failing in this case.

@vitalyd
Copy link

vitalyd commented Jul 3, 2017

@golddranks , a workaround would be to use a Vec<Cow<'static, str>>; just an FYI if you've not thought of that already.

@golddranks
Copy link
Contributor

golddranks commented Jul 3, 2017

@vitalyd Unfortunately that's not an acceptable workaround if a Vec<String> is all you've got. A working workaround in this situation was to use .iter().any(|s| s == "static string").

@vitalyd
Copy link

vitalyd commented Jul 3, 2017

@golddranks , you mean if you don't have control over the Vec? Yeah, indeed the workaround isn't possible.

Your workaround using any over an iterator brings up the question of whether providing contains(Q) where T: Borrow<Q> is worth its weight. Given this is an arbitrary slice, a linear search is unavoidable in either case. So this is more about ergonomics, I guess. For containers that perform sub-linear lookups, like HashSet, this is more critical as there's a performance implication of course.

@aturon
Copy link
Member

aturon commented Jul 3, 2017

Note, this PR is closely related to:

The latter is a PR doing something similar for binary_search, which resulted in too much inference breakage for us to merge. I suspect that would also be the case here, though we could try a crater run if you like -- let me know!

@sunshowers
Copy link
Contributor Author

Yeah the inference breaking is unfortunate :( seems like this could have been fixed before 1.0, but it's too late now.

@aturon Do you think it's worth adding _borrow methods (or maybe contains2, binary_search2 etc, or maybe some other set of names) and deprecating the existing methods?

@golddranks
Copy link
Contributor

Maybe we should do that cargobomb run first before thinking the plan B?

@sunshowers
Copy link
Contributor Author

I'm fine with that. Note that inference wasn't the only thing failing -- https://travis-ci.org/rust-lang/rust/jobs/249466135 has an example of code that was accepted earlier (extra &) now failing.

@aturon aturon added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jul 3, 2017
@aturon
Copy link
Member

aturon commented Jul 3, 2017

@brson Flagging for crater run!

@jethrogb
Copy link
Contributor

jethrogb commented Jul 4, 2017

Why Borrow and not PartialEq?

@carols10cents
Copy link
Member

ping @alexcrichton, @brson, @tomprince, and @frewsxcv for a crater run!

@brson
Copy link
Contributor

brson commented Jul 11, 2017

Crater does not work any more. @tomprince has cargobomb running against PRs. Maybe he can do it. I will add this to my list.

@Mark-Simulacrum
Copy link
Member

Looking at the PR's travis log, this failed cargotest -- clap had inference failures. So I worry that we shouldn't do this. However, in order to run cargobomb, we'll need to get the artifacts; this may not work due to the cargotest failure but I guess we can try:

@bors try

@bors
Copy link
Contributor

bors commented Jul 11, 2017

🔒 Merge conflict

@brson
Copy link
Contributor

brson commented Jul 11, 2017

Looks like the merge conflict needs to be resolved, then the try run, then cargobomb.

@briansmith
Copy link
Contributor

briansmith commented Jul 12, 2017

I agree with @jethrogb that it seems to make more sense to use PartialEq instead of Borrow here, and just make the constrait looser. I think contains()'s semantics is by definition self.iter().any(|elem| *elem == *x) and the expression self.iter().any(|elem| *elem == *x) requires x: U where T: PartialEq<U>:

trait Contains<T> {
    fn contains<U>(&self, x: &U) -> bool where T: PartialEq<U>;
}

impl<T> Contains<T> for [T] {
    fn contains<U>(&self, x: &U) -> bool where T: PartialEq<U> {
        self.iter().any(|e| e == x)
    }
}

@aidanhs aidanhs added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jul 13, 2017
@brson
Copy link
Contributor

brson commented Jul 14, 2017

@bors try

@aidanhs
Copy link
Member

aidanhs commented Jul 20, 2017

@Sid0 could you take a look at rebasing?

In order to get artifacts, I believe we'll need to do alter the CI for this job to not build rustbook - I don't see how we'll get to the deploy stage otherwise. I don't know how to achieve that off the top of my head, so I'll look into it.

@alexcrichton
Copy link
Member

I'm going to close this out of inactivity, but please feel free to resubmit!

varkor added a commit to varkor/rust that referenced this pull request Dec 22, 2017
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).
bors added a commit that referenced this pull request Dec 27, 2017
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.