Skip to content

Commit 4813457

Browse files
committed
impl PartialEq<{str,String}> for {Path,PathBuf}
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
1 parent d5a7ddd commit 4813457

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

library/std/src/path.rs

+65
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,38 @@ impl cmp::PartialEq for PathBuf {
18851885
}
18861886
}
18871887

1888+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
1889+
impl cmp::PartialEq<str> for PathBuf {
1890+
#[inline]
1891+
fn eq(&self, other: &str) -> bool {
1892+
&*self == other
1893+
}
1894+
}
1895+
1896+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
1897+
impl cmp::PartialEq<PathBuf> for str {
1898+
#[inline]
1899+
fn eq(&self, other: &PathBuf) -> bool {
1900+
other == self
1901+
}
1902+
}
1903+
1904+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
1905+
impl cmp::PartialEq<String> for PathBuf {
1906+
#[inline]
1907+
fn eq(&self, other: &String) -> bool {
1908+
**self == **other
1909+
}
1910+
}
1911+
1912+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
1913+
impl cmp::PartialEq<PathBuf> for String {
1914+
#[inline]
1915+
fn eq(&self, other: &PathBuf) -> bool {
1916+
other == self
1917+
}
1918+
}
1919+
18881920
#[stable(feature = "rust1", since = "1.0.0")]
18891921
impl Hash for PathBuf {
18901922
fn hash<H: Hasher>(&self, h: &mut H) {
@@ -3003,6 +3035,39 @@ impl cmp::PartialEq for Path {
30033035
}
30043036
}
30053037

3038+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
3039+
impl cmp::PartialEq<str> for Path {
3040+
#[inline]
3041+
fn eq(&self, other: &str) -> bool {
3042+
let other: &OsStr = other.as_ref();
3043+
self == other
3044+
}
3045+
}
3046+
3047+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
3048+
impl cmp::PartialEq<Path> for str {
3049+
#[inline]
3050+
fn eq(&self, other: &Path) -> bool {
3051+
other == self
3052+
}
3053+
}
3054+
3055+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
3056+
impl cmp::PartialEq<String> for Path {
3057+
#[inline]
3058+
fn eq(&self, other: &String) -> bool {
3059+
self == &*other
3060+
}
3061+
}
3062+
3063+
#[stable(feature = "eq-str-for-path", since = "CURRENT_RUSTC_VERSION")]
3064+
impl cmp::PartialEq<Path> for String {
3065+
#[inline]
3066+
fn eq(&self, other: &Path) -> bool {
3067+
other == self
3068+
}
3069+
}
3070+
30063071
#[stable(feature = "rust1", since = "1.0.0")]
30073072
impl Hash for Path {
30083073
fn hash<H: Hasher>(&self, h: &mut H) {

src/test/ui/inference/issue-72616.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ LL | if String::from("a") == "a".try_into().unwrap() {}
66
| |
77
| type must be known at this point
88
|
9-
= note: multiple `impl`s satisfying `String: PartialEq<_>` found in the `alloc` crate:
9+
= note: multiple `impl`s satisfying `String: PartialEq<_>` found in the following crates: `alloc`, `std`:
1010
- impl PartialEq for String;
11+
- impl PartialEq<Path> for String;
12+
- impl PartialEq<PathBuf> for String;
1113
- impl<'a, 'b> PartialEq<&'a str> for String;
1214
- impl<'a, 'b> PartialEq<Cow<'a, str>> for String;
1315
- impl<'a, 'b> PartialEq<str> for String;

0 commit comments

Comments
 (0)