about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-03 19:29:44 +0000
committerbors <bors@rust-lang.org>2015-11-03 19:29:44 +0000
commit2a7bd082ac8443dc74628b9b21686b84b7440d6b (patch)
treeb92e07bf88f1267f0d482da72d114ba769f694ce /src/libstd
parentde11d2aa835ff6bbb0a8b6fcef1ff0a1174418b3 (diff)
parentf57621535efc5b68d883ef51dc3b324e9ab3d83f (diff)
downloadrust-2a7bd082ac8443dc74628b9b21686b84b7440d6b.tar.gz
rust-2a7bd082ac8443dc74628b9b21686b84b7440d6b.zip
Auto merge of #29532 - Ryman:cow_path, r=alexcrichton
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/path.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 0559849f6a6..9a5852663cb 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<PathBuf> 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;
@@ -1893,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 { <Path as PartialEq>::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 { <Path as PartialEq>::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::*;
@@ -2003,6 +2042,26 @@ mod tests {
     }
 
     #[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> = 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() {
         t!("",
@@ -3071,6 +3130,31 @@ mod tests {
     }
 
     #[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<Path> = borrowed.into();
+        let owned_cow: Cow<Path> = 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};