File tree 3 files changed +70
-3
lines changed
3 files changed +70
-3
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,13 @@ pub trait ToCStr {
293
293
}
294
294
}
295
295
296
+ // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
297
+ // we cannot usefully take ToCStr arguments by reference (without forcing an
298
+ // additional & around &str). So we are instead temporarily adding an instance
299
+ // for ~str, so that we can take ToCStr as owned. When DST lands, the string
300
+ // instances should be revisted, and arguments bound by ToCStr should be passed
301
+ // by reference.
302
+
296
303
impl < ' a > ToCStr for & ' a str {
297
304
#[ inline]
298
305
fn to_c_str ( & self ) -> CString {
@@ -315,6 +322,28 @@ impl<'a> ToCStr for &'a str {
315
322
}
316
323
}
317
324
325
+ impl ToCStr for ~str {
326
+ #[ inline]
327
+ fn to_c_str ( & self ) -> CString {
328
+ self . as_bytes ( ) . to_c_str ( )
329
+ }
330
+
331
+ #[ inline]
332
+ unsafe fn to_c_str_unchecked ( & self ) -> CString {
333
+ self . as_bytes ( ) . to_c_str_unchecked ( )
334
+ }
335
+
336
+ #[ inline]
337
+ fn with_c_str < T > ( & self , f : |* libc:: c_char | -> T ) -> T {
338
+ self . as_bytes ( ) . with_c_str ( f)
339
+ }
340
+
341
+ #[ inline]
342
+ unsafe fn with_c_str_unchecked < T > ( & self , f : |* libc:: c_char | -> T ) -> T {
343
+ self . as_bytes ( ) . with_c_str_unchecked ( f)
344
+ }
345
+ }
346
+
318
347
// The length of the stack allocated buffer for `vec.with_c_str()`
319
348
static BUF_LEN : uint = 128 ;
320
349
Original file line number Diff line number Diff line change @@ -79,11 +79,18 @@ impl FromStr for Path {
79
79
}
80
80
}
81
81
82
+ // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
83
+ // we cannot usefully take ToCStr arguments by reference (without forcing an
84
+ // additional & around &str). So we are instead temporarily adding an instance
85
+ // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
86
+ // instance should be removed, and arguments bound by ToCStr should be passed by
87
+ // reference.
88
+
82
89
impl ToCStr for Path {
83
90
#[ inline]
84
91
fn to_c_str ( & self ) -> CString {
85
92
// The Path impl guarantees no internal NUL
86
- unsafe { self . as_vec ( ) . to_c_str_unchecked ( ) }
93
+ unsafe { self . to_c_str_unchecked ( ) }
87
94
}
88
95
89
96
#[ inline]
@@ -92,6 +99,18 @@ impl ToCStr for Path {
92
99
}
93
100
}
94
101
102
+ impl < ' a > ToCStr for & ' a Path {
103
+ #[ inline]
104
+ fn to_c_str ( & self ) -> CString {
105
+ ( * self ) . to_c_str ( )
106
+ }
107
+
108
+ #[ inline]
109
+ unsafe fn to_c_str_unchecked ( & self ) -> CString {
110
+ ( * self ) . to_c_str_unchecked ( )
111
+ }
112
+ }
113
+
95
114
impl < S : Writer > :: hash:: Hash < S > for Path {
96
115
#[ inline]
97
116
fn hash ( & self , state : & mut S ) {
Original file line number Diff line number Diff line change @@ -103,11 +103,18 @@ impl FromStr for Path {
103
103
}
104
104
}
105
105
106
+ // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
107
+ // we cannot usefully take ToCStr arguments by reference (without forcing an
108
+ // additional & around &str). So we are instead temporarily adding an instance
109
+ // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
110
+ // instance should be removed, and arguments bound by ToCStr should be passed by
111
+ // reference.
112
+
106
113
impl ToCStr for Path {
107
114
#[ inline]
108
115
fn to_c_str ( & self ) -> CString {
109
- // The Path impl guarantees no embedded NULs
110
- unsafe { self . as_vec ( ) . to_c_str_unchecked ( ) }
116
+ // The Path impl guarantees no internal NUL
117
+ unsafe { self . to_c_str_unchecked ( ) }
111
118
}
112
119
113
120
#[ inline]
@@ -116,6 +123,18 @@ impl ToCStr for Path {
116
123
}
117
124
}
118
125
126
+ impl < ' a > ToCStr for & ' a Path {
127
+ #[ inline]
128
+ fn to_c_str ( & self ) -> CString {
129
+ ( * self ) . to_c_str ( )
130
+ }
131
+
132
+ #[ inline]
133
+ unsafe fn to_c_str_unchecked ( & self ) -> CString {
134
+ ( * self ) . to_c_str_unchecked ( )
135
+ }
136
+ }
137
+
119
138
impl < S : Writer > :: hash:: Hash < S > for Path {
120
139
#[ inline]
121
140
fn hash ( & self , state : & mut S ) {
You can’t perform that action at this time.
0 commit comments