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