@@ -74,7 +74,7 @@ fn main() {
7474use collections:: string:: String ;
7575use collections:: hash;
7676use core:: fmt;
77- use core:: kinds:: marker;
77+ use core:: kinds:: { Sized , marker} ;
7878use core:: mem;
7979use core:: prelude:: { Clone , Collection , Drop , Eq , ImmutableSlice , Iterator } ;
8080use core:: prelude:: { MutableSlice , None , Option , Ordering , PartialEq } ;
@@ -286,7 +286,7 @@ impl fmt::Show for CString {
286286}
287287
288288/// A generic trait for converting a value to a CString.
289- pub trait ToCStr {
289+ pub trait ToCStr for Sized ? {
290290 /// Copy the receiver into a CString.
291291 ///
292292 /// # Failure
@@ -329,15 +329,7 @@ pub trait ToCStr {
329329 }
330330}
331331
332- // FIXME (#12938): Until DST lands, we cannot decompose &str into &
333- // and str, so we cannot usefully take ToCStr arguments by reference
334- // (without forcing an additional & around &str). So we are instead
335- // temporarily adding an instance for ~str and String, so that we can
336- // take ToCStr as owned. When DST lands, the string instances should
337- // be revisited, and arguments bound by ToCStr should be passed by
338- // reference.
339-
340- impl < ' a > ToCStr for & ' a str {
332+ impl ToCStr for str {
341333 #[ inline]
342334 fn to_c_str ( & self ) -> CString {
343335 self . as_bytes ( ) . to_c_str ( )
@@ -384,10 +376,10 @@ impl ToCStr for String {
384376// The length of the stack allocated buffer for `vec.with_c_str()`
385377const BUF_LEN : uint = 128 ;
386378
387- impl < ' a > ToCStr for & ' a [ u8 ] {
379+ impl ToCStr for [ u8 ] {
388380 fn to_c_str ( & self ) -> CString {
389381 let mut cs = unsafe { self . to_c_str_unchecked ( ) } ;
390- check_for_null ( * self , cs. as_mut_ptr ( ) ) ;
382+ check_for_null ( self , cs. as_mut_ptr ( ) ) ;
391383 cs
392384 }
393385
@@ -403,11 +395,33 @@ impl<'a> ToCStr for &'a [u8] {
403395 }
404396
405397 fn with_c_str < T > ( & self , f : |* const libc:: c_char | -> T ) -> T {
406- unsafe { with_c_str ( * self , true , f) }
398+ unsafe { with_c_str ( self , true , f) }
399+ }
400+
401+ unsafe fn with_c_str_unchecked < T > ( & self , f : |* const libc:: c_char | -> T ) -> T {
402+ with_c_str ( self , false , f)
403+ }
404+ }
405+
406+ impl < ' a , Sized ? T : ToCStr > ToCStr for & ' a T {
407+ #[ inline]
408+ fn to_c_str ( & self ) -> CString {
409+ ( * * self ) . to_c_str ( )
410+ }
411+
412+ #[ inline]
413+ unsafe fn to_c_str_unchecked ( & self ) -> CString {
414+ ( * * self ) . to_c_str_unchecked ( )
415+ }
416+
417+ #[ inline]
418+ fn with_c_str < T > ( & self , f : |* const libc:: c_char | -> T ) -> T {
419+ ( * * self ) . with_c_str ( f)
407420 }
408421
422+ #[ inline]
409423 unsafe fn with_c_str_unchecked < T > ( & self , f : |* const libc:: c_char | -> T ) -> T {
410- with_c_str ( * self , false , f)
424+ ( * * self ) . with_c_str_unchecked ( f)
411425 }
412426}
413427
0 commit comments