Skip to content

Commit ddd7bcc

Browse files
authored
Rollup merge of rust-lang#140956 - Kixunil:impl-partialeq-str-for-path, r=Amanieu
`impl PartialEq<{str,String}> for {Path,PathBuf}` This is a revival of rust-lang#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 425a9c0 + aab1563 commit ddd7bcc

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
@@ -2105,6 +2105,38 @@ impl PartialEq for PathBuf {
21052105
}
21062106
}
21072107

2108+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
2109+
impl cmp::PartialEq<str> for PathBuf {
2110+
#[inline]
2111+
fn eq(&self, other: &str) -> bool {
2112+
&*self == other
2113+
}
2114+
}
2115+
2116+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
2117+
impl cmp::PartialEq<PathBuf> for str {
2118+
#[inline]
2119+
fn eq(&self, other: &PathBuf) -> bool {
2120+
other == self
2121+
}
2122+
}
2123+
2124+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
2125+
impl cmp::PartialEq<String> for PathBuf {
2126+
#[inline]
2127+
fn eq(&self, other: &String) -> bool {
2128+
**self == **other
2129+
}
2130+
}
2131+
2132+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
2133+
impl cmp::PartialEq<PathBuf> for String {
2134+
#[inline]
2135+
fn eq(&self, other: &PathBuf) -> bool {
2136+
other == self
2137+
}
2138+
}
2139+
21082140
#[stable(feature = "rust1", since = "1.0.0")]
21092141
impl Hash for PathBuf {
21102142
fn hash<H: Hasher>(&self, h: &mut H) {
@@ -3366,6 +3398,39 @@ impl PartialEq for Path {
33663398
}
33673399
}
33683400

3401+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
3402+
impl cmp::PartialEq<str> for Path {
3403+
#[inline]
3404+
fn eq(&self, other: &str) -> bool {
3405+
let other: &OsStr = other.as_ref();
3406+
self == other
3407+
}
3408+
}
3409+
3410+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
3411+
impl cmp::PartialEq<Path> for str {
3412+
#[inline]
3413+
fn eq(&self, other: &Path) -> bool {
3414+
other == self
3415+
}
3416+
}
3417+
3418+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
3419+
impl cmp::PartialEq<String> for Path {
3420+
#[inline]
3421+
fn eq(&self, other: &String) -> bool {
3422+
self == &*other
3423+
}
3424+
}
3425+
3426+
#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
3427+
impl cmp::PartialEq<Path> for String {
3428+
#[inline]
3429+
fn eq(&self, other: &Path) -> bool {
3430+
other == self
3431+
}
3432+
}
3433+
33693434
#[stable(feature = "rust1", since = "1.0.0")]
33703435
impl Hash for Path {
33713436
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
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)