about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2013-06-10 17:27:18 -0400
committerBen Blum <bblum@andrew.cmu.edu>2013-06-10 17:34:28 -0400
commit967c7d828aacd9f788973c05acabafc194bd5772 (patch)
treee4fff46ac86a96981d4b494f80b3dd19dbe15f30
parent1310212c27c1c294e1f907b05a225440c987a912 (diff)
downloadrust-967c7d828aacd9f788973c05acabafc194bd5772.tar.gz
rust-967c7d828aacd9f788973c05acabafc194bd5772.zip
Replace str::raw::buf_as_slice with c_str_to_static_slice. Close #3843.
-rw-r--r--src/libstd/rt/uv/mod.rs16
-rw-r--r--src/libstd/str.rs17
2 files changed, 13 insertions, 20 deletions
diff --git a/src/libstd/rt/uv/mod.rs b/src/libstd/rt/uv/mod.rs
index 10c8b84bc51..dd66a76eead 100644
--- a/src/libstd/rt/uv/mod.rs
+++ b/src/libstd/rt/uv/mod.rs
@@ -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,
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 8cd69f32e49..f270964c3b5 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -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)
     }
 
     /**