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:
parent
1f732ef53d
commit
ec7a50d20d
59 changed files with 1018 additions and 1998 deletions
|
|
@ -320,30 +320,6 @@ impl String {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a `String` from a null-terminated `*const u8` buffer.
|
||||
///
|
||||
/// This function is unsafe because we dereference memory until we find the
|
||||
/// NUL character, which is not guaranteed to be present. Additionally, the
|
||||
/// slice is not checked to see whether it contains valid UTF-8
|
||||
#[unstable = "just renamed from `mod raw`"]
|
||||
pub unsafe fn from_raw_buf(buf: *const u8) -> String {
|
||||
String::from_str(str::from_c_str(buf as *const i8))
|
||||
}
|
||||
|
||||
/// Creates a `String` from a `*const u8` buffer of the given length.
|
||||
///
|
||||
/// This function is unsafe because it blindly assumes the validity of the
|
||||
/// pointer `buf` for `len` bytes of memory. This function will copy the
|
||||
/// memory from `buf` into a new allocation (owned by the returned
|
||||
/// `String`).
|
||||
///
|
||||
/// This function is also unsafe because it does not validate that the
|
||||
/// buffer is valid UTF-8 encoded data.
|
||||
#[unstable = "just renamed from `mod raw`"]
|
||||
pub unsafe fn from_raw_buf_len(buf: *const u8, len: uint) -> String {
|
||||
String::from_utf8_unchecked(Vec::from_raw_buf(buf, len))
|
||||
}
|
||||
|
||||
/// Converts a vector of bytes to a new `String` without checking if
|
||||
/// it contains valid UTF-8. This is unsafe because it assumes that
|
||||
/// the UTF-8-ness of the vector has already been validated.
|
||||
|
|
@ -1126,24 +1102,6 @@ mod tests {
|
|||
String::from_str("\u{FFFD}𐒋\u{FFFD}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_buf_len() {
|
||||
unsafe {
|
||||
let a = vec![65u8, 65, 65, 65, 65, 65, 65, 0];
|
||||
assert_eq!(String::from_raw_buf_len(a.as_ptr(), 3), String::from_str("AAA"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_buf() {
|
||||
unsafe {
|
||||
let a = vec![65, 65, 65, 65, 65, 65, 65, 0];
|
||||
let b = a.as_ptr();
|
||||
let c = String::from_raw_buf(b);
|
||||
assert_eq!(c, String::from_str("AAAAAAA"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_bytes() {
|
||||
let mut s = String::from_str("ABC");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue