@@ -74,7 +74,7 @@ fn main() {
74
74
use collections:: string:: String ;
75
75
use collections:: hash;
76
76
use core:: fmt;
77
- use core:: kinds:: marker;
77
+ use core:: kinds:: { Sized , marker} ;
78
78
use core:: mem;
79
79
use core:: prelude:: { Clone , Collection , Drop , Eq , ImmutableSlice , Iterator } ;
80
80
use core:: prelude:: { MutableSlice , None , Option , Ordering , PartialEq } ;
@@ -286,7 +286,7 @@ impl fmt::Show for CString {
286
286
}
287
287
288
288
/// A generic trait for converting a value to a CString.
289
- pub trait ToCStr {
289
+ pub trait ToCStr for Sized ? {
290
290
/// Copy the receiver into a CString.
291
291
///
292
292
/// # Failure
@@ -329,15 +329,7 @@ pub trait ToCStr {
329
329
}
330
330
}
331
331
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 {
341
333
#[ inline]
342
334
fn to_c_str ( & self ) -> CString {
343
335
self . as_bytes ( ) . to_c_str ( )
@@ -384,10 +376,10 @@ impl ToCStr for String {
384
376
// The length of the stack allocated buffer for `vec.with_c_str()`
385
377
const BUF_LEN : uint = 128 ;
386
378
387
- impl < ' a > ToCStr for & ' a [ u8 ] {
379
+ impl ToCStr for [ u8 ] {
388
380
fn to_c_str ( & self ) -> CString {
389
381
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 ( ) ) ;
391
383
cs
392
384
}
393
385
@@ -403,11 +395,33 @@ impl<'a> ToCStr for &'a [u8] {
403
395
}
404
396
405
397
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)
407
420
}
408
421
422
+ #[ inline]
409
423
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)
411
425
}
412
426
}
413
427
0 commit comments