Skip to content

Commit 2a7bd08

Browse files
committed
Auto merge of #29532 - Ryman:cow_path, r=alexcrichton
2 parents de11d2a + f576215 commit 2a7bd08

File tree

2 files changed

+87
-18
lines changed

2 files changed

+87
-18
lines changed

src/libcollections/string.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -963,15 +963,15 @@ impl PartialEq for String {
963963
macro_rules! impl_eq {
964964
($lhs:ty, $rhs: ty) => {
965965
#[stable(feature = "rust1", since = "1.0.0")]
966-
impl<'a> PartialEq<$rhs> for $lhs {
966+
impl<'a, 'b> PartialEq<$rhs> for $lhs {
967967
#[inline]
968968
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
969969
#[inline]
970970
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
971971
}
972972

973973
#[stable(feature = "rust1", since = "1.0.0")]
974-
impl<'a> PartialEq<$lhs> for $rhs {
974+
impl<'a, 'b> PartialEq<$lhs> for $rhs {
975975
#[inline]
976976
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
977977
#[inline]
@@ -984,24 +984,9 @@ macro_rules! impl_eq {
984984
impl_eq! { String, str }
985985
impl_eq! { String, &'a str }
986986
impl_eq! { Cow<'a, str>, str }
987+
impl_eq! { Cow<'a, str>, &'b str }
987988
impl_eq! { Cow<'a, str>, String }
988989

989-
#[stable(feature = "rust1", since = "1.0.0")]
990-
impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> {
991-
#[inline]
992-
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&self[..], &other[..]) }
993-
#[inline]
994-
fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&self[..], &other[..]) }
995-
}
996-
997-
#[stable(feature = "rust1", since = "1.0.0")]
998-
impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
999-
#[inline]
1000-
fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&self[..], &other[..]) }
1001-
#[inline]
1002-
fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&self[..], &other[..]) }
1003-
}
1004-
1005990
#[stable(feature = "rust1", since = "1.0.0")]
1006991
impl Default for String {
1007992
#[inline]

src/libstd/path.rs

+84
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,22 @@ impl<'a> IntoCow<'a, Path> for &'a Path {
11661166
}
11671167
}
11681168

1169+
#[stable(feature = "cow_from_path", since = "1.6.0")]
1170+
impl<'a> From<&'a Path> for Cow<'a, Path> {
1171+
#[inline]
1172+
fn from(s: &'a Path) -> Cow<'a, Path> {
1173+
Cow::Borrowed(s)
1174+
}
1175+
}
1176+
1177+
#[stable(feature = "cow_from_path", since = "1.6.0")]
1178+
impl<'a> From<PathBuf> for Cow<'a, Path> {
1179+
#[inline]
1180+
fn from(s: PathBuf) -> Cow<'a, Path> {
1181+
Cow::Owned(s)
1182+
}
1183+
}
1184+
11691185
#[stable(feature = "rust1", since = "1.0.0")]
11701186
impl ToOwned for Path {
11711187
type Owned = PathBuf;
@@ -1893,6 +1909,29 @@ impl<'a> IntoIterator for &'a Path {
18931909
fn into_iter(self) -> Iter<'a> { self.iter() }
18941910
}
18951911

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+
18961935
#[cfg(test)]
18971936
mod tests {
18981937
use super::*;
@@ -2002,6 +2041,26 @@ mod tests {
20022041
assert_eq!(static_cow_path, owned_cow_path);
20032042
}
20042043

2044+
#[test]
2045+
fn into() {
2046+
use borrow::Cow;
2047+
2048+
let static_path = Path::new("/home/foo");
2049+
let static_cow_path: Cow<'static, Path> = static_path.into();
2050+
let pathbuf = PathBuf::from("/home/foo");
2051+
2052+
{
2053+
let path: &Path = &pathbuf;
2054+
let borrowed_cow_path: Cow<Path> = path.into();
2055+
2056+
assert_eq!(static_cow_path, borrowed_cow_path);
2057+
}
2058+
2059+
let owned_cow_path: Cow<'static, Path> = pathbuf.into();
2060+
2061+
assert_eq!(static_cow_path, owned_cow_path);
2062+
}
2063+
20052064
#[test]
20062065
#[cfg(unix)]
20072066
pub fn test_decompositions_unix() {
@@ -3070,6 +3129,31 @@ mod tests {
30703129
tfe!("/", "foo", "/", false);
30713130
}
30723131

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+
30733157
#[test]
30743158
pub fn test_compare() {
30753159
use hash::{Hash, Hasher, SipHasher};

0 commit comments

Comments
 (0)