Skip to content

Commit 967c7d8

Browse files
committed
Replace str::raw::buf_as_slice with c_str_to_static_slice. Close #3843.
1 parent 1310212 commit 967c7d8

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/libstd/rt/uv/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,14 @@ pub fn last_uv_error<H, W: Watcher + NativeHandle<*H>>(watcher: &W) -> UvError {
238238
}
239239
240240
pub fn uv_error_to_io_error(uverr: UvError) -> IoError {
241-
242-
// XXX: Could go in str::raw
243-
unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
244-
let s = s as *u8;
245-
let mut (curr, len) = (s, 0u);
246-
while *curr != 0u8 {
247-
len += 1u;
248-
curr = ptr::offset(s, len);
249-
}
250-
251-
str::raw::buf_as_slice(s, len, |d| cast::transmute(d))
252-
}
253-
254-
255241
unsafe {
256242
// Importing error constants
257243
use rt::uv::uvll::*;
258244
use rt::io::*;
259245
260246
// uv error descriptions are static
261247
let c_desc = uvll::strerror(&*uverr);
262-
let desc = c_str_to_static_slice(c_desc);
248+
let desc = str::raw::c_str_to_static_slice(c_desc);
263249
264250
let kind = match uverr.code {
265251
UNKNOWN => OtherIoError,

src/libstd/str.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,12 +1396,19 @@ pub mod raw {
13961396
/// Converts a byte to a string.
13971397
pub unsafe fn from_byte(u: u8) -> ~str { raw::from_bytes([u]) }
13981398

1399-
/// Form a slice from a *u8 buffer of the given length without copying.
1400-
pub unsafe fn buf_as_slice<T>(buf: *u8, len: uint,
1401-
f: &fn(v: &str) -> T) -> T {
1402-
let v = (buf, len + 1);
1399+
/// Form a slice from a C string. Unsafe because the caller must ensure the
1400+
/// C string has the static lifetime, or else the return value may be
1401+
/// invalidated later.
1402+
pub unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
1403+
let s = s as *u8;
1404+
let mut (curr, len) = (s, 0u);
1405+
while *curr != 0u8 {
1406+
len += 1u;
1407+
curr = ptr::offset(s, len);
1408+
}
1409+
let v = (s, len + 1);
14031410
assert!(is_utf8(::cast::transmute(v)));
1404-
f(::cast::transmute(v))
1411+
::cast::transmute(v)
14051412
}
14061413

14071414
/**

0 commit comments

Comments
 (0)