From 7690ec89ffeac4b4537e71c980d65ead9177d76c Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Tue, 3 Nov 2015 04:26:45 +0000 Subject: [PATCH 1/3] libstd: implement From<&Path|PathBuf> for Cow --- src/libstd/path.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 0559849f6a6..b3f755a60a5 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1166,6 +1166,22 @@ impl<'a> IntoCow<'a, Path> for &'a Path { } } +#[stable(feature = "cow_from_path", since = "1.6.0")] +impl<'a> From<&'a Path> for Cow<'a, Path> { + #[inline] + fn from(s: &'a Path) -> Cow<'a, Path> { + Cow::Borrowed(s) + } +} + +#[stable(feature = "cow_from_path", since = "1.6.0")] +impl<'a> From for Cow<'a, Path> { + #[inline] + fn from(s: PathBuf) -> Cow<'a, Path> { + Cow::Owned(s) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl ToOwned for Path { type Owned = PathBuf; @@ -2002,6 +2018,26 @@ mod tests { assert_eq!(static_cow_path, owned_cow_path); } + #[test] + fn into() { + use borrow::Cow; + + let static_path = Path::new("/home/foo"); + let static_cow_path: Cow<'static, Path> = static_path.into(); + let pathbuf = PathBuf::from("/home/foo"); + + { + let path: &Path = &pathbuf; + let borrowed_cow_path: Cow = path.into(); + + assert_eq!(static_cow_path, borrowed_cow_path); + } + + let owned_cow_path: Cow<'static, Path> = pathbuf.into(); + + assert_eq!(static_cow_path, owned_cow_path); + } + #[test] #[cfg(unix)] pub fn test_decompositions_unix() { From cc830ef18b3014158715f0fd5635a179c5bd7844 Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Tue, 3 Nov 2015 04:51:21 +0000 Subject: [PATCH 2/3] libstd: implement PartialEq for PathBuf and Cow --- src/libstd/path.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index b3f755a60a5..9a5852663cb 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1909,6 +1909,29 @@ impl<'a> IntoIterator for &'a Path { fn into_iter(self) -> Iter<'a> { self.iter() } } +macro_rules! impl_eq { + ($lhs:ty, $rhs: ty) => { + #[stable(feature = "partialeq_path", since = "1.6.0")] + impl<'a, 'b> PartialEq<$rhs> for $lhs { + #[inline] + fn eq(&self, other: &$rhs) -> bool { ::eq(self, other) } + } + + #[stable(feature = "partialeq_path", since = "1.6.0")] + impl<'a, 'b> PartialEq<$lhs> for $rhs { + #[inline] + fn eq(&self, other: &$lhs) -> bool { ::eq(self, other) } + } + + } +} + +impl_eq!(PathBuf, Path); +impl_eq!(PathBuf, &'a Path); +impl_eq!(Cow<'a, Path>, Path); +impl_eq!(Cow<'a, Path>, &'b Path); +impl_eq!(Cow<'a, Path>, PathBuf); + #[cfg(test)] mod tests { use super::*; @@ -3106,6 +3129,31 @@ mod tests { tfe!("/", "foo", "/", false); } + #[test] + fn test_eq_recievers() { + use borrow::Cow; + + let borrowed: &Path = Path::new("foo/bar"); + let mut owned: PathBuf = PathBuf::new(); + owned.push("foo"); + owned.push("bar"); + let borrowed_cow: Cow = borrowed.into(); + let owned_cow: Cow = owned.clone().into(); + + macro_rules! t { + ($($current:expr),+) => { + $( + assert_eq!($current, borrowed); + assert_eq!($current, owned); + assert_eq!($current, borrowed_cow); + assert_eq!($current, owned_cow); + )+ + } + } + + t!(borrowed, owned, borrowed_cow, owned_cow); + } + #[test] pub fn test_compare() { use hash::{Hash, Hasher, SipHasher}; From f57621535efc5b68d883ef51dc3b324e9ab3d83f Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Tue, 3 Nov 2015 04:54:32 +0000 Subject: [PATCH 3/3] libcollections: DRY up a PartialEq impl for String --- src/libcollections/string.rs | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 96a28d3ee3b..8d07e2b2018 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -963,7 +963,7 @@ impl PartialEq for String { macro_rules! impl_eq { ($lhs:ty, $rhs: ty) => { #[stable(feature = "rust1", since = "1.0.0")] - impl<'a> PartialEq<$rhs> for $lhs { + impl<'a, 'b> PartialEq<$rhs> for $lhs { #[inline] fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) } #[inline] @@ -971,7 +971,7 @@ macro_rules! impl_eq { } #[stable(feature = "rust1", since = "1.0.0")] - impl<'a> PartialEq<$lhs> for $rhs { + impl<'a, 'b> PartialEq<$lhs> for $rhs { #[inline] fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) } #[inline] @@ -984,24 +984,9 @@ macro_rules! impl_eq { impl_eq! { String, str } impl_eq! { String, &'a str } impl_eq! { Cow<'a, str>, str } +impl_eq! { Cow<'a, str>, &'b str } impl_eq! { Cow<'a, str>, String } -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> { - #[inline] - fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&self[..], &other[..]) } - #[inline] - fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&self[..], &other[..]) } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b> PartialEq> for &'b str { - #[inline] - fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&self[..], &other[..]) } - #[inline] - fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&self[..], &other[..]) } -} - #[stable(feature = "rust1", since = "1.0.0")] impl Default for String { #[inline]