8000 Auto merge of #140956 - Kixunil:impl-partialeq-str-for-path, r=<try> · rust-lang/rust@afeffcd · GitHub
[go: up one dir, main page]

Skip to content
  • Pricing
  • Search code, repositories, users, issues, pull requests...

    Provide feedback

    We read every piece of feedback, and take your input very seriously.

    Saved searches

    Use saved searches to filter your results more quickly

    Appearance settings

    Commit afeffcd

    Browse files
    committed
    Auto merge of #140956 - Kixunil:impl-partialeq-str-for-path, r=<try>
    `impl PartialEq<{str,String}> for {Path,PathBuf}` This is a revival of #105877 Comparison of paths and strings is expected to be possible and needed e.g. in tests. This change adds the impls os `PartialEq` between strings and paths, both owned and unsized, in both directions. ACP: rust-lang/libs-team#151
    2 parents 4455c89 + aab1563 commit afeffcd

    File tree

    3 files changed

    +71
    -8
    lines changed

    3 files changed

    +71
    -8
    lines changed

    library/std/src/path.rs

    Lines changed: 65 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2068,6 +2068,38 @@ impl PartialEq for PathBuf {
    20682068
    }
    20692069
    }
    20702070

    2071+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    2072+
    impl cmp::PartialEq<str> for PathBuf {
    2073+
    #[inline]
    2074+
    fn eq(&self, other: &str) -> bool {
    2075+
    &*self == other
    2076+
    }
    2077+
    }
    2078+
    2079+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    2080+
    impl cmp::PartialEq<PathBuf> for str {
    2081+
    #[inline]
    2082+
    fn eq(&self, other: &PathBuf) -> bool {
    2083+
    other == self
    2084+
    }
    2085+
    }
    2086+
    2087+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    2088+
    impl cmp::PartialEq<String> for PathBuf {
    2089+
    #[inline]
    2090+
    fn eq(&self, other: &String) -> bool {
    2091+
    **self == **other
    2092+
    }
    2093+
    }
    2094+
    2095+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    2096+
    impl cmp::PartialEq<PathBuf> for String {
    2097+
    #[inline]
    2098+
    fn eq(&self, other: &PathBuf) -> bool {
    2099+
    other == self
    2100+
    }
    2101+
    }
    2102+
    20712103
    #[stable(feature = "rust1", since = "1.0.0")]
    20722104
    impl Hash for PathBuf {
    20732105
    fn hash<H: Hasher>(&self, h: &mut H) {
    @@ -3242,6 +3274,39 @@ impl PartialEq for Path {
    32423274
    }
    32433275
    }
    32443276

    3277+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    3278+
    impl cmp::PartialEq<str> for Path {
    3279+
    #[inline]
    3280+
    fn eq(&self, other: &str) -> bool {
    3281+
    let other: &OsStr = other.as_ref();
    3282+
    self == other
    3283+
    }
    3284+
    }
    3285+
    3286+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    3287+
    impl cmp::PartialEq<Path> for str {
    3288+
    #[inline]
    3289+
    fn eq(&self, other: &Path) -> bool {
    3290+
    other == self
    3291+
    }
    3292+
    }
    3293+
    3294+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    3295+
    impl cmp::PartialEq<String> for Path {
    3296+
    #[inline]
    3297+
    fn eq(&self, other: &String) -> bool {
    3298+
    self == &*other
    3299+
    }
    3300+
    }
    3301+
    3302+
    #[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
    3303+
    impl cmp::PartialEq<Path> for String {
    3304+
    #[inline]
    3305+
    fn eq(&self, other: &Path) -> bool {
    3306+
    other == self
    3307+
    }
    3308+
    }
    3309+
    32453310
    #[stable(feature = "rust1", since = "1.0.0")]
    32463311
    impl Hash for Path {
    32473312
    fn hash<H: Hasher>(&self, h: &mut H) {

    tests/ui/inference/issue-72616.stderr

    Lines changed: 4 additions & 8 deletions
    Original file line numberDiff line numberDiff line change
    @@ -6,14 +6,10 @@ LL | if String::from("a") == "a".try_into().unwrap() {}
    66
    | |
    77
    | type must be known at this point
    88
    |
    9-
    = note: cannot satisfy `String: PartialEq<_>`
    10-
    = help: the following types implement trait `PartialEq<Rhs>`:
    11-
    `String` implements `PartialEq<&str>`
    12-
    `String` implements `PartialEq<ByteStr>`
    13-
    `String` implements `PartialEq<ByteString>`
    14-
    `String` implements `PartialEq<Cow<'_, str>>`
    15-
    `String` implements `PartialEq<str>`
    16-
    `String` implements `PartialEq`
    9+
    = note: multiple `impl`s satisfying `String: PartialEq<_>` found in the following crates: `alloc`, `std`:
    10+
    - impl PartialEq for String;
    11+
    - impl PartialEq<Path> for String;
    12+
    - impl PartialEq<PathBuf> for String;
    1713
    help: try using a fully qualified path to specify the expected types
    1814
    |
    1915
    LL - if String::from("a") == "a".try_into().unwrap() {}

    tests/ui/suggestions/partialeq_suggest_swap_on_e0277.stderr

    Lines changed: 2 additions & 0 deletions
    38B5
    Original file line numberDiff line numberDiff line change
    @@ -10,6 +10,8 @@ LL | String::from("Girls Band Cry") == T(String::from("Girls Band Cry"));
    1010
    `String` implements `PartialEq<ByteStr>`
    1111
    `String` implements `PartialEq<ByteString>`
    1212
    `String` implements `PartialEq<Cow<'_, str>>`
    13+
    `String` implements `PartialEq<Path>`
    14+
    `String` implements `PartialEq<PathBuf>`
    1315
    `String` implements `PartialEq<str>`
    1416
    `String` implements `PartialEq`
    1517
    = note: `T` implements `PartialEq<String>`

    0 commit comments

    Comments
     (0)
    0