Skip to content

Commit 3ef3fd9

Browse files
committed
Rollup merge of #29775 - arcnmx:raw-c_char, r=alexcrichton
It's a bit strange to expect users of `libstd` to require the use of an external crates.io crate to work with standard types. This commit encourages the use `os::raw::c_char` instead, although users are certainly free to use `libc::c_char` if they wish; the test still exists to ensure the two types are identical (though the reported bug only exists on platforms that are not officially tested). Fixes #29774
2 parents f51e030 + f9f7347 commit 3ef3fd9

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

src/libstd/ffi/c_str.rs

+32-40
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use libc;
2121
use mem;
2222
use ops::Deref;
2323
use option::Option::{self, Some, None};
24+
use os::raw::c_char;
2425
use result::Result::{self, Ok, Err};
2526
use slice;
2627
use str::{self, Utf8Error};
@@ -36,23 +37,20 @@ use vec::Vec;
3637
///
3738
/// A `CString` is created from either a byte slice or a byte vector. After
3839
/// 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.
4444
///
4545
/// # Examples
4646
///
4747
/// ```no_run
48-
/// # #![feature(libc)]
49-
/// # extern crate libc;
5048
/// # fn main() {
5149
/// use std::ffi::CString;
52-
/// use libc;
50+
/// use std::os::raw::c_char;
5351
///
5452
/// extern {
55-
/// fn my_printer(s: *const libc::c_char);
53+
/// fn my_printer(s: *const c_char);
5654
/// }
5755
///
5856
/// let c_to_print = CString::new("Hello, world!").unwrap();
@@ -83,11 +81,10 @@ pub struct CString {
8381
/// Inspecting a foreign C string
8482
///
8583
/// ```no_run
86-
/// # #![feature(libc)]
87-
/// extern crate libc;
8884
/// use std::ffi::CStr;
85+
/// use std::os::raw::c_char;
8986
///
90-
/// extern { fn my_string() -> *const libc::c_char; }
87+
/// extern { fn my_string() -> *const c_char; }
9188
///
9289
/// fn main() {
9390
/// unsafe {
@@ -100,12 +97,11 @@ pub struct CString {
10097
/// Passing a Rust-originating C string
10198
///
10299
/// ```no_run
103-
/// # #![feature(libc)]
104-
/// extern crate libc;
105100
/// use std::ffi::{CString, CStr};
101+
/// use std::os::raw::c_char;
106102
///
107103
/// fn work(data: &CStr) {
108-
/// extern { fn work_with(data: *const libc::c_char); }
104+
/// extern { fn work_with(data: *const c_char); }
109105
///
110106
/// unsafe { work_with(data.as_ptr()) }
111107
/// }
@@ -119,11 +115,10 @@ pub struct CString {
119115
/// Converting a foreign C string into a Rust `String`
120116
///
121117
/// ```no_run
122-
/// # #![feature(libc)]
123-
/// extern crate libc;
124118
/// use std::ffi::CStr;
119+
/// use std::os::raw::c_char;
125120
///
126-
/// extern { fn my_string() -> *const libc::c_char; }
121+
/// extern { fn my_string() -> *const c_char; }
127122
///
128123
/// fn my_string_safe() -> String {
129124
/// unsafe {
@@ -139,10 +134,10 @@ pub struct CString {
139134
#[stable(feature = "rust1", since = "1.0.0")]
140135
pub struct CStr {
141136
// 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
143138
// this an unsized type. Essentially `sizeof(&CStr)` should be the
144139
// same as `sizeof(&c_char)` but `CStr` should be an unsized type.
145-
inner: [libc::c_char]
140+
inner: [c_char]
146141
}
147142

148143
/// An error returned from `CString::new` to indicate that a nul byte was found
@@ -169,11 +164,10 @@ impl CString {
169164
/// # Examples
170165
///
171166
/// ```no_run
172-
/// # #![feature(libc)]
173-
/// extern crate libc;
174167
/// use std::ffi::CString;
168+
/// use std::os::raw::c_char;
175169
///
176-
/// extern { fn puts(s: *const libc::c_char); }
170+
/// extern { fn puts(s: *const c_char); }
177171
///
178172
/// fn main() {
179173
/// let to_print = CString::new("Hello!").unwrap();
@@ -220,7 +214,7 @@ impl CString {
220214
#[unstable(feature = "cstr_memory2", reason = "recently added",
221215
issue = "27769")]
222216
#[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 {
224218
CString::from_raw(ptr as *mut _)
225219
}
226220

@@ -230,7 +224,7 @@ impl CString {
230224
/// `into_raw`. The length of the string will be recalculated
231225
/// using the pointer.
232226
#[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 {
234228
let len = libc::strlen(ptr) + 1; // Including the NUL byte
235229
let slice = slice::from_raw_parts(ptr, len as usize);
236230
CString { inner: mem::transmute(slice) }
@@ -247,7 +241,7 @@ impl CString {
247241
#[unstable(feature = "cstr_memory2", reason = "recently added",
248242
issue = "27769")]
249243
#[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 {
251245
self.into_raw() as *const _
252246
}
253247

@@ -260,8 +254,8 @@ impl CString {
260254
///
261255
/// Failure to call `from_raw` will lead to a memory leak.
262256
#[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
265259
}
266260

267261
/// Converts the `CString` into a `String` if it contains valid Unicode data.
@@ -426,15 +420,13 @@ impl CStr {
426420
/// # Examples
427421
///
428422
/// ```no_run
429-
/// # #![feature(libc)]
430-
/// # extern crate libc;
431423
/// # fn main() {
432424
/// use std::ffi::CStr;
425+
/// use std::os::raw::c_char;
433426
/// use std::str;
434-
/// use libc;
435427
///
436428
/// extern {
437-
/// fn my_string() -> *const libc::c_char;
429+
/// fn my_string() -> *const c_char;
438430
/// }
439431
///
440432
/// unsafe {
@@ -445,7 +437,7 @@ impl CStr {
445437
/// # }
446438
/// ```
447439
#[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 {
449441
let len = libc::strlen(ptr);
450442
mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
451443
}
@@ -456,7 +448,7 @@ impl CStr {
456448
/// to a contiguous region of memory terminated with a 0 byte to represent
457449
/// the end of the string.
458450
#[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 {
460452
self.inner.as_ptr()
461453
}
462454

@@ -560,14 +552,14 @@ impl ToOwned for CStr {
560552
mod tests {
561553
use prelude::v1::*;
562554
use super::*;
563-
use libc;
555+
use os::raw::c_char;
564556
use borrow::Cow::{Borrowed, Owned};
565557
use hash::{SipHasher, Hash, Hasher};
566558

567559
#[test]
568560
fn c_to_rust() {
569561
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;
571563
unsafe {
572564
assert_eq!(CStr::from_ptr(ptr).to_bytes(), b"123");
573565
assert_eq!(CStr::from_ptr(ptr).to_bytes_with_nul(), b"123\0");
@@ -616,13 +608,13 @@ mod tests {
616608
#[test]
617609
fn to_str() {
618610
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;
620612
unsafe {
621613
assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
622614
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
623615
}
624616
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;
626618
unsafe {
627619
assert!(CStr::from_ptr(ptr).to_str().is_err());
628620
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
@@ -632,7 +624,7 @@ mod tests {
632624
#[test]
633625
fn to_owned() {
634626
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;
636628

637629
let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
638630
assert_eq!(owned.as_bytes_with_nul(), data);
@@ -641,7 +633,7 @@ mod tests {
641633
#[test]
642634
fn equal_hash() {
643635
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;
645637
let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
646638

647639
let mut s = SipHasher::new_with_keys(0, 0);

src/libstd/os/raw.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
1313
#![stable(feature = "raw_os", since = "1.1.0")]
1414

15-
#[cfg(any(target_arch = "aarch64", target_os = "android"))]
15+
#[cfg(any(target_os = "android",
16+
all(target_os = "linux", any(target_arch = "aarch64",
17+
target_arch = "arm"))))]
1618
#[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = u8;
17-
#[cfg(not(any(target_arch = "aarch64", target_os = "android")))]
19+
#[cfg(not(any(target_os = "android",
20+
all(target_os = "linux", any(target_arch = "aarch64",
21+
target_arch = "arm")))))]
1822
#[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = i8;
1923
#[stable(feature = "raw_os", since = "1.1.0")] pub type c_schar = i8;
2024
#[stable(feature = "raw_os", since = "1.1.0")] pub type c_uchar = u8;

0 commit comments

Comments
 (0)