Skip to content

Commit 1a4b888

Browse files
committed
Deprecated str::raw::from_c_str
Use `string::raw::from_buf` instead [breaking-change]
1 parent 347383b commit 1a4b888

File tree

6 files changed

+20
-32
lines changed

6 files changed

+20
-32
lines changed

src/libcollections/str.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,9 @@ impl<'a> fmt::Show for MaybeOwned<'a> {
555555

556556
/// Unsafe operations
557557
pub mod raw {
558-
use core::prelude::*;
559558
use core::mem;
560559
use core::raw::Slice;
560+
use core::ptr::RawPtr;
561561

562562
use string::String;
563563
use vec::Vec;
@@ -575,7 +575,8 @@ pub mod raw {
575575
result
576576
}
577577

578-
/// Create a Rust string from a null-terminated C string
578+
/// Deprecated. Use `CString::as_str().unwrap().to_string()`
579+
#[deprecated = "Use CString::as_str().unwrap().to_string()"]
579580
pub unsafe fn from_c_str(c_string: *const i8) -> String {
580581
let mut buf = String::new();
581582
let mut len = 0;
@@ -1346,16 +1347,6 @@ mod tests {
13461347
[0x50d7, 0xd824, 0x5010, 0xb369, 0x22ea]);
13471348
}
13481349

1349-
#[test]
1350-
fn test_raw_from_c_str() {
1351-
unsafe {
1352-
let a = vec![65, 65, 65, 65, 65, 65, 65, 0];
1353-
let b = a.as_ptr();
1354-
let c = raw::from_c_str(b);
1355-
assert_eq!(c, String::from_str("AAAAAAA"));
1356-
}
1357-
}
1358-
13591350
#[test]
13601351
fn test_as_bytes() {
13611352
// no null

src/libcoretest/ptr.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use core::ptr::*;
1212
use libc::c_char;
1313
use core::mem;
14-
use std::str;
1514
use libc;
15+
use std::c_str::CString;
1616

1717
#[test]
1818
fn test() {
@@ -186,9 +186,8 @@ fn test_ptr_array_each_with_len() {
186186
let mut ctr = 0;
187187
let mut iteration_count = 0;
188188
array_each_with_len(arr.as_ptr(), arr.len(), |e| {
189-
let actual = str::raw::from_c_str(e);
190-
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
191-
assert_eq!(actual.as_slice(), expected.as_slice());
189+
let actual = CString::new(e, false);
190+
assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
192191
ctr += 1;
193192
iteration_count += 1;
194193
});
@@ -217,9 +216,8 @@ fn test_ptr_array_each() {
217216
let mut ctr = 0u;
218217
let mut iteration_count = 0u;
219218
array_each(arr_ptr, |e| {
220-
let actual = str::raw::from_c_str(e);
221-
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
222-
assert_eq!(actual.as_slice(), expected.as_slice());
219+
let actual = CString::new(e, false);
220+
assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
223221
ctr += 1;
224222
iteration_count += 1;
225223
});
@@ -232,7 +230,7 @@ fn test_ptr_array_each() {
232230
fn test_ptr_array_each_with_len_null_ptr() {
233231
unsafe {
234232
array_each_with_len(0 as *const *const libc::c_char, 1, |e| {
235-
str::raw::from_c_str(e);
233+
CString::new(e, false).as_str().unwrap();
236234
});
237235
}
238236
}
@@ -241,7 +239,7 @@ fn test_ptr_array_each_with_len_null_ptr() {
241239
fn test_ptr_array_each_null_ptr() {
242240
unsafe {
243241
array_each(0 as *const *const libc::c_char, |e| {
244-
str::raw::from_c_str(e);
242+
CString::new(e, false).as_str().unwrap();
245243
});
246244
}
247245
}

src/librustc/middle/trans/type_.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ use middle::trans::context::CrateContext;
1919
use syntax::ast;
2020
use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel};
2121

22-
use std::c_str::ToCStr;
22+
use std::c_str::{CString, ToCStr};
2323
use std::mem;
2424
use std::cell::RefCell;
2525
use std::collections::HashMap;
26-
use std::str::raw::from_c_str;
2726

2827
use libc::{c_uint, c_void, free};
2928

@@ -334,9 +333,9 @@ impl TypeNames {
334333
pub fn type_to_string(&self, ty: Type) -> String {
335334
unsafe {
336335
let s = llvm::LLVMTypeToString(ty.to_ref());
337-
let ret = from_c_str(s);
336+
let ret = CString::new(s, false).as_str().unwrap().to_string();
338337
free(s as *mut c_void);
339-
ret.to_string()
338+
ret
340339
}
341340
}
342341

@@ -348,9 +347,9 @@ impl TypeNames {
348347
pub fn val_to_string(&self, val: ValueRef) -> String {
349348
unsafe {
350349
let s = llvm::LLVMValueToString(val);
351-
let ret = from_c_str(s);
350+
let ret = CString::new(s, false).as_str().unwrap().to_string();
352351
free(s as *mut c_void);
353-
ret.to_string()
352+
ret
354353
}
355354
}
356355
}

src/librustuv/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ extern crate libc;
5555
extern crate alloc;
5656

5757
use libc::{c_int, c_void};
58+
use std::c_str::CString;
5859
use std::fmt;
5960
use std::mem;
6061
use std::ptr;
6162
use std::rt::local::Local;
6263
use std::rt::rtio;
6364
use std::rt::rtio::{IoResult, IoError};
6465
use std::rt::task::{BlockedTask, Task};
65-
use std::str::raw::from_c_str;
6666
use std::task;
6767

6868
pub use self::async::AsyncWatcher;
@@ -363,7 +363,7 @@ impl UvError {
363363
let inner = match self { &UvError(a) => a };
364364
let name_str = uvll::uv_err_name(inner);
365365
assert!(name_str.is_not_null());
366-
from_c_str(name_str).to_string()
366+
CString::new(name_str, false).as_str().unwrap().to_string()
367367
}
368368
}
369369

@@ -372,7 +372,7 @@ impl UvError {
372372
let inner = match self { &UvError(a) => a };
373373
let desc_str = uvll::uv_strerror(inner);
374374
assert!(desc_str.is_not_null());
375-
from_c_str(desc_str).to_string()
375+
CString::new(desc_str, false).as_str().unwrap().to_string()
376376
}
377377
}
378378

src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ pub fn error_string(errnum: uint) -> String {
998998
fail!("strerror_r failure");
999999
}
10001000

1001-
str::raw::from_c_str(p as *const c_char).into_string()
1001+
::c_str::CString::new(p as *const c_char, false).as_str().unwrap().to_string()
10021002
}
10031003
}
10041004

src/test/run-pass/const-str-ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ pub fn main() {
2424
assert!(*(&B[0] as *const u8) == A[0]);
2525

2626
let bar = str::raw::from_utf8(A).to_c_str();
27-
assert_eq!(str::raw::from_c_str(bar.as_ptr()), "hi".to_string());
27+
assert_eq!(bar.as_str(), "hi".to_c_str().as_str());
2828
}
2929
}

0 commit comments

Comments
 (0)