Skip to content

Commit

Permalink
Deprecated str::raw::from_c_str
Browse files Browse the repository at this point in the history
Use `string::raw::from_buf` instead

[breaking-change]
  • Loading branch information
aochagavia authored and alexcrichton committed Jul 24, 2014
1 parent ba707fb commit eacc5d7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
15 changes: 3 additions & 12 deletions src/libcollections/str.rs
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1348,16 +1349,6 @@ mod tests {
[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
Expand Down
16 changes: 7 additions & 9 deletions src/libcoretest/ptr.rs
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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;
});
Expand All @@ -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();
});
}
}
Expand All @@ -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();
});
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/middle/trans/type_.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}
}
6 changes: 3 additions & 3 deletions src/librustuv/lib.rs
Expand Up @@ -55,14 +55,14 @@ 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;
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;
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/os.rs
Expand Up @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/const-str-ptr.rs
Expand Up @@ -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());
}
}

0 comments on commit eacc5d7

Please sign in to comment.