]> git.lizzy.rs Git - rust.git/commitdiff
Replace str::raw::buf_as_slice with c_str_to_static_slice. Close #3843.
authorBen Blum <bblum@andrew.cmu.edu>
Mon, 10 Jun 2013 21:27:18 +0000 (17:27 -0400)
committerBen Blum <bblum@andrew.cmu.edu>
Mon, 10 Jun 2013 21:34:28 +0000 (17:34 -0400)
src/libstd/rt/uv/mod.rs
src/libstd/str.rs

index 10c8b84bc512aadb197c5535c083f292dfeabde0..dd66a76eead91777ea2130c5070b8bce3d8ca3e3 100644 (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 @@ unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str {
 
         // 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,
index 8cd69f32e4969b1dcdbc112edd1a8148ea8e6014..f270964c3b5697224d3e4121aa50a3fa85266bc6 100644 (file)
@@ -1396,12 +1396,19 @@ pub unsafe fn from_bytes_with_null<'a>(v: &'a [u8]) -> &'a str {
     /// 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)
     }
 
     /**