Replace str::raw::buf_as_slice with c_str_to_static_slice. Close #3843.

This commit is contained in:
Ben Blum 2013-06-10 17:27:18 -04:00
parent 1310212c27
commit 967c7d828a
2 changed files with 13 additions and 20 deletions

View File

@ -238,20 +238,6 @@ pub fn last_uv_error<H, W: Watcher + NativeHandle<*H>>(watcher: &W) -> UvError {
}
pub fn uv_error_to_io_error(uverr: UvError) -> IoError {
// XXX: Could go in str::raw
unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
let s = s as *u8;
let mut (curr, len) = (s, 0u);
while *curr != 0u8 {
len += 1u;
curr = ptr::offset(s, len);
}
str::raw::buf_as_slice(s, len, |d| cast::transmute(d))
}
unsafe {
// Importing error constants
use rt::uv::uvll::*;
@ -259,7 +245,7 @@ pub fn uv_error_to_io_error(uverr: UvError) -> IoError {
// uv error descriptions are static
let c_desc = uvll::strerror(&*uverr);
let desc = c_str_to_static_slice(c_desc);
let desc = str::raw::c_str_to_static_slice(c_desc);
let kind = match uverr.code {
UNKNOWN => OtherIoError,

View File

@ -1396,12 +1396,19 @@ pub mod raw {
/// Converts a byte to a string.
pub unsafe fn from_byte(u: u8) -> ~str { raw::from_bytes([u]) }
/// Form a slice from a *u8 buffer of the given length without copying.
pub unsafe fn buf_as_slice<T>(buf: *u8, len: uint,
f: &fn(v: &str) -> T) -> T {
let v = (buf, len + 1);
/// Form a slice from a C string. Unsafe because the caller must ensure the
/// C string has the static lifetime, or else the return value may be
/// invalidated later.
pub unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
let s = s as *u8;
let mut (curr, len) = (s, 0u);
while *curr != 0u8 {
len += 1u;
curr = ptr::offset(s, len);
}
let v = (s, len + 1);
assert!(is_utf8(::cast::transmute(v)));
f(::cast::transmute(v))
::cast::transmute(v)
}
/**