Skip to content

Commit 20417eb

Browse files
committed
core: Move unsafe conversions to str::unsafe
1 parent 1a40aa0 commit 20417eb

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

src/libcore/os.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn fill_charp_buf(f: fn(*mutable c_char, size_t) -> bool)
6666
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
6767
vec::as_mut_buf(buf) { |b|
6868
if f(b, tmpbuf_sz as size_t) unsafe {
69-
some(str::from_buf(b as *u8))
69+
some(str::unsafe::from_buf(b as *u8))
7070
} else {
7171
none
7272
}
@@ -125,7 +125,7 @@ fn getenv(n: str) -> option<str> unsafe {
125125
option::none::<str>
126126
} else {
127127
let s = unsafe::reinterpret_cast(s);
128-
option::some::<str>(str::from_buf(s))
128+
option::some::<str>(str::unsafe::from_buf(s))
129129
};
130130
}
131131

src/libcore/str.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export
1313
from_byte,
1414
from_char,
1515
from_chars,
16-
from_buf,
17-
from_buf_len,
18-
from_c_str,
19-
from_c_str_len,
2016
push_char,
2117
concat,
2218
connect,
@@ -185,40 +181,6 @@ fn from_chars(chs: [char]) -> str {
185181
ret buf;
186182
}
187183

188-
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
189-
unsafe fn from_buf(buf: *u8) -> str {
190-
let mut curr = buf, i = 0u;
191-
while *curr != 0u8 {
192-
i += 1u;
193-
curr = ptr::offset(buf, i);
194-
}
195-
ret from_buf_len(buf, i);
196-
}
197-
198-
#[doc = "Create a Rust string from a null-terminated C string"]
199-
unsafe fn from_c_str(c_str: *libc::c_char) -> str {
200-
from_buf(::unsafe::reinterpret_cast(c_str))
201-
}
202-
203-
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
204-
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
205-
let mut v: [u8] = [];
206-
vec::reserve(v, len + 1u);
207-
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
208-
vec::unsafe::set_len(v, len);
209-
v += [0u8];
210-
211-
assert is_utf8(v);
212-
let s: str = ::unsafe::reinterpret_cast(v);
213-
::unsafe::leak(v);
214-
ret s;
215-
}
216-
217-
#[doc = "Create a Rust string from a `*c_char` buffer of the given length"]
218-
unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str {
219-
from_buf_len(::unsafe::reinterpret_cast(c_str), len)
220-
}
221-
222184
#[doc = "Concatenate a vector of strings"]
223185
fn concat(v: [str]) -> str {
224186
let mut s: str = "";
@@ -1522,6 +1484,10 @@ fn reserve(&ss: str, nn: uint) {
15221484
mod unsafe {
15231485
export
15241486
// FIXME: stop exporting several of these
1487+
from_buf,
1488+
from_buf_len,
1489+
from_c_str,
1490+
from_c_str_len,
15251491
from_bytes,
15261492
from_byte,
15271493
slice_bytes,
@@ -1531,6 +1497,42 @@ mod unsafe {
15311497
shift_byte,
15321498
set_len;
15331499

1500+
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
1501+
unsafe fn from_buf(buf: *u8) -> str {
1502+
let mut curr = buf, i = 0u;
1503+
while *curr != 0u8 {
1504+
i += 1u;
1505+
curr = ptr::offset(buf, i);
1506+
}
1507+
ret from_buf_len(buf, i);
1508+
}
1509+
1510+
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
1511+
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
1512+
let mut v: [u8] = [];
1513+
vec::reserve(v, len + 1u);
1514+
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
1515+
vec::unsafe::set_len(v, len);
1516+
v += [0u8];
1517+
1518+
assert is_utf8(v);
1519+
let s: str = ::unsafe::reinterpret_cast(v);
1520+
::unsafe::leak(v);
1521+
ret s;
1522+
}
1523+
1524+
#[doc = "Create a Rust string from a null-terminated C string"]
1525+
unsafe fn from_c_str(c_str: *libc::c_char) -> str {
1526+
from_buf(::unsafe::reinterpret_cast(c_str))
1527+
}
1528+
1529+
#[doc = "
1530+
Create a Rust string from a `*c_char` buffer of the given length
1531+
"]
1532+
unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str {
1533+
from_buf_len(::unsafe::reinterpret_cast(c_str), len)
1534+
}
1535+
15341536
#[doc = "
15351537
Converts a vector of bytes to a string.
15361538
@@ -2222,15 +2224,15 @@ mod tests {
22222224
fn test_from_buf() unsafe {
22232225
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
22242226
let b = vec::unsafe::to_ptr(a);
2225-
let c = from_buf(b);
2227+
let c = unsafe::from_buf(b);
22262228
assert (c == "AAAAAAA");
22272229
}
22282230

22292231
#[test]
22302232
fn test_from_buf_len() unsafe {
22312233
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
22322234
let b = vec::unsafe::to_ptr(a);
2233-
let c = from_buf_len(b, 3u);
2235+
let c = unsafe::from_buf_len(b, 3u);
22342236
assert (c == "AAA");
22352237
}
22362238

@@ -2252,7 +2254,7 @@ mod tests {
22522254
fn test_as_buf2() unsafe {
22532255
let s = "hello";
22542256
let sb = as_buf(s, {|b| b });
2255-
let s_cstr = from_buf(sb);
2257+
let s_cstr = unsafe::from_buf(sb);
22562258
assert (eq(s_cstr, s));
22572259
}
22582260

src/rustc/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn llvm_err(sess: session, msg: str) -> ! unsafe {
2727
let cstr = llvm::LLVMRustGetLastError();
2828
if cstr == ptr::null() {
2929
sess.fatal(msg);
30-
} else { sess.fatal(msg + ": " + str::from_c_str(cstr)); }
30+
} else { sess.fatal(msg + ": " + str::unsafe::from_c_str(cstr)); }
3131
}
3232

3333
fn load_intrinsics_bc(sess: session) -> option<ModuleRef> {

src/rustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn get_metadata_section(sess: session::session,
216216
let si = mk_section_iter(of.llof);
217217
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
218218
let name_buf = llvm::LLVMGetSectionName(si.llsi);
219-
let name = unsafe { str::from_c_str(name_buf) };
219+
let name = unsafe { str::unsafe::from_c_str(name_buf) };
220220
if str::eq(name, sess.targ_cfg.target_strs.meta_sect_name) {
221221
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
222222
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;

0 commit comments

Comments
 (0)