@@ -21,6 +21,7 @@ use libc;
21
21
use mem;
22
22
use ops:: Deref ;
23
23
use option:: Option :: { self , Some , None } ;
24
+ use os:: raw:: c_char;
24
25
use result:: Result :: { self , Ok , Err } ;
25
26
use slice;
26
27
use str:: { self , Utf8Error } ;
@@ -36,23 +37,20 @@ use vec::Vec;
36
37
///
37
38
/// A `CString` is created from either a byte slice or a byte vector. After
38
39
/// being created, a `CString` predominately inherits all of its methods from
39
- /// the `Deref` implementation to `[libc::c_char]`. Note that the underlying
40
- /// array is represented as an array of `libc::c_char` as opposed to `u8`. A
41
- /// `u8` slice can be obtained with the `as_bytes` method. Slices produced from
42
- /// a `CString` do *not* contain the trailing nul terminator unless otherwise
43
- /// specified.
40
+ /// the `Deref` implementation to `[c_char]`. Note that the underlying array
41
+ /// is represented as an array of `c_char` as opposed to `u8`. A `u8` slice
42
+ /// can be obtained with the `as_bytes` method. Slices produced from a `CString`
43
+ /// do *not* contain the trailing nul terminator unless otherwise specified.
44
44
///
45
45
/// # Examples
46
46
///
47
47
/// ```no_run
48
- /// # #![feature(libc)]
49
- /// # extern crate libc;
50
48
/// # fn main() {
51
49
/// use std::ffi::CString;
52
- /// use libc ;
50
+ /// use std::os::raw::c_char ;
53
51
///
54
52
/// extern {
55
- /// fn my_printer(s: *const libc:: c_char);
53
+ /// fn my_printer(s: *const c_char);
56
54
/// }
57
55
///
58
56
/// let c_to_print = CString::new("Hello, world!").unwrap();
@@ -83,11 +81,10 @@ pub struct CString {
83
81
/// Inspecting a foreign C string
84
82
///
85
83
/// ```no_run
86
- /// # #![feature(libc)]
87
- /// extern crate libc;
88
84
/// use std::ffi::CStr;
85
+ /// use std::os::raw::c_char;
89
86
///
90
- /// extern { fn my_string() -> *const libc:: c_char; }
87
+ /// extern { fn my_string() -> *const c_char; }
91
88
///
92
89
/// fn main() {
93
90
/// unsafe {
@@ -100,12 +97,11 @@ pub struct CString {
100
97
/// Passing a Rust-originating C string
101
98
///
102
99
/// ```no_run
103
- /// # #![feature(libc)]
104
- /// extern crate libc;
105
100
/// use std::ffi::{CString, CStr};
101
+ /// use std::os::raw::c_char;
106
102
///
107
103
/// fn work(data: &CStr) {
108
- /// extern { fn work_with(data: *const libc:: c_char); }
104
+ /// extern { fn work_with(data: *const c_char); }
109
105
///
110
106
/// unsafe { work_with(data.as_ptr()) }
111
107
/// }
@@ -119,11 +115,10 @@ pub struct CString {
119
115
/// Converting a foreign C string into a Rust `String`
120
116
///
121
117
/// ```no_run
122
- /// # #![feature(libc)]
123
- /// extern crate libc;
124
118
/// use std::ffi::CStr;
119
+ /// use std::os::raw::c_char;
125
120
///
126
- /// extern { fn my_string() -> *const libc:: c_char; }
121
+ /// extern { fn my_string() -> *const c_char; }
127
122
///
128
123
/// fn my_string_safe() -> String {
129
124
/// unsafe {
@@ -139,10 +134,10 @@ pub struct CString {
139
134
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
140
135
pub struct CStr {
141
136
// FIXME: this should not be represented with a DST slice but rather with
142
- // just a raw `libc:: c_char` along with some form of marker to make
137
+ // just a raw `c_char` along with some form of marker to make
143
138
// this an unsized type. Essentially `sizeof(&CStr)` should be the
144
139
// same as `sizeof(&c_char)` but `CStr` should be an unsized type.
145
- inner : [ libc :: c_char ]
140
+ inner : [ c_char ]
146
141
}
147
142
148
143
/// An error returned from `CString::new` to indicate that a nul byte was found
@@ -169,11 +164,10 @@ impl CString {
169
164
/// # Examples
170
165
///
171
166
/// ```no_run
172
- /// # #![feature(libc)]
173
- /// extern crate libc;
174
167
/// use std::ffi::CString;
168
+ /// use std::os::raw::c_char;
175
169
///
176
- /// extern { fn puts(s: *const libc:: c_char); }
170
+ /// extern { fn puts(s: *const c_char); }
177
171
///
178
172
/// fn main() {
179
173
/// let to_print = CString::new("Hello!").unwrap();
@@ -220,7 +214,7 @@ impl CString {
220
214
#[ unstable( feature = "cstr_memory2" , reason = "recently added" ,
221
215
issue = "27769" ) ]
222
216
#[ deprecated( since = "1.4.0" , reason = "renamed to from_raw" ) ]
223
- pub unsafe fn from_ptr ( ptr : * const libc :: c_char ) -> CString {
217
+ pub unsafe fn from_ptr ( ptr : * const c_char ) -> CString {
224
218
CString :: from_raw ( ptr as * mut _ )
225
219
}
226
220
@@ -230,7 +224,7 @@ impl CString {
230
224
/// `into_raw`. The length of the string will be recalculated
231
225
/// using the pointer.
232
226
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
233
- pub unsafe fn from_raw ( ptr : * mut libc :: c_char ) -> CString {
227
+ pub unsafe fn from_raw ( ptr : * mut c_char ) -> CString {
234
228
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
235
229
let slice = slice:: from_raw_parts ( ptr, len as usize ) ;
236
230
CString { inner : mem:: transmute ( slice) }
@@ -247,7 +241,7 @@ impl CString {
247
241
#[ unstable( feature = "cstr_memory2" , reason = "recently added" ,
248
242
issue = "27769" ) ]
249
243
#[ deprecated( since = "1.4.0" , reason = "renamed to into_raw" ) ]
250
- pub fn into_ptr ( self ) -> * const libc :: c_char {
244
+ pub fn into_ptr ( self ) -> * const c_char {
251
245
self . into_raw ( ) as * const _
252
246
}
253
247
@@ -260,8 +254,8 @@ impl CString {
260
254
///
261
255
/// Failure to call `from_raw` will lead to a memory leak.
262
256
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
263
- pub fn into_raw ( self ) -> * mut libc :: c_char {
264
- Box :: into_raw ( self . inner ) as * mut libc :: c_char
257
+ pub fn into_raw ( self ) -> * mut c_char {
258
+ Box :: into_raw ( self . inner ) as * mut c_char
265
259
}
266
260
267
261
/// Converts the `CString` into a `String` if it contains valid Unicode data.
@@ -426,15 +420,13 @@ impl CStr {
426
420
/// # Examples
427
421
///
428
422
/// ```no_run
429
- /// # #![feature(libc)]
430
- /// # extern crate libc;
431
423
/// # fn main() {
432
424
/// use std::ffi::CStr;
425
+ /// use std::os::raw::c_char;
433
426
/// use std::str;
434
- /// use libc;
435
427
///
436
428
/// extern {
437
- /// fn my_string() -> *const libc:: c_char;
429
+ /// fn my_string() -> *const c_char;
438
430
/// }
439
431
///
440
432
/// unsafe {
@@ -445,7 +437,7 @@ impl CStr {
445
437
/// # }
446
438
/// ```
447
439
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
448
- pub unsafe fn from_ptr < ' a > ( ptr : * const libc :: c_char ) -> & ' a CStr {
440
+ pub unsafe fn from_ptr < ' a > ( ptr : * const c_char ) -> & ' a CStr {
449
441
let len = libc:: strlen ( ptr) ;
450
442
mem:: transmute ( slice:: from_raw_parts ( ptr, len as usize + 1 ) )
451
443
}
@@ -456,7 +448,7 @@ impl CStr {
456
448
/// to a contiguous region of memory terminated with a 0 byte to represent
457
449
/// the end of the string.
458
450
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
459
- pub fn as_ptr ( & self ) -> * const libc :: c_char {
451
+ pub fn as_ptr ( & self ) -> * const c_char {
460
452
self . inner . as_ptr ( )
461
453
}
462
454
@@ -560,14 +552,14 @@ impl ToOwned for CStr {
560
552
mod tests {
561
553
use prelude:: v1:: * ;
562
554
use super :: * ;
563
- use libc ;
555
+ use os :: raw :: c_char ;
564
556
use borrow:: Cow :: { Borrowed , Owned } ;
565
557
use hash:: { SipHasher , Hash , Hasher } ;
566
558
567
559
#[ test]
568
560
fn c_to_rust ( ) {
569
561
let data = b"123\0 " ;
570
- let ptr = data. as_ptr ( ) as * const libc :: c_char ;
562
+ let ptr = data. as_ptr ( ) as * const c_char ;
571
563
unsafe {
572
564
assert_eq ! ( CStr :: from_ptr( ptr) . to_bytes( ) , b"123" ) ;
573
565
assert_eq ! ( CStr :: from_ptr( ptr) . to_bytes_with_nul( ) , b"123\0 " ) ;
@@ -616,13 +608,13 @@ mod tests {
616
608
#[ test]
617
609
fn to_str ( ) {
618
610
let data = b"123\xE2 \x80 \xA6 \0 " ;
619
- let ptr = data. as_ptr ( ) as * const libc :: c_char ;
611
+ let ptr = data. as_ptr ( ) as * const c_char ;
620
612
unsafe {
621
613
assert_eq ! ( CStr :: from_ptr( ptr) . to_str( ) , Ok ( "123…" ) ) ;
622
614
assert_eq ! ( CStr :: from_ptr( ptr) . to_string_lossy( ) , Borrowed ( "123…" ) ) ;
623
615
}
624
616
let data = b"123\xE2 \0 " ;
625
- let ptr = data. as_ptr ( ) as * const libc :: c_char ;
617
+ let ptr = data. as_ptr ( ) as * const c_char ;
626
618
unsafe {
627
619
assert ! ( CStr :: from_ptr( ptr) . to_str( ) . is_err( ) ) ;
628
620
assert_eq ! ( CStr :: from_ptr( ptr) . to_string_lossy( ) , Owned :: <str >( format!( "123\u{FFFD} " ) ) ) ;
@@ -632,7 +624,7 @@ mod tests {
632
624
#[ test]
633
625
fn to_owned ( ) {
634
626
let data = b"123\0 " ;
635
- let ptr = data. as_ptr ( ) as * const libc :: c_char ;
627
+ let ptr = data. as_ptr ( ) as * const c_char ;
636
628
637
629
let owned = unsafe { CStr :: from_ptr ( ptr) . to_owned ( ) } ;
638
630
assert_eq ! ( owned. as_bytes_with_nul( ) , data) ;
@@ -641,7 +633,7 @@ mod tests {
641
633
#[ test]
642
634
fn equal_hash ( ) {
643
635
let data = b"123\xE2 \xFA \xA6 \0 " ;
644
- let ptr = data. as_ptr ( ) as * const libc :: c_char ;
636
+ let ptr = data. as_ptr ( ) as * const c_char ;
645
637
let cstr: & ' static CStr = unsafe { CStr :: from_ptr ( ptr) } ;
646
638
647
639
let mut s = SipHasher :: new_with_keys ( 0 , 0 ) ;
0 commit comments