about summary refs log tree commit diff
path: root/src/libstd/path
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/path')
-rw-r--r--src/libstd/path/posix.rs21
-rw-r--r--src/libstd/path/windows.rs23
2 files changed, 41 insertions, 3 deletions
diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs
index 99a281755e4..4f7132dc6e4 100644
--- a/src/libstd/path/posix.rs
+++ b/src/libstd/path/posix.rs
@@ -79,11 +79,18 @@ impl FromStr for Path {
     }
 }
 
+// FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
+// we cannot usefully take ToCStr arguments by reference (without forcing an
+// additional & around &str). So we are instead temporarily adding an instance
+// for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
+// instance should be removed, and arguments bound by ToCStr should be passed by
+// reference.
+
 impl ToCStr for Path {
     #[inline]
     fn to_c_str(&self) -> CString {
         // The Path impl guarantees no internal NUL
-        unsafe { self.as_vec().to_c_str_unchecked() }
+        unsafe { self.to_c_str_unchecked() }
     }
 
     #[inline]
@@ -92,6 +99,18 @@ impl ToCStr for Path {
     }
 }
 
+impl<'a> ToCStr for &'a Path {
+    #[inline]
+    fn to_c_str(&self) -> CString {
+        (*self).to_c_str()
+    }
+
+    #[inline]
+    unsafe fn to_c_str_unchecked(&self) -> CString {
+        (*self).to_c_str_unchecked()
+    }
+}
+
 impl<S: Writer> ::hash::Hash<S> for Path {
     #[inline]
     fn hash(&self, state: &mut S) {
diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs
index dfe7241d7af..176788edcc4 100644
--- a/src/libstd/path/windows.rs
+++ b/src/libstd/path/windows.rs
@@ -103,11 +103,18 @@ impl FromStr for Path {
     }
 }
 
+// FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
+// we cannot usefully take ToCStr arguments by reference (without forcing an
+// additional & around &str). So we are instead temporarily adding an instance
+// for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
+// instance should be removed, and arguments bound by ToCStr should be passed by
+// reference.
+
 impl ToCStr for Path {
     #[inline]
     fn to_c_str(&self) -> CString {
-        // The Path impl guarantees no embedded NULs
-        unsafe { self.as_vec().to_c_str_unchecked() }
+        // The Path impl guarantees no internal NUL
+        unsafe { self.to_c_str_unchecked() }
     }
 
     #[inline]
@@ -116,6 +123,18 @@ impl ToCStr for Path {
     }
 }
 
+impl<'a> ToCStr for &'a Path {
+    #[inline]
+    fn to_c_str(&self) -> CString {
+        (*self).to_c_str()
+    }
+
+    #[inline]
+    unsafe fn to_c_str_unchecked(&self) -> CString {
+        (*self).to_c_str_unchecked()
+    }
+}
+
 impl<S: Writer> ::hash::Hash<S> for Path {
     #[inline]
     fn hash(&self, state: &mut S) {