From eacc5d779fe4080dd2b45e035ca2af099b8b906d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sat, 19 Jul 2014 12:08:34 +0200 Subject: [PATCH] Deprecated `str::raw::from_c_str` Use `string::raw::from_buf` instead [breaking-change] --- src/libcollections/str.rs | 15 +++------------ src/libcoretest/ptr.rs | 16 +++++++--------- src/librustc/middle/trans/type_.rs | 11 +++++------ src/librustuv/lib.rs | 6 +++--- src/libstd/os.rs | 2 +- src/test/run-pass/const-str-ptr.rs | 2 +- 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 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// 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 unsafe fn from_buf_len(buf: *const u8, len: uint) -> String { 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; @@ -1348,16 +1349,6 @@ fn test_is_utf16() { [0x50d7, 0xd824, 0x5010, 0xb369, 0x22ea]); } - #[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 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 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 @@ pub fn find_type(&self, s: &str) -> Option { 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 @@ pub fn types_to_str(&self, tys: &[Type]) -> String { 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 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::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 @@ pub fn name(&self) -> String { 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 @@ pub fn desc(&self) -> String { 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 @@ fn __xpg_strerror_r(errnum: c_int, 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()); } }