std: Redesign c_str and c_vec

This commit is an implementation of [RFC 494][rfc] which removes the entire
`std::c_vec` module and redesigns the `std::c_str` module as `std::ffi`.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0494-c_str-and-c_vec-stability.md

The interface of the new `CString` is outlined in the linked RFC, the primary
changes being:

* The `ToCStr` trait is gone, meaning the `with_c_str` and `to_c_str` methods
  are now gone. These two methods are replaced with a `CString::from_slice`
  method.
* The `CString` type is now just a wrapper around `Vec<u8>` with a static
  guarantee that there is a trailing nul byte with no internal nul bytes. This
  means that `CString` now implements `Deref<Target = [c_char]>`, which is where
  it gains most of its methods from. A few helper methods are added to acquire a
  slice of `u8` instead of `c_char`, as well as including a slice with the
  trailing nul byte if necessary.
* All usage of non-owned `CString` values is now done via two functions inside
  of `std::ffi`, called `c_str_to_bytes` and `c_str_to_bytes_with_nul`. These
  functions are now the one method used to convert a `*const c_char` to a Rust
  slice of `u8`.

Many more details, including newly deprecated methods, can be found linked in
the RFC. This is a:

[breaking-change]
Closes #20444
This commit is contained in:
Alex Crichton 2014-11-25 13:28:35 -08:00
parent 1f732ef53d
commit ec7a50d20d
59 changed files with 1018 additions and 1998 deletions

View file

@ -27,7 +27,7 @@ fn bar() { }
fn baz() { }
pub fn test() {
let none: Option<Path> = None; // appease the typechecker
let none: Option<&Path> = None; // appease the typechecker
let lib = DynamicLibrary::open(none).unwrap();
unsafe {
assert!(lib.symbol::<int>("foo").is_ok());

View file

@ -12,7 +12,7 @@
extern crate libc;
use std::c_str::ToCStr;
use std::ffi::CString;
mod mlibc {
use libc::{c_char, c_long, c_longlong};
@ -24,11 +24,13 @@ mod mlibc {
}
fn atol(s: String) -> int {
s.as_slice().with_c_str(|x| unsafe { mlibc::atol(x) as int })
let c = CString::from_slice(s.as_bytes());
unsafe { mlibc::atol(c.as_ptr()) as int }
}
fn atoll(s: String) -> i64 {
s.as_slice().with_c_str(|x| unsafe { mlibc::atoll(x) as i64 })
let c = CString::from_slice(s.as_bytes());
unsafe { mlibc::atoll(c.as_ptr()) as i64 }
}
pub fn main() {

View file

@ -9,7 +9,6 @@
// except according to those terms.
use std::{str, string};
use std::c_str::ToCStr;
const A: [u8; 2] = ['h' as u8, 'i' as u8];
const B: &'static [u8; 2] = &A;
@ -23,8 +22,5 @@ pub fn main() {
assert_eq!(String::from_raw_buf_len(C, B.len()), "hi".to_string());
assert!(*C == A[0]);
assert!(*(&B[0] as *const u8) == A[0]);
let bar = str::from_utf8_unchecked(&A).to_c_str();
assert_eq!(bar.as_str(), "hi".to_c_str().as_str());
}
}

View file

@ -11,7 +11,7 @@
// ignore-fast doesn't like extern crate
extern crate libc;
use std::c_str::ToCStr;
use std::ffi::CString;
mod mlibc {
use libc::{c_char, size_t};
@ -24,11 +24,10 @@ mod mlibc {
fn strlen(str: String) -> uint {
// C string is terminated with a zero
str.as_slice().with_c_str(|buf| {
unsafe {
mlibc::my_strlen(buf) as uint
}
})
let s = CString::from_slice(str.as_bytes());
unsafe {
mlibc::my_strlen(s.as_ptr()) as uint
}
}
pub fn main() {

View file

@ -13,8 +13,8 @@
extern crate libc;
use std::ffi::CString;
use std::io::TempDir;
use std::c_str::ToCStr;
use std::io::fs::PathExtensions;
use std::io::fs;
use std::io;
@ -31,20 +31,17 @@ fn rename_directory() {
let test_file = &old_path.join("temp.txt");
/* Write the temp input file */
let ostream = test_file.with_c_str(|fromp| {
"w+b".with_c_str(|modebuf| {
libc::fopen(fromp, modebuf)
})
});
let fromp = CString::from_slice(test_file.as_vec());
let modebuf = CString::from_slice(b"w+b");
let ostream = libc::fopen(fromp.as_ptr(), modebuf.as_ptr());
assert!((ostream as uint != 0u));
let s = "hello".to_string();
"hello".with_c_str(|buf| {
let write_len = libc::fwrite(buf as *const libc::c_void,
1u as libc::size_t,
(s.len() + 1u) as libc::size_t,
ostream);
assert_eq!(write_len, (s.len() + 1) as libc::size_t)
});
let buf = CString::from_slice(b"hello");
let write_len = libc::fwrite(buf.as_ptr() as *mut _,
1u as libc::size_t,
(s.len() + 1u) as libc::size_t,
ostream);
assert_eq!(write_len, (s.len() + 1) as libc::size_t);
assert_eq!(libc::fclose(ostream), (0u as libc::c_int));
let new_path = tmpdir.join_many(&["quux", "blat"]);

View file

@ -10,7 +10,7 @@
extern crate libc;
use std::c_str::{CString, ToCStr};
use std::ffi::{self, CString};
use libc::{c_char, c_int};
// ignore-fast doesn't like extern crate
@ -22,40 +22,35 @@ extern {
unsafe fn check<T>(expected: &str, f: |*mut c_char| -> T) {
let mut x = [0 as c_char; 50];
f(&mut x[0] as *mut c_char);
let res = CString::new(&x[0], false);
assert_eq!(expected, res.as_str().unwrap());
assert_eq!(expected.as_bytes(), ffi::c_str_to_bytes(&x.as_ptr()));
}
pub fn main() {
unsafe {
// Call with just the named parameter
"Hello World\n".with_c_str(|c| {
check("Hello World\n", |s| sprintf(s, c));
});
let c = CString::from_slice(b"Hello World\n");
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
// Call with variable number of arguments
"%d %f %c %s\n".with_c_str(|c| {
check("42 42.500000 a %d %f %c %s\n\n", |s| {
sprintf(s, c, 42i, 42.5f64, 'a' as c_int, c);
})
let c = CString::from_slice(b"%d %f %c %s\n");
check("42 42.500000 a %d %f %c %s\n\n", |s| {
sprintf(s, c.as_ptr(), 42i, 42.5f64, 'a' as c_int, c.as_ptr());
});
// Make a function pointer
let x: unsafe extern "C" fn(*mut c_char, *const c_char, ...) -> c_int = sprintf;
let x: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int = sprintf;
// A function that takes a function pointer
unsafe fn call(p: unsafe extern "C" fn(*mut c_char, *const c_char, ...) -> c_int) {
// Call with just the named parameter via fn pointer
"Hello World\n".with_c_str(|c| {
check("Hello World\n", |s| p(s, c));
});
unsafe fn call(p: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int) {
// Call with just the named parameter
let c = CString::from_slice(b"Hello World\n");
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
// Call with variable number of arguments
"%d %f %c %s\n".with_c_str(|c| {
check("42 42.500000 a %d %f %c %s\n\n", |s| {
p(s, c, 42i, 42.5f64, 'a' as c_int, c);
})
let c = CString::from_slice(b"%d %f %c %s\n");
check("42 42.500000 a %d %f %c %s\n\n", |s| {
sprintf(s, c.as_ptr(), 42i, 42.5f64, 'a' as c_int, c.as_ptr());
});
}