1
Fork 0

std: Align raw modules with unsafe conventions

This commit is an implementation of [RFC 240][rfc] when applied to the standard
library. It primarily deprecates the entirety of `string::raw`, `vec::raw`,
`slice::raw`, and `str::raw` in favor of associated functions, methods, and
other free functions. The detailed renaming is:

* slice::raw::buf_as_slice => slice::with_raw_buf
* slice::raw::mut_buf_as_slice => slice::with_raw_mut_buf
* slice::shift_ptr => deprecated with no replacement
* slice::pop_ptr => deprecated with no replacement
* str::raw::from_utf8 => str::from_utf8_unchecked
* str::raw::c_str_to_static_slice => str::from_c_str
* str::raw::slice_bytes => deprecated for slice_unchecked (slight semantic diff)
* str::raw::slice_unchecked => str.slice_unchecked
* string::raw::from_parts => String::from_raw_parts
* string::raw::from_buf_len => String::from_raw_buf_len
* string::raw::from_buf => String::from_raw_buf
* string::raw::from_utf8 => String::from_utf8_unchecked
* vec::raw::from_buf => Vec::from_raw_buf

All previous functions exist in their `#[deprecated]` form, and the deprecation
messages indicate how to migrate to the newer variants.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0240-unsafe-api-location.md
[breaking-change]

Closes #17863
This commit is contained in:
Alex Crichton 2014-11-20 10:11:15 -08:00
parent 399ff259e1
commit 8ca27a633e
15 changed files with 304 additions and 208 deletions

View file

@ -21,7 +21,7 @@ use mem;
use option::{Option, Some, None};
use slice::{SlicePrelude, AsSlice};
use str::{Str, StrPrelude};
use string::{mod, String, IntoString};
use string::{String, IntoString};
use vec::Vec;
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
@ -328,9 +328,7 @@ impl AsciiStr for [Ascii] {
impl IntoString for Vec<Ascii> {
#[inline]
fn into_string(self) -> String {
unsafe {
string::raw::from_utf8(self.into_bytes())
}
unsafe { String::from_utf8_unchecked(self.into_bytes()) }
}
}
@ -391,13 +389,13 @@ impl AsciiExt<String> for str {
#[inline]
fn to_ascii_upper(&self) -> String {
// Vec<u8>::to_ascii_upper() preserves the UTF-8 invariant.
unsafe { string::raw::from_utf8(self.as_bytes().to_ascii_upper()) }
unsafe { String::from_utf8_unchecked(self.as_bytes().to_ascii_upper()) }
}
#[inline]
fn to_ascii_lower(&self) -> String {
// Vec<u8>::to_ascii_lower() preserves the UTF-8 invariant.
unsafe { string::raw::from_utf8(self.as_bytes().to_ascii_lower()) }
unsafe { String::from_utf8_unchecked(self.as_bytes().to_ascii_lower()) }
}
#[inline]
@ -410,13 +408,13 @@ impl OwnedAsciiExt for String {
#[inline]
fn into_ascii_upper(self) -> String {
// Vec<u8>::into_ascii_upper() preserves the UTF-8 invariant.
unsafe { string::raw::from_utf8(self.into_bytes().into_ascii_upper()) }
unsafe { String::from_utf8_unchecked(self.into_bytes().into_ascii_upper()) }
}
#[inline]
fn into_ascii_lower(self) -> String {
// Vec<u8>::into_ascii_lower() preserves the UTF-8 invariant.
unsafe { string::raw::from_utf8(self.into_bytes().into_ascii_lower()) }
unsafe { String::from_utf8_unchecked(self.into_bytes().into_ascii_lower()) }
}
}