Skip to content

Commit c7d5b49

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 8405332 commit c7d5b49

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

library/std/src/path.rs

+65
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) {

0 commit comments

Comments
 (0)