about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAdolfo OchagavĂ­a <aochagavia92@gmail.com>2014-07-19 12:08:34 +0200
committerAlex Crichton <alex@alexcrichton.com>2014-07-24 07:25:43 -0700
commiteacc5d779fe4080dd2b45e035ca2af099b8b906d (patch)
tree6b895ebcb49f2596add96c98eb10562ebe112e5a
parentba707fb3a0735c0ca19baac426b88218c656a34b (diff)
downloadrust-eacc5d779fe4080dd2b45e035ca2af099b8b906d.tar.gz
rust-eacc5d779fe4080dd2b45e035ca2af099b8b906d.zip
Deprecated `str::raw::from_c_str`
Use `string::raw::from_buf` instead

[breaking-change]
-rw-r--r--src/libcollections/str.rs15
-rw-r--r--src/libcoretest/ptr.rs16
-rw-r--r--src/librustc/middle/trans/type_.rs11
-rw-r--r--src/librustuv/lib.rs6
-rw-r--r--src/libstd/os.rs2
-rw-r--r--src/test/run-pass/const-str-ptr.rs2
6 files changed, 20 insertions, 32 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs
index ae1fb87ef7f..69372b6d89c 100644
--- a/src/libcollections/str.rs
+++ b/src/libcollections/str.rs
@@ -555,9 +555,9 @@ impl<'a> fmt::Show for MaybeOwned<'a> {
 
 /// Unsafe operations
 pub mod raw {
-    use core::prelude::*;
     use core::mem;
     use core::raw::Slice;
+    use core::ptr::RawPtr;
 
     use string::String;
     use vec::Vec;
@@ -577,7 +577,8 @@ pub mod raw {
         result
     }
 
-    /// Create a Rust string from a null-terminated C string
+    /// Deprecated. Use `CString::as_str().unwrap().to_string()`
+    #[deprecated = "Use CString::as_str().unwrap().to_string()"]
     pub unsafe fn from_c_str(c_string: *const i8) -> String {
         let mut buf = String::new();
         let mut len = 0;
@@ -1349,16 +1350,6 @@ mod tests {
     }
 
     #[test]
-    fn test_raw_from_c_str() {
-        unsafe {
-            let a = vec![65, 65, 65, 65, 65, 65, 65, 0];
-            let b = a.as_ptr();
-            let c = raw::from_c_str(b);
-            assert_eq!(c, String::from_str("AAAAAAA"));
-        }
-    }
-
-    #[test]
     fn test_as_bytes() {
         // no null
         let v = [
diff --git a/src/libcoretest/ptr.rs b/src/libcoretest/ptr.rs
index 2a4ef5e275d..9058ae56c45 100644
--- a/src/libcoretest/ptr.rs
+++ b/src/libcoretest/ptr.rs
@@ -11,8 +11,8 @@
 use core::ptr::*;
 use libc::c_char;
 use core::mem;
-use std::str;
 use libc;
+use std::c_str::CString;
 
 #[test]
 fn test() {
@@ -186,9 +186,8 @@ fn test_ptr_array_each_with_len() {
         let mut ctr = 0;
         let mut iteration_count = 0;
         array_each_with_len(arr.as_ptr(), arr.len(), |e| {
-                let actual = str::raw::from_c_str(e);
-                let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
-                assert_eq!(actual.as_slice(), expected.as_slice());
+                let actual = CString::new(e, false);
+                assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
                 ctr += 1;
                 iteration_count += 1;
             });
@@ -217,9 +216,8 @@ fn test_ptr_array_each() {
         let mut ctr = 0u;
         let mut iteration_count = 0u;
         array_each(arr_ptr, |e| {
-                let actual = str::raw::from_c_str(e);
-                let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
-                assert_eq!(actual.as_slice(), expected.as_slice());
+                let actual = CString::new(e, false);
+                assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
                 ctr += 1;
                 iteration_count += 1;
             });
@@ -232,7 +230,7 @@ fn test_ptr_array_each() {
 fn test_ptr_array_each_with_len_null_ptr() {
     unsafe {
         array_each_with_len(0 as *const *const libc::c_char, 1, |e| {
-            str::raw::from_c_str(e);
+            CString::new(e, false).as_str().unwrap();
         });
     }
 }
@@ -241,7 +239,7 @@ fn test_ptr_array_each_with_len_null_ptr() {
 fn test_ptr_array_each_null_ptr() {
     unsafe {
         array_each(0 as *const *const libc::c_char, |e| {
-            str::raw::from_c_str(e);
+            CString::new(e, false).as_str().unwrap();
         });
     }
 }
diff --git a/src/librustc/middle/trans/type_.rs b/src/librustc/middle/trans/type_.rs
index 573965108ad..3833d4cd0c8 100644
--- a/src/librustc/middle/trans/type_.rs
+++ b/src/librustc/middle/trans/type_.rs
@@ -19,11 +19,10 @@ use middle::trans::context::CrateContext;
 use syntax::ast;
 use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel};
 
-use std::c_str::ToCStr;
+use std::c_str::{CString, ToCStr};
 use std::mem;
 use std::cell::RefCell;
 use std::collections::HashMap;
-use std::str::raw::from_c_str;
 
 use libc::{c_uint, c_void, free};
 
@@ -334,9 +333,9 @@ impl TypeNames {
     pub fn type_to_string(&self, ty: Type) -> String {
         unsafe {
             let s = llvm::LLVMTypeToString(ty.to_ref());
-            let ret = from_c_str(s);
+            let ret = CString::new(s, false).as_str().unwrap().to_string();
             free(s as *mut c_void);
-            ret.to_string()
+            ret
         }
     }
 
@@ -348,9 +347,9 @@ impl TypeNames {
     pub fn val_to_string(&self, val: ValueRef) -> String {
         unsafe {
             let s = llvm::LLVMValueToString(val);
-            let ret = from_c_str(s);
+            let ret = CString::new(s, false).as_str().unwrap().to_string();
             free(s as *mut c_void);
-            ret.to_string()
+            ret
         }
     }
 }
diff --git a/src/librustuv/lib.rs b/src/librustuv/lib.rs
index 2c2e134d882..dc43a68e64d 100644
--- a/src/librustuv/lib.rs
+++ b/src/librustuv/lib.rs
@@ -55,6 +55,7 @@ extern crate libc;
 extern crate alloc;
 
 use libc::{c_int, c_void};
+use std::c_str::CString;
 use std::fmt;
 use std::mem;
 use std::ptr;
@@ -62,7 +63,6 @@ use std::rt::local::Local;
 use std::rt::rtio;
 use std::rt::rtio::{IoResult, IoError};
 use std::rt::task::{BlockedTask, Task};
-use std::str::raw::from_c_str;
 use std::task;
 
 pub use self::async::AsyncWatcher;
@@ -363,7 +363,7 @@ impl UvError {
             let inner = match self { &UvError(a) => a };
             let name_str = uvll::uv_err_name(inner);
             assert!(name_str.is_not_null());
-            from_c_str(name_str).to_string()
+            CString::new(name_str, false).as_str().unwrap().to_string()
         }
     }
 
@@ -372,7 +372,7 @@ impl UvError {
             let inner = match self { &UvError(a) => a };
             let desc_str = uvll::uv_strerror(inner);
             assert!(desc_str.is_not_null());
-            from_c_str(desc_str).to_string()
+            CString::new(desc_str, false).as_str().unwrap().to_string()
         }
     }
 
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index ca76be40cb5..1b61dec99b4 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -998,7 +998,7 @@ pub fn error_string(errnum: uint) -> String {
                 fail!("strerror_r failure");
             }
 
-            str::raw::from_c_str(p as *const c_char).into_string()
+            ::c_str::CString::new(p as *const c_char, false).as_str().unwrap().to_string()
         }
     }
 
diff --git a/src/test/run-pass/const-str-ptr.rs b/src/test/run-pass/const-str-ptr.rs
index 6790e237e26..57d262d3268 100644
--- a/src/test/run-pass/const-str-ptr.rs
+++ b/src/test/run-pass/const-str-ptr.rs
@@ -24,6 +24,6 @@ pub fn main() {
         assert!(*(&B[0] as *const u8) == A[0]);
 
         let bar = str::raw::from_utf8(A).to_c_str();
-        assert_eq!(str::raw::from_c_str(bar.as_ptr()), "hi".to_string());
+        assert_eq!(bar.as_str(), "hi".to_c_str().as_str());
     }
 }