Skip to content

Tracking issue for Option::contains and Result::contains #62358

Closed

Description

@Centril

This is a tracking issue for Option::contains and Result::contains.

The implementations are as follows:

    pub fn contains(&self, x: &T) -> bool where T: PartialEq {
        match self {
            Some(y) => y == x,
            None => false,
        }
    }

    pub fn contains(&self, x: &T) -> bool where T: PartialEq {
        match self {
            Ok(y) => y == x,
            Err(_) => false,
        }
    }
``

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jul 4, 2019
  2. Mark-Simulacrum commented on Jul 4, 2019

    @Mark-Simulacrum
    Member

    Should we update the signatures to fn contains<U>(&self, x: &U) -> bool where T: PartialEq<U>?

  3. Centril commented on Jul 4, 2019

    @Centril
    ContributorAuthor

    @Mark-Simulacrum That seems like a good idea -- type inference should be driven by x in any case. Let's do that in the PR.

  4. timvermeulen commented on Jul 4, 2019

    @timvermeulen
    Contributor

    Looks like slice/Vec/VecDeque/LinkedList and possibly others have a contains method that could benefit in the same way.

  5. soc commented on Jul 4, 2019

    @soc
    Contributor

    @Mark-Simulacrum I didn't do that because I checked the collections first, and they were all invariant. Shall I change this for Option and Result only?

  6. Mark-Simulacrum commented on Jul 4, 2019

    @Mark-Simulacrum
    Member

    We usually can't change preexisting APIs due to inference breakage, but since this is new, we should be able to be general.

  7. frewsxcv commented on Jul 6, 2019

    @frewsxcv
    Contributor

    Are these functionally equivalent?

        pub fn contains(&self, x: &T) -> bool where T: PartialEq {
            match self {
                Some(y) => y == x,
                None => false,
            }
        }
        pub fn contains(&self, x: &T) -> bool where T: PartialEq {
            self.as_ref() == Some(x)
        }
  8. soc commented on Jul 8, 2019

    @soc
    Contributor

    @frewsxcv Not quite, due to the requested variance changes.

  9. czipperz commented on Jul 14, 2019

    @czipperz
    Contributor

    What does that mean @soc?

  10. soc commented on Jul 14, 2019

    @soc
    Contributor

    @czipperz The implementation is:

    // Option
    pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> {
        match self {
            Some(y) => x == y,
            None => false,
        }
    }
    
    // Result
    pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> {
        match self {
            Ok(y) => x == y,
            Err(_) => false
        }
    }
    
    pub fn contains_err<F>(&self, f: &F) -> bool where F: PartialEq<E> {
        match self {
            Ok(_) => false,
            Err(e) => f == e
        }
    }
    

    The argument doesn't have to be of the same type as the value inside option/result, only that it knows how to compare itself to it. I. e. the operation is not invariant on the contained type, it allows an arbitrary type.

  11. czipperz commented on Jul 14, 2019

    @czipperz
    Contributor

    So there's no implementation of PartialEq<Option<U>> for Option<T> where U: PartialEq<T>?

  12. timvermeulen commented on Jul 14, 2019

    @timvermeulen
    Contributor

    There is indeed not, see #20063.

  13. 150 remaining items

  14. added a commit that references this issue on Mar 28, 2023
  15. soc commented on Mar 29, 2023

    @soc
    Contributor

    @Centril methods have been removed, this issue can be closed now.

  16. univerz commented on Apr 19, 2023

    @univerz

    i tried to remove .contains(&y), but rewriting it to .as_ref().is_some_and(|x| x == &y) annoyed me so much i had to re/introduce Contains trait into my stdx.rs file

  17. bjorn commented on Apr 19, 2023

    @bjorn

    @univerz As per above commit one could also replace by iter().any(...), but it is only a little bit shorter so not any less annoying. I'm sad about the outcome of this issue. :-(

  18. soc commented on Apr 19, 2023

    @soc
    Contributor

    @univerz Perhaps option-ext can be of use to you!

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

    A-result-optionArea: Result and Option combinatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-api[DEPRECATED; DO NOT USE]disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions