Skip to content

Commit fc60b2c

Browse files
committed
Prefer raw::c_char or libc::c_char and fix arm
1 parent 8c743a9 commit fc60b2c

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 30 additions & 37 deletions
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,21 @@ 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
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
4142
/// `u8` slice can be obtained with the `as_bytes` method. Slices produced from
4243
/// a `CString` do *not* contain the trailing nul terminator unless otherwise
4344
/// specified.
4445
///
4546
/// # Examples
4647
///
4748
/// ```no_run
48-
/// # #![feature(libc)]
49-
/// # extern crate libc;
5049
/// # fn main() {
5150
/// use std::ffi::CString;
52-
/// use libc;
51+
/// use std::os::raw::c_char;
5352
///
5453
/// extern {
55-
/// fn my_printer(s: *const libc::c_char);
54+
/// fn my_printer(s: *const c_char);
5655
/// }
5756
///
5857
/// let c_to_print = CString::new("Hello, world!").unwrap();
@@ -83,11 +82,10 @@ pub struct CString {
8382
/// Inspecting a foreign C string
8483
///
8584
/// ```no_run
86-
/// # #![feature(libc)]
87-
/// extern crate libc;
8885
/// use std::ffi::CStr;
86+
/// use std::os::raw::c_char;
8987
///
90-
/// extern { fn my_string() -> *const libc::c_char; }
88+
/// extern { fn my_string() -> *const c_char; }
9189
///
9290
/// fn main() {
9391
/// unsafe {
@@ -100,12 +98,11 @@ pub struct CString {
10098
/// Passing a Rust-originating C string
10199
///
102100
/// ```no_run
103-
/// # #![feature(libc)]
104-
/// extern crate libc;
105101
/// use std::ffi::{CString, CStr};
102+
/// use std::os::raw::c_char;
106103
///
107104
/// fn work(data: &CStr) {
108-
/// extern { fn work_with(data: *const libc::c_char); }
105+
/// extern { fn work_with(data: *const c_char); }
109106
///
110107
/// unsafe { work_with(data.as_ptr()) }
111108
/// }
@@ -119,11 +116,10 @@ pub struct CString {
119116
/// Converting a foreign C string into a Rust `String`
120117
///
121118
/// ```no_run
122-
/// # #![feature(libc)]
123-
/// extern crate libc;
124119
/// use std::ffi::CStr;
120+
/// use std::os::raw::c_char;
125121
///
126-
/// extern { fn my_string() -> *const libc::c_char; }
122+
/// extern { fn my_string() -> *const c_char; }
127123
///
128124
/// fn my_string_safe() -> String {
129125
/// unsafe {
@@ -139,10 +135,10 @@ pub struct CString {
139135
#[stable(feature = "rust1", since = "1.0.0")]
140136
pub struct CStr {
141137
// 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
143139
// this an unsized type. Essentially `sizeof(&CStr)` should be the
144140
// same as `sizeof(&c_char)` but `CStr` should be an unsized type.
145-
inner: [libc::c_char]
141+
inner: [c_char]
146142
}
147143

148144
/// An error returned from `CString::new` to indicate that a nul byte was found
@@ -169,11 +165,10 @@ impl CString {
169165
/// # Examples
170166
///
171167
/// ```no_run
172-
/// # #![feature(libc)]
173-
/// extern crate libc;
174168
/// use std::ffi::CString;
169+
/// use std::os::raw::c_char;
175170
///
176-
/// extern { fn puts(s: *const libc::c_char); }
171+
/// extern { fn puts(s: *const c_char); }
177172
///
178173
/// fn main() {
179174
/// let to_print = CString::new("Hello!").unwrap();
@@ -220,7 +215,7 @@ impl CString {
220215
#[unstable(feature = "cstr_memory2", reason = "recently added",
221216
issue = "27769")]
222217
#[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 {
224219
CString::from_raw(ptr as *mut _)
225220
}
226221

@@ -230,7 +225,7 @@ impl CString {
230225
/// `into_raw`. The length of the string will be recalculated
231226
/// using the pointer.
232227
#[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 {
234229
let len = libc::strlen(ptr) + 1; // Including the NUL byte
235230
let slice = slice::from_raw_parts(ptr, len as usize);
236231
CString { inner: mem::transmute(slice) }
@@ -247,7 +242,7 @@ impl CString {
247242
#[unstable(feature = "cstr_memory2", reason = "recently added",
248243
issue = "27769")]
249244
#[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 {
251246
self.into_raw() as *const _
252247
}
253248

@@ -260,8 +255,8 @@ impl CString {
260255
///
261256
/// Failure to call `from_raw` will lead to a memory leak.
262257
#[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
265260
}
266261

267262
/// Converts the `CString` into a `String` if it contains valid Unicode data.
@@ -426,15 +421,13 @@ impl CStr {
426421
/// # Examples
427422
///
428423
/// ```no_run
429-
/// # #![feature(libc)]
430-
/// # extern crate libc;
431424
/// # fn main() {
432425
/// use std::ffi::CStr;
426+
/// use std::os::raw::c_char;
433427
/// use std::str;
434-
/// use libc;
435428
///
436429
/// extern {
437-
/// fn my_string() -> *const libc::c_char;
430+
/// fn my_string() -> *const c_char;
438431
/// }
439432
///
440433
/// unsafe {
@@ -445,7 +438,7 @@ impl CStr {
445438
/// # }
446439
/// ```
447440
#[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 {
449442
let len = libc::strlen(ptr);
450443
mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
451444
}
@@ -456,7 +449,7 @@ impl CStr {
456449
/// to a contiguous region of memory terminated with a 0 byte to represent
457450
/// the end of the string.
458451
#[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 {
460453
self.inner.as_ptr()
461454
}
462455

@@ -560,14 +553,14 @@ impl ToOwned for CStr {
560553
mod tests {
561554
use prelude::v1::*;
562555
use super::*;
563-
use libc;
556+
use os::raw::c_char;
564557
use borrow::Cow::{Borrowed, Owned};
565558
use hash::{SipHasher, Hash, Hasher};
566559

567560
#[test]
568561
fn c_to_rust() {
569562
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;
571564
unsafe {
572565
assert_eq!(CStr::from_ptr(ptr).to_bytes(), b"123");
573566
assert_eq!(CStr::from_ptr(ptr).to_bytes_with_nul(), b"123\0");
@@ -616,13 +609,13 @@ mod tests {
616609
#[test]
617610
fn to_str() {
618611
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;
620613
unsafe {
621614
assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
622615
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
623616
}
624617
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;
626619
unsafe {
627620
assert!(CStr::from_ptr(ptr).to_str().is_err());
628621
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
@@ -632,7 +625,7 @@ mod tests {
632625
#[test]
633626
fn to_owned() {
634627
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;
636629

637630
let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
638631
assert_eq!(owned.as_bytes_with_nul(), data);
@@ -641,7 +634,7 @@ mod tests {
641634
#[test]
642635
fn equal_hash() {
643636
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;
645638
let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
646639

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

src/libstd/os/raw.rs

Lines changed: 6 additions & 2 deletions
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(all(not(target_os = "ios"), not(target_os = "macos"),
16+
any(target_arch = "aarch64", target_arch = "arm",
17+
target_os = "android")))]
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(any(target_os = "ios", target_os = "macos",
20+
not(any(target_arch = "aarch64", target_arch = "arm",
21+
target_os = "android"))))]
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)