about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2014-10-24 19:23:22 -0500
committerJorge Aparicio <japaricious@gmail.com>2014-10-31 10:09:15 -0500
commitdd9dda7a1c87170fe241345f6da88874921ec433 (patch)
tree665d0d06c5827ab9ea2debff8799c198b9e61ed8
parent82045ca3606dfe070fce99ca71fb6a9d39217b73 (diff)
downloadrust-dd9dda7a1c87170fe241345f6da88874921ec433.tar.gz
rust-dd9dda7a1c87170fe241345f6da88874921ec433.zip
DSTify ToCStr
-rw-r--r--src/librustrt/c_str.rs44
-rw-r--r--src/libstd/path/posix.rs12
-rw-r--r--src/libstd/path/windows.rs12
3 files changed, 29 insertions, 39 deletions
diff --git a/src/librustrt/c_str.rs b/src/librustrt/c_str.rs
index ddb4df4fdc5..92d8e4005bf 100644
--- a/src/librustrt/c_str.rs
+++ b/src/librustrt/c_str.rs
@@ -74,7 +74,7 @@ fn main() {
 use collections::string::String;
 use collections::hash;
 use core::fmt;
-use core::kinds::marker;
+use core::kinds::{Sized, marker};
 use core::mem;
 use core::prelude::{Clone, Collection, Drop, Eq, ImmutableSlice, Iterator};
 use core::prelude::{MutableSlice, None, Option, Ordering, PartialEq};
@@ -286,7 +286,7 @@ impl fmt::Show for CString {
 }
 
 /// A generic trait for converting a value to a CString.
-pub trait ToCStr {
+pub trait ToCStr for Sized? {
     /// Copy the receiver into a CString.
     ///
     /// # Failure
@@ -329,15 +329,7 @@ pub trait ToCStr {
     }
 }
 
-// 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 ~str and String, so that we can
-// take ToCStr as owned. When DST lands, the string instances should
-// be revisited, and arguments bound by ToCStr should be passed by
-// reference.
-
-impl<'a> ToCStr for &'a str {
+impl ToCStr for str {
     #[inline]
     fn to_c_str(&self) -> CString {
         self.as_bytes().to_c_str()
@@ -384,10 +376,10 @@ impl ToCStr for String {
 // The length of the stack allocated buffer for `vec.with_c_str()`
 const BUF_LEN: uint = 128;
 
-impl<'a> ToCStr for &'a [u8] {
+impl ToCStr for [u8] {
     fn to_c_str(&self) -> CString {
         let mut cs = unsafe { self.to_c_str_unchecked() };
-        check_for_null(*self, cs.as_mut_ptr());
+        check_for_null(self, cs.as_mut_ptr());
         cs
     }
 
@@ -403,11 +395,33 @@ impl<'a> ToCStr for &'a [u8] {
     }
 
     fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
-        unsafe { with_c_str(*self, true, f) }
+        unsafe { with_c_str(self, true, f) }
+    }
+
+    unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
+        with_c_str(self, false, f)
+    }
+}
+
+impl<'a, Sized? T: ToCStr> ToCStr for &'a T {
+    #[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()
+    }
+
+    #[inline]
+    fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
+        (**self).with_c_str(f)
     }
 
+    #[inline]
     unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
-        with_c_str(*self, false, f)
+        (**self).with_c_str_unchecked(f)
     }
 }
 
diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs
index 0d7a467b313..8a50ecc5ca6 100644
--- a/src/libstd/path/posix.rs
+++ b/src/libstd/path/posix.rs
@@ -106,18 +106,6 @@ 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: hash::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 1ddc027a07e..e4bead6d8ab 100644
--- a/src/libstd/path/windows.rs
+++ b/src/libstd/path/windows.rs
@@ -130,18 +130,6 @@ 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: hash::Writer> hash::Hash<S> for Path {
     #[cfg(not(test))]
     #[inline]