Skip to content

Commit b569c77

Browse files
committed
auto merge of rust-lang#14904 : huonw/rust/cstr-remove-withref, r=alexcrichton
2 parents 6a3695d + 569f13a commit b569c77

File tree

16 files changed

+230
-162
lines changed

16 files changed

+230
-162
lines changed

src/libcore/ptr.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ pub mod test {
682682
let two = "twoTwo".to_c_str();
683683
let three = "threeThree".to_c_str();
684684
let arr = vec![
685-
one.with_ref(|buf| buf),
686-
two.with_ref(|buf| buf),
687-
three.with_ref(|buf| buf)
685+
one.as_ptr(),
686+
two.as_ptr(),
687+
three.as_ptr()
688688
];
689689
let expected_arr = [
690690
one, two, three
@@ -694,9 +694,7 @@ pub mod test {
694694
let mut iteration_count = 0;
695695
array_each_with_len(arr.as_ptr(), arr.len(), |e| {
696696
let actual = str::raw::from_c_str(e);
697-
let expected = expected_arr[ctr].with_ref(|buf| {
698-
str::raw::from_c_str(buf)
699-
});
697+
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
700698
assert_eq!(actual.as_slice(), expected.as_slice());
701699
ctr += 1;
702700
iteration_count += 1;
@@ -712,9 +710,9 @@ pub mod test {
712710
let two = "twoTwo".to_c_str();
713711
let three = "threeThree".to_c_str();
714712
let arr = vec![
715-
one.with_ref(|buf| buf),
716-
two.with_ref(|buf| buf),
717-
three.with_ref(|buf| buf),
713+
one.as_ptr(),
714+
two.as_ptr(),
715+
three.as_ptr(),
718716
// fake a null terminator
719717
null()
720718
];
@@ -727,9 +725,7 @@ pub mod test {
727725
let mut iteration_count = 0u;
728726
array_each(arr_ptr, |e| {
729727
let actual = str::raw::from_c_str(e);
730-
let expected = expected_arr[ctr].with_ref(|buf| {
731-
str::raw::from_c_str(buf)
732-
});
728+
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
733729
assert_eq!(actual.as_slice(), expected.as_slice());
734730
ctr += 1;
735731
iteration_count += 1;

src/libnative/io/addrinfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl GetAddrInfoRequest {
5050

5151
// Make the call
5252
let s = unsafe {
53-
let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
54-
let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
53+
let ch = if c_host.is_null() { null() } else { c_host.as_ptr() };
54+
let cs = if c_serv.is_null() { null() } else { c_serv.as_ptr() };
5555
getaddrinfo(ch, cs, hint_ptr, &mut res)
5656
};
5757

src/libnative/io/file_unix.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,15 @@ pub fn open(path: &CString, fm: rtio::FileMode, fa: rtio::FileAccess)
339339
libc::S_IRUSR | libc::S_IWUSR),
340340
};
341341

342-
match retry(|| unsafe { libc::open(path.with_ref(|p| p), flags, mode) }) {
342+
match retry(|| unsafe { libc::open(path.as_ptr(), flags, mode) }) {
343343
-1 => Err(super::last_error()),
344344
fd => Ok(FileDesc::new(fd, true)),
345345
}
346346
}
347347

348348
pub fn mkdir(p: &CString, mode: uint) -> IoResult<()> {
349349
super::mkerr_libc(retry(|| unsafe {
350-
libc::mkdir(p.with_ref(|p| p), mode as libc::mode_t)
350+
libc::mkdir(p.as_ptr(), mode as libc::mode_t)
351351
}))
352352
}
353353

@@ -356,7 +356,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
356356
use libc::{opendir, readdir_r, closedir};
357357

358358
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
359-
let root = unsafe { CString::new(root.with_ref(|p| p), false) };
359+
let root = unsafe { CString::new(root.as_ptr(), false) };
360360
let root = Path::new(root);
361361

362362
dirs.move_iter().filter(|path| {
@@ -373,7 +373,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
373373
let mut buf = Vec::<u8>::with_capacity(size as uint);
374374
let ptr = buf.as_mut_slice().as_mut_ptr() as *mut dirent_t;
375375

376-
let dir_ptr = p.with_ref(|buf| unsafe { opendir(buf) });
376+
let dir_ptr = unsafe {opendir(p.as_ptr())};
377377

378378
if dir_ptr as uint != 0 {
379379
let mut paths = vec!();
@@ -393,36 +393,36 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
393393
}
394394

395395
pub fn unlink(p: &CString) -> IoResult<()> {
396-
super::mkerr_libc(retry(|| unsafe { libc::unlink(p.with_ref(|p| p)) }))
396+
super::mkerr_libc(retry(|| unsafe { libc::unlink(p.as_ptr()) }))
397397
}
398398

399399
pub fn rename(old: &CString, new: &CString) -> IoResult<()> {
400400
super::mkerr_libc(retry(|| unsafe {
401-
libc::rename(old.with_ref(|p| p), new.with_ref(|p| p))
401+
libc::rename(old.as_ptr(), new.as_ptr())
402402
}))
403403
}
404404

405405
pub fn chmod(p: &CString, mode: uint) -> IoResult<()> {
406406
super::mkerr_libc(retry(|| unsafe {
407-
libc::chmod(p.with_ref(|p| p), mode as libc::mode_t)
407+
libc::chmod(p.as_ptr(), mode as libc::mode_t)
408408
}))
409409
}
410410

411411
pub fn rmdir(p: &CString) -> IoResult<()> {
412412
super::mkerr_libc(retry(|| unsafe {
413-
libc::rmdir(p.with_ref(|p| p))
413+
libc::rmdir(p.as_ptr())
414414
}))
415415
}
416416

417417
pub fn chown(p: &CString, uid: int, gid: int) -> IoResult<()> {
418418
super::mkerr_libc(retry(|| unsafe {
419-
libc::chown(p.with_ref(|p| p), uid as libc::uid_t,
419+
libc::chown(p.as_ptr(), uid as libc::uid_t,
420420
gid as libc::gid_t)
421421
}))
422422
}
423423

424424
pub fn readlink(p: &CString) -> IoResult<CString> {
425-
let p = p.with_ref(|p| p);
425+
let p = p.as_ptr();
426426
let mut len = unsafe { libc::pathconf(p as *mut _, libc::_PC_NAME_MAX) };
427427
if len == -1 {
428428
len = 1024; // FIXME: read PATH_MAX from C ffi?
@@ -443,13 +443,13 @@ pub fn readlink(p: &CString) -> IoResult<CString> {
443443

444444
pub fn symlink(src: &CString, dst: &CString) -> IoResult<()> {
445445
super::mkerr_libc(retry(|| unsafe {
446-
libc::symlink(src.with_ref(|p| p), dst.with_ref(|p| p))
446+
libc::symlink(src.as_ptr(), dst.as_ptr())
447447
}))
448448
}
449449

450450
pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
451451
super::mkerr_libc(retry(|| unsafe {
452-
libc::link(src.with_ref(|p| p), dst.with_ref(|p| p))
452+
libc::link(src.as_ptr(), dst.as_ptr())
453453
}))
454454
}
455455

@@ -489,15 +489,15 @@ fn mkstat(stat: &libc::stat) -> rtio::FileStat {
489489

490490
pub fn stat(p: &CString) -> IoResult<rtio::FileStat> {
491491
let mut stat: libc::stat = unsafe { mem::zeroed() };
492-
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
492+
match retry(|| unsafe { libc::stat(p.as_ptr(), &mut stat) }) {
493493
0 => Ok(mkstat(&stat)),
494494
_ => Err(super::last_error()),
495495
}
496496
}
497497

498498
pub fn lstat(p: &CString) -> IoResult<rtio::FileStat> {
499499
let mut stat: libc::stat = unsafe { mem::zeroed() };
500-
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
500+
match retry(|| unsafe { libc::lstat(p.as_ptr(), &mut stat) }) {
501501
0 => Ok(mkstat(&stat)),
502502
_ => Err(super::last_error()),
503503
}
@@ -509,7 +509,7 @@ pub fn utime(p: &CString, atime: u64, mtime: u64) -> IoResult<()> {
509509
modtime: (mtime / 1000) as libc::time_t,
510510
};
511511
super::mkerr_libc(retry(|| unsafe {
512-
libc::utime(p.with_ref(|p| p), &buf)
512+
libc::utime(p.as_ptr(), &buf)
513513
}))
514514
}
515515

src/libnative/io/file_win32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
347347
use std::rt::libc_heap::malloc_raw;
348348

349349
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
350-
let root = unsafe { CString::new(root.with_ref(|p| p), false) };
350+
let root = unsafe { CString::new(root.as_ptr(), false) };
351351
let root = Path::new(root);
352352

353353
dirs.move_iter().filter(|path| {
@@ -360,7 +360,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
360360
fn rust_list_dir_wfd_fp_buf(wfd: *mut libc::c_void) -> *const u16;
361361
}
362362
let star = Path::new(unsafe {
363-
CString::new(p.with_ref(|p| p), false)
363+
CString::new(p.as_ptr(), false)
364364
}).join("*");
365365
let path = try!(to_utf16(&star.to_c_str()));
366366

src/libnative/io/pipe_unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl Drop for UnixListener {
278278
// careful to unlink the path before we close the file descriptor to
279279
// prevent races where we unlink someone else's path.
280280
unsafe {
281-
let _ = libc::unlink(self.path.with_ref(|p| p));
281+
let _ = libc::unlink(self.path.as_ptr());
282282
}
283283
}
284284
}

src/libnative/io/process.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ fn spawn_process_os(cfg: ProcessConfig,
531531
assert_eq!(ret, 0);
532532
}
533533

534-
let dirp = cfg.cwd.map(|c| c.with_ref(|p| p)).unwrap_or(ptr::null());
534+
let dirp = cfg.cwd.map(|c| c.as_ptr()).unwrap_or(ptr::null());
535535

536536
let cfg = unsafe {
537537
mem::transmute::<ProcessConfig,ProcessConfig<'static>>(cfg)
@@ -633,7 +633,7 @@ fn spawn_process_os(cfg: ProcessConfig,
633633
} else {
634634
libc::O_RDWR
635635
};
636-
devnull.with_ref(|p| libc::open(p, flags, 0))
636+
libc::open(devnull.as_ptr(), flags, 0)
637637
}
638638
Some(obj) => {
639639
let fd = obj.fd();
@@ -715,8 +715,8 @@ fn with_argv<T>(prog: &CString, args: &[CString],
715715
// larger than the lifetime of our invocation of cb, but this is
716716
// technically unsafe as the callback could leak these pointers
717717
// out of our scope.
718-
ptrs.push(prog.with_ref(|buf| buf));
719-
ptrs.extend(args.iter().map(|tmp| tmp.with_ref(|buf| buf)));
718+
ptrs.push(prog.as_ptr());
719+
ptrs.extend(args.iter().map(|tmp| tmp.as_ptr()));
720720

721721
// Add a terminating null pointer (required by libc).
722722
ptrs.push(ptr::null());

src/librustc/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ pub mod write {
412412
{
413413
let add = |arg: &str| {
414414
let s = arg.to_c_str();
415-
llvm_args.push(s.with_ref(|p| p));
415+
llvm_args.push(s.as_ptr());
416416
llvm_c_strs.push(s);
417417
};
418418
add("rustc"); // fake program name

src/librustc/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
9494
// Internalize everything but the reachable symbols of the current module
9595
let cstrs: Vec<::std::c_str::CString> =
9696
reachable.iter().map(|s| s.as_slice().to_c_str()).collect();
97-
let arr: Vec<*const i8> = cstrs.iter().map(|c| c.with_ref(|p| p)).collect();
97+
let arr: Vec<*const i8> = cstrs.iter().map(|c| c.as_ptr()).collect();
9898
let ptr = arr.as_ptr();
9999
unsafe {
100100
llvm::LLVMRustRunRestrictionPass(llmod,

src/librustc/middle/trans/debuginfo.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,24 +1433,23 @@ fn compile_unit_metadata(cx: &CrateContext) {
14331433
let producer = format!("rustc version {}",
14341434
(option_env!("CFG_VERSION")).expect("CFG_VERSION"));
14351435

1436-
compile_unit_name.with_ref(|compile_unit_name| {
1437-
work_dir.as_vec().with_c_str(|work_dir| {
1438-
producer.with_c_str(|producer| {
1439-
"".with_c_str(|flags| {
1440-
"".with_c_str(|split_name| {
1441-
unsafe {
1442-
llvm::LLVMDIBuilderCreateCompileUnit(
1443-
debug_context(cx).builder,
1444-
DW_LANG_RUST,
1445-
compile_unit_name,
1446-
work_dir,
1447-
producer,
1448-
cx.sess().opts.optimize != config::No,
1449-
flags,
1450-
0,
1451-
split_name);
1452-
}
1453-
})
1436+
let compile_unit_name = compile_unit_name.as_ptr();
1437+
work_dir.as_vec().with_c_str(|work_dir| {
1438+
producer.with_c_str(|producer| {
1439+
"".with_c_str(|flags| {
1440+
"".with_c_str(|split_name| {
1441+
unsafe {
1442+
llvm::LLVMDIBuilderCreateCompileUnit(
1443+
debug_context(cx).builder,
1444+
DW_LANG_RUST,
1445+
compile_unit_name,
1446+
work_dir,
1447+
producer,
1448+
cx.sess().opts.optimize != config::No,
1449+
flags,
1450+
0,
1451+
split_name);
1452+
}
14541453
})
14551454
})
14561455
})

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
206206
s.push_str(highlight::highlight(text.as_slice(), None, id)
207207
.as_slice());
208208
let output = s.to_c_str();
209-
output.with_ref(|r| {
210-
hoedown_buffer_puts(ob, r)
211-
})
209+
hoedown_buffer_puts(ob, output.as_ptr());
212210
}
213211
})
214212
}

0 commit comments

Comments
 (0)