Skip to content

slice::contains with borrowed data #62367

Open
@RalfJung

Description

@RalfJung
Member

HashSet::contains has the following type to allow e.g. searching in a HashSet<String> with an &str:

pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where
    T: Borrow<Q>,
    Q: Hash + Eq, 

However, slice::contains does not use Borrow, so to search in an &[String] one has to actually allocate a String:

pub fn contains(&self, x: &T) -> bool where
    T: PartialEq<T>, 

Is there a fundamental reason for this, or is this just an omission?

Activity

Mark-Simulacrum

Mark-Simulacrum commented on Jul 4, 2019

@Mark-Simulacrum
Member

I'm fairly certain that this was essentially just a mistake when we implemented it, but we can't change it now as it causes inference failures across the ecosystem. I believe it's been tried multiple times and crater runs are disappointing. It's possible the inference for U: PartialEq or the reverse would also be worse.

RalfJung

RalfJung commented on Jul 4, 2019

@RalfJung
MemberAuthor

I feared that might be the case. :/

Would it be reasonable to add a contains_bor or so? It's a kludge, but seems better than having to allocate.

Mark-Simulacrum

Mark-Simulacrum commented on Jul 4, 2019

@Mark-Simulacrum
Member

Probably not worth it -- if you want to skip allocating .iter().any(|e| e == foo) is equivalent to contains.

RalfJung

RalfJung commented on Jul 4, 2019

@RalfJung
MemberAuthor

Looking at the SliceContains specialization, I am not sure if that's equally efficient for all types?

At the least, the docs should call out this alternative then.

Mark-Simulacrum

Mark-Simulacrum commented on Jul 4, 2019

@Mark-Simulacrum
Member

Hm, yeah, that might be slower for some cases. I forgot we had specialization internally to optimize for some types. Ideally we'd get around the inference failures somehow, e.g., with a default type param, but that seems unlikely to happen soon :/

added a commit that references this issue on Jul 22, 2019

Rollup merge of rust-lang#62656 - RalfJung:contains-no-own, r=Dylan-DPC

966ba8d
added a commit that references this issue on Jul 23, 2019

Rollup merge of rust-lang#62656 - RalfJung:contains-no-own, r=Dylan-DPC

4264f83
added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
on Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RalfJung@Centril@Mark-Simulacrum

        Issue actions

          slice::contains with borrowed data · Issue #62367 · rust-lang/rust