Skip to content

Commit cc830ef

Browse files
committed
libstd: implement PartialEq<Path> for PathBuf and Cow<Path>
1 parent 7690ec8 commit cc830ef

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/libstd/path.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,29 @@ impl<'a> IntoIterator for &'a Path {
19091909
fn into_iter(self) -> Iter<'a> { self.iter() }
19101910
}
19111911

1912+
macro_rules! impl_eq {
1913+
($lhs:ty, $rhs: ty) => {
1914+
#[stable(feature = "partialeq_path", since = "1.6.0")]
1915+
impl<'a, 'b> PartialEq<$rhs> for $lhs {
1916+
#[inline]
1917+
fn eq(&self, other: &$rhs) -> bool { <Path as PartialEq>::eq(self, other) }
1918+
}
1919+
1920+
#[stable(feature = "partialeq_path", since = "1.6.0")]
1921+
impl<'a, 'b> PartialEq<$lhs> for $rhs {
1922+
#[inline]
1923+
fn eq(&self, other: &$lhs) -> bool { <Path as PartialEq>::eq(self, other) }
1924+
}
1925+
1926+
}
1927+
}
1928+
1929+
impl_eq!(PathBuf, Path);
1930+
impl_eq!(PathBuf, &'a Path);
1931+
impl_eq!(Cow<'a, Path>, Path);
1932+
impl_eq!(Cow<'a, Path>, &'b Path);
1933+
impl_eq!(Cow<'a, Path>, PathBuf);
1934+
19121935
#[cfg(test)]
19131936
mod tests {
19141937
use super::*;
@@ -3106,6 +3129,31 @@ mod tests {
31063129
tfe!("/", "foo", "/", false);
31073130
}
31083131

3132+
#[test]
3133+
fn test_eq_recievers() {
3134+
use borrow::Cow;
3135+
3136+
let borrowed: &Path = Path::new("foo/bar");
3137+
let mut owned: PathBuf = PathBuf::new();
3138+
owned.push("foo");
3139+
owned.push("bar");
3140+
let borrowed_cow: Cow<Path> = borrowed.into();
3141+
let owned_cow: Cow<Path> = owned.clone().into();
3142+
3143+
macro_rules! t {
3144+
($($current:expr),+) => {
3145+
$(
3146+
assert_eq!($current, borrowed);
3147+
assert_eq!($current, owned);
3148+
assert_eq!($current, borrowed_cow);
3149+
assert_eq!($current, owned_cow);
3150+
)+
3151+
}
3152+
}
3153+
3154+
t!(borrowed, owned, borrowed_cow, owned_cow);
3155+
}
3156+
31093157
#[test]
31103158
pub fn test_compare() {
31113159
use hash::{Hash, Hasher, SipHasher};

0 commit comments

Comments
 (0)