8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ #![ unstable( feature = "std_misc" ) ]
12
+
11
13
use cmp:: { PartialEq , Eq , PartialOrd , Ord , Ordering } ;
12
14
use error:: { Error , FromError } ;
13
15
use fmt;
@@ -59,6 +61,7 @@ use vec::Vec;
59
61
/// # }
60
62
/// ```
61
63
#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
64
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
62
65
pub struct CString {
63
66
inner : Vec < u8 > ,
64
67
}
@@ -110,13 +113,19 @@ pub struct CString {
110
113
/// }
111
114
/// ```
112
115
#[ derive( Hash ) ]
116
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
113
117
pub struct CStr {
118
+ // FIXME: this should not be represented with a DST slice but rather with
119
+ // just a raw `libc::c_char` along with some form of marker to make
120
+ // this an unsized type. Essentially `sizeof(&CStr)` should be the
121
+ // same as `sizeof(&c_char)` but `CStr` should be an unsized type.
114
122
inner : [ libc:: c_char ]
115
123
}
116
124
117
125
/// An error returned from `CString::new` to indicate that a nul byte was found
118
126
/// in the vector provided.
119
127
#[ derive( Clone , PartialEq , Debug ) ]
128
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
120
129
pub struct NulError ( usize , Vec < u8 > ) ;
121
130
122
131
/// A conversion trait used by the constructor of `CString` for types that can
@@ -153,6 +162,7 @@ impl CString {
153
162
/// This function will return an error if the bytes yielded contain an
154
163
/// internal 0 byte. The error returned will contain the bytes as well as
155
164
/// the position of the nul byte.
165
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
156
166
pub fn new < T : IntoBytes > ( t : T ) -> Result < CString , NulError > {
157
167
let bytes = t. into_bytes ( ) ;
158
168
match bytes. iter ( ) . position ( |x| * x == 0 ) {
@@ -216,6 +226,7 @@ impl CString {
216
226
///
217
227
/// This method is equivalent to `from_vec` except that no runtime assertion
218
228
/// is made that `v` contains no 0 bytes.
229
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
219
230
pub unsafe fn from_vec_unchecked ( mut v : Vec < u8 > ) -> CString {
220
231
v. push ( 0 ) ;
221
232
CString { inner : v }
@@ -225,17 +236,20 @@ impl CString {
225
236
///
226
237
/// The returned slice does **not** contain the trailing nul separator and
227
238
/// it is guaranteed to not have any interior nul bytes.
239
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
228
240
pub fn as_bytes ( & self ) -> & [ u8 ] {
229
241
& self . inner [ ..self . inner . len ( ) - 1 ]
230
242
}
231
243
232
244
/// Equivalent to the `as_bytes` function except that the returned slice
233
245
/// includes the trailing nul byte.
246
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
234
247
pub fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
235
248
& self . inner
236
249
}
237
250
}
238
251
252
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
239
253
impl Deref for CString {
240
254
type Target = CStr ;
241
255
@@ -254,30 +268,36 @@ impl fmt::Debug for CString {
254
268
impl NulError {
255
269
/// Returns the position of the nul byte in the slice that was provided to
256
270
/// `CString::from_vec`.
271
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
257
272
pub fn nul_position ( & self ) -> usize { self . 0 }
258
273
259
274
/// Consumes this error, returning the underlying vector of bytes which
260
275
/// generated the error in the first place.
276
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
261
277
pub fn into_vec ( self ) -> Vec < u8 > { self . 1 }
262
278
}
263
279
280
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
264
281
impl Error for NulError {
265
282
fn description ( & self ) -> & str { "nul byte found in data" }
266
283
}
267
284
285
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
268
286
impl fmt:: Display for NulError {
269
287
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
270
288
write ! ( f, "nul byte found in provided data at position: {}" , self . 0 )
271
289
}
272
290
}
273
291
292
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
274
293
impl FromError < NulError > for io:: Error {
275
294
fn from_error ( _: NulError ) -> io:: Error {
276
295
io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
277
296
"data provided contains a nul byte" , None )
278
297
}
279
298
}
280
299
300
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
281
301
impl FromError < NulError > for old_io:: IoError {
282
302
fn from_error ( _: NulError ) -> old_io:: IoError {
283
303
old_io:: IoError {
@@ -325,6 +345,7 @@ impl CStr {
325
345
/// }
326
346
/// # }
327
347
/// ```
348
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
328
349
pub unsafe fn from_ptr < ' a > ( ptr : * const libc:: c_char ) -> & ' a CStr {
329
350
let len = libc:: strlen ( ptr) ;
330
351
mem:: transmute ( slice:: from_raw_parts ( ptr, len as usize + 1 ) )
@@ -335,6 +356,7 @@ impl CStr {
335
356
/// The returned pointer will be valid for as long as `self` is and points
336
357
/// to a contiguous region of memory terminated with a 0 byte to represent
337
358
/// the end of the string.
359
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
338
360
pub fn as_ptr ( & self ) -> * const libc:: c_char {
339
361
self . inner . as_ptr ( )
340
362
}
@@ -351,6 +373,7 @@ impl CStr {
351
373
/// > **Note**: This method is currently implemented as a 0-cost cast, but
352
374
/// > it is planned to alter its definition in the future to perform the
353
375
/// > length calculation whenever this method is called.
376
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
354
377
pub fn to_bytes ( & self ) -> & [ u8 ] {
355
378
let bytes = self . to_bytes_with_nul ( ) ;
356
379
& bytes[ ..bytes. len ( ) - 1 ]
@@ -364,22 +387,27 @@ impl CStr {
364
387
/// > **Note**: This method is currently implemented as a 0-cost cast, but
365
388
/// > it is planned to alter its definition in the future to perform the
366
389
/// > length calculation whenever this method is called.
390
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
367
391
pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
368
392
unsafe { mem:: transmute :: < & [ libc:: c_char ] , & [ u8 ] > ( & self . inner ) }
369
393
}
370
394
}
371
395
396
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
372
397
impl PartialEq for CStr {
373
398
fn eq ( & self , other : & CStr ) -> bool {
374
399
self . to_bytes ( ) . eq ( other. to_bytes ( ) )
375
400
}
376
401
}
402
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
377
403
impl Eq for CStr { }
404
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
378
405
impl PartialOrd for CStr {
379
406
fn partial_cmp ( & self , other : & CStr ) -> Option < Ordering > {
380
407
self . to_bytes ( ) . partial_cmp ( & other. to_bytes ( ) )
381
408
}
382
409
}
410
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
383
411
impl Ord for CStr {
384
412
fn cmp ( & self , other : & CStr ) -> Ordering {
385
413
self . to_bytes ( ) . cmp ( & other. to_bytes ( ) )
0 commit comments