rollup merge of #20507: alexcrichton/issue-20444

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 2015-01-05 18:37:22 -08:00
commit 25d5a3a194
59 changed files with 1018 additions and 1998 deletions

View file

@ -20,8 +20,8 @@ pub use self::imp::Lock;
#[cfg(unix)]
mod imp {
use std::ffi::CString;
use libc;
use std::c_str::ToCStr;
#[cfg(target_os = "linux")]
mod os {
@ -111,9 +111,11 @@ mod imp {
impl Lock {
pub fn new(p: &Path) -> Lock {
let fd = p.with_c_str(|s| unsafe {
libc::open(s, libc::O_RDWR | libc::O_CREAT, libc::S_IRWXU)
});
let buf = CString::from_slice(p.as_vec());
let fd = unsafe {
libc::open(buf.as_ptr(), libc::O_RDWR | libc::O_CREAT,
libc::S_IRWXU)
};
assert!(fd > 0);
let flock = os::flock {
l_start: 0,

View file

@ -29,7 +29,7 @@
use libc;
use std::ascii::AsciiExt;
use std::c_str::ToCStr;
use std::ffi::CString;
use std::cell::{RefCell, Cell};
use std::collections::HashMap;
use std::fmt;
@ -215,7 +215,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
let id = id.as_ref().map(|a| a.as_slice());
s.push_str(highlight::highlight(text.as_slice(), None, id)
.as_slice());
let output = s.to_c_str();
let output = CString::from_vec(s.into_bytes());
hoedown_buffer_puts(ob, output.as_ptr());
})
}
@ -224,15 +224,16 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
extern fn header(ob: *mut hoedown_buffer, text: *const hoedown_buffer,
level: libc::c_int, opaque: *mut libc::c_void) {
// hoedown does this, we may as well too
"\n".with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
unsafe { hoedown_buffer_puts(ob, "\n\0".as_ptr() as *const _); }
// Extract the text provided
let s = if text.is_null() {
"".to_string()
} else {
unsafe {
String::from_raw_buf_len((*text).data, (*text).size as uint)
}
let s = unsafe {
slice::from_raw_buf(&(*text).data, (*text).size as uint)
};
str::from_utf8(s).unwrap().to_string()
};
// Transform the contents of the header into a hyphenated string
@ -273,7 +274,8 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
format!("{} ", sec)
});
text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
let text = CString::from_vec(text.into_bytes());
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
}
reset_headers();