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

Projects
None yet
@sunshowers
Copy link

sunshowers commented Jul 3, 2017

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

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Jul 3, 2017

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.

slice: allow [T]::contains to accept any Q where T: Borrow<Q>
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 force-pushed the sunshowers:borrow branch from ef44821 to f3eceba Jul 3, 2017

@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

This comment has been minimized.

Copy link
Author

sunshowers commented Jul 3, 2017

BTW, something similar can be done for binary_search too.

@sunshowers

This comment has been minimized.

Copy link
Author

sunshowers commented Jul 3, 2017

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

This comment has been minimized.

Copy link
Author

sunshowers commented Jul 3, 2017

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

This comment has been minimized.

Copy link
Contributor

golddranks commented Jul 3, 2017

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();

This comment has been minimized.

@Mark-Simulacrum

Mark-Simulacrum Jul 3, 2017

Member

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.

This comment has been minimized.

@sunshowers

sunshowers Jul 3, 2017

Author

Yeah, inference was failing in this case.

@vitalyd

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Author

sunshowers commented Jul 3, 2017

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

This comment has been minimized.

Copy link
Contributor

golddranks commented Jul 3, 2017

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

@sunshowers

This comment has been minimized.

Copy link
Author

sunshowers commented Jul 3, 2017

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

This comment has been minimized.

Copy link
Member

aturon commented Jul 3, 2017

@brson Flagging for crater run!

@jethrogb

This comment has been minimized.

Copy link
Contributor

jethrogb commented Jul 4, 2017

Why Borrow and not PartialEq?

@carols10cents

This comment has been minimized.

Copy link
Member

carols10cents commented Jul 10, 2017

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

@brson

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented Jul 11, 2017

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

This comment has been minimized.

Copy link
Contributor

bors commented Jul 11, 2017

🔒 Merge conflict

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jul 11, 2017

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

@briansmith

This comment has been minimized.

Copy link

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)
    }
}
@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jul 14, 2017

@bors try

@aidanhs

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

alexcrichton commented Jul 27, 2017

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

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 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

Auto merge of #46934 - varkor:slice-contains-generic, r=<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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.