Deprecated str::raw::from_c_str

Use `string::raw::from_buf` instead

[breaking-change]
This commit is contained in:
Adolfo Ochagavía 2014-07-19 12:08:34 +02:00 committed by Alex Crichton
parent ba707fb3a0
commit eacc5d779f
6 changed files with 20 additions and 32 deletions

View File

@ -555,9 +555,9 @@ impl<'a> fmt::Show for MaybeOwned<'a> {
/// Unsafe operations /// Unsafe operations
pub mod raw { pub mod raw {
use core::prelude::*;
use core::mem; use core::mem;
use core::raw::Slice; use core::raw::Slice;
use core::ptr::RawPtr;
use string::String; use string::String;
use vec::Vec; use vec::Vec;
@ -577,7 +577,8 @@ pub mod raw {
result 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 { pub unsafe fn from_c_str(c_string: *const i8) -> String {
let mut buf = String::new(); let mut buf = String::new();
let mut len = 0; let mut len = 0;
@ -1348,16 +1349,6 @@ mod tests {
[0x50d7, 0xd824, 0x5010, 0xb369, 0x22ea]); [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] #[test]
fn test_as_bytes() { fn test_as_bytes() {
// no null // no null

View File

@ -11,8 +11,8 @@
use core::ptr::*; use core::ptr::*;
use libc::c_char; use libc::c_char;
use core::mem; use core::mem;
use std::str;
use libc; use libc;
use std::c_str::CString;
#[test] #[test]
fn test() { fn test() {
@ -186,9 +186,8 @@ fn test_ptr_array_each_with_len() {
let mut ctr = 0; let mut ctr = 0;
let mut iteration_count = 0; let mut iteration_count = 0;
array_each_with_len(arr.as_ptr(), arr.len(), |e| { array_each_with_len(arr.as_ptr(), arr.len(), |e| {
let actual = str::raw::from_c_str(e); let actual = CString::new(e, false);
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr()); assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
assert_eq!(actual.as_slice(), expected.as_slice());
ctr += 1; ctr += 1;
iteration_count += 1; iteration_count += 1;
}); });
@ -217,9 +216,8 @@ fn test_ptr_array_each() {
let mut ctr = 0u; let mut ctr = 0u;
let mut iteration_count = 0u; let mut iteration_count = 0u;
array_each(arr_ptr, |e| { array_each(arr_ptr, |e| {
let actual = str::raw::from_c_str(e); let actual = CString::new(e, false);
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr()); assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
assert_eq!(actual.as_slice(), expected.as_slice());
ctr += 1; ctr += 1;
iteration_count += 1; iteration_count += 1;
}); });
@ -232,7 +230,7 @@ fn test_ptr_array_each() {
fn test_ptr_array_each_with_len_null_ptr() { fn test_ptr_array_each_with_len_null_ptr() {
unsafe { unsafe {
array_each_with_len(0 as *const *const libc::c_char, 1, |e| { 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() { fn test_ptr_array_each_null_ptr() {
unsafe { unsafe {
array_each(0 as *const *const libc::c_char, |e| { array_each(0 as *const *const libc::c_char, |e| {
str::raw::from_c_str(e); CString::new(e, false).as_str().unwrap();
}); });
} }
} }

View File

@ -19,11 +19,10 @@ use middle::trans::context::CrateContext;
use syntax::ast; use syntax::ast;
use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel}; 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::mem;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::raw::from_c_str;
use libc::{c_uint, c_void, free}; use libc::{c_uint, c_void, free};
@ -334,9 +333,9 @@ impl TypeNames {
pub fn type_to_string(&self, ty: Type) -> String { pub fn type_to_string(&self, ty: Type) -> String {
unsafe { unsafe {
let s = llvm::LLVMTypeToString(ty.to_ref()); 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); 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 { pub fn val_to_string(&self, val: ValueRef) -> String {
unsafe { unsafe {
let s = llvm::LLVMValueToString(val); 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); free(s as *mut c_void);
ret.to_string() ret
} }
} }
} }

View File

@ -55,6 +55,7 @@ extern crate libc;
extern crate alloc; extern crate alloc;
use libc::{c_int, c_void}; use libc::{c_int, c_void};
use std::c_str::CString;
use std::fmt; use std::fmt;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
@ -62,7 +63,6 @@ use std::rt::local::Local;
use std::rt::rtio; use std::rt::rtio;
use std::rt::rtio::{IoResult, IoError}; use std::rt::rtio::{IoResult, IoError};
use std::rt::task::{BlockedTask, Task}; use std::rt::task::{BlockedTask, Task};
use std::str::raw::from_c_str;
use std::task; use std::task;
pub use self::async::AsyncWatcher; pub use self::async::AsyncWatcher;
@ -363,7 +363,7 @@ impl UvError {
let inner = match self { &UvError(a) => a }; let inner = match self { &UvError(a) => a };
let name_str = uvll::uv_err_name(inner); let name_str = uvll::uv_err_name(inner);
assert!(name_str.is_not_null()); 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 inner = match self { &UvError(a) => a };
let desc_str = uvll::uv_strerror(inner); let desc_str = uvll::uv_strerror(inner);
assert!(desc_str.is_not_null()); assert!(desc_str.is_not_null());
from_c_str(desc_str).to_string() CString::new(desc_str, false).as_str().unwrap().to_string()
} }
} }

View File

@ -998,7 +998,7 @@ pub fn error_string(errnum: uint) -> String {
fail!("strerror_r failure"); 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()
} }
} }

View File

@ -24,6 +24,6 @@ pub fn main() {
assert!(*(&B[0] as *const u8) == A[0]); assert!(*(&B[0] as *const u8) == A[0]);
let bar = str::raw::from_utf8(A).to_c_str(); 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());
} }
} }