Skip to content

Replace c_str.with_ref(...) with .as_ptr() #14904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,9 @@ pub mod test {
let two = "twoTwo".to_c_str();
let three = "threeThree".to_c_str();
let arr = vec![
one.with_ref(|buf| buf),
two.with_ref(|buf| buf),
three.with_ref(|buf| buf)
one.as_ptr(),
two.as_ptr(),
three.as_ptr()
];
let expected_arr = [
one, two, three
Expand All @@ -694,9 +694,7 @@ pub mod test {
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 = expected_arr[ctr].with_ref(|buf| {
str::raw::from_c_str(buf)
});
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
assert_eq!(actual.as_slice(), expected.as_slice());
ctr += 1;
iteration_count += 1;
Expand All @@ -712,9 +710,9 @@ pub mod test {
let two = "twoTwo".to_c_str();
let three = "threeThree".to_c_str();
let arr = vec![
one.with_ref(|buf| buf),
two.with_ref(|buf| buf),
three.with_ref(|buf| buf),
one.as_ptr(),
two.as_ptr(),
three.as_ptr(),
// fake a null terminator
null()
];
Expand All @@ -727,9 +725,7 @@ pub mod test {
let mut iteration_count = 0u;
array_each(arr_ptr, |e| {
let actual = str::raw::from_c_str(e);
let expected = expected_arr[ctr].with_ref(|buf| {
str::raw::from_c_str(buf)
});
let expected = str::raw::from_c_str(expected_arr[ctr].as_ptr());
assert_eq!(actual.as_slice(), expected.as_slice());
ctr += 1;
iteration_count += 1;
Expand Down
4 changes: 2 additions & 2 deletions src/libnative/io/addrinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl GetAddrInfoRequest {

// Make the call
let s = unsafe {
let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
let ch = if c_host.is_null() { null() } else { c_host.as_ptr() };
let cs = if c_serv.is_null() { null() } else { c_serv.as_ptr() };
getaddrinfo(ch, cs, hint_ptr, &mut res)
};

Expand Down
30 changes: 15 additions & 15 deletions src/libnative/io/file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ pub fn open(path: &CString, fm: rtio::FileMode, fa: rtio::FileAccess)
libc::S_IRUSR | libc::S_IWUSR),
};

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

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

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

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

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

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

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

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

pub fn rename(old: &CString, new: &CString) -> IoResult<()> {
super::mkerr_libc(retry(|| unsafe {
libc::rename(old.with_ref(|p| p), new.with_ref(|p| p))
libc::rename(old.as_ptr(), new.as_ptr())
}))
}

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

pub fn rmdir(p: &CString) -> IoResult<()> {
super::mkerr_libc(retry(|| unsafe {
libc::rmdir(p.with_ref(|p| p))
libc::rmdir(p.as_ptr())
}))
}

pub fn chown(p: &CString, uid: int, gid: int) -> IoResult<()> {
super::mkerr_libc(retry(|| unsafe {
libc::chown(p.with_ref(|p| p), uid as libc::uid_t,
libc::chown(p.as_ptr(), uid as libc::uid_t,
gid as libc::gid_t)
}))
}

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

pub fn symlink(src: &CString, dst: &CString) -> IoResult<()> {
super::mkerr_libc(retry(|| unsafe {
libc::symlink(src.with_ref(|p| p), dst.with_ref(|p| p))
libc::symlink(src.as_ptr(), dst.as_ptr())
}))
}

pub fn link(src: &CString, dst: &CString) -> IoResult<()> {
super::mkerr_libc(retry(|| unsafe {
libc::link(src.with_ref(|p| p), dst.with_ref(|p| p))
libc::link(src.as_ptr(), dst.as_ptr())
}))
}

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

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

pub fn lstat(p: &CString) -> IoResult<rtio::FileStat> {
let mut stat: libc::stat = unsafe { mem::zeroed() };
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
match retry(|| unsafe { libc::lstat(p.as_ptr(), &mut stat) }) {
0 => Ok(mkstat(&stat)),
_ => Err(super::last_error()),
}
Expand All @@ -509,7 +509,7 @@ pub fn utime(p: &CString, atime: u64, mtime: u64) -> IoResult<()> {
modtime: (mtime / 1000) as libc::time_t,
};
super::mkerr_libc(retry(|| unsafe {
libc::utime(p.with_ref(|p| p), &buf)
libc::utime(p.as_ptr(), &buf)
}))
}

Expand Down
4 changes: 2 additions & 2 deletions src/libnative/io/file_win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
use std::rt::libc_heap::malloc_raw;

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/pipe_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Drop for UnixListener {
// careful to unlink the path before we close the file descriptor to
// prevent races where we unlink someone else's path.
unsafe {
let _ = libc::unlink(self.path.with_ref(|p| p));
let _ = libc::unlink(self.path.as_ptr());
}
}
}
8 changes: 4 additions & 4 deletions src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ fn spawn_process_os(cfg: ProcessConfig,
assert_eq!(ret, 0);
}

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

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

// Add a terminating null pointer (required by libc).
ptrs.push(ptr::null());
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ pub mod write {
{
let add = |arg: &str| {
let s = arg.to_c_str();
llvm_args.push(s.with_ref(|p| p));
llvm_args.push(s.as_ptr());
llvm_c_strs.push(s);
};
add("rustc"); // fake program name
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
// Internalize everything but the reachable symbols of the current module
let cstrs: Vec<::std::c_str::CString> =
reachable.iter().map(|s| s.as_slice().to_c_str()).collect();
let arr: Vec<*const i8> = cstrs.iter().map(|c| c.with_ref(|p| p)).collect();
let arr: Vec<*const i8> = cstrs.iter().map(|c| c.as_ptr()).collect();
let ptr = arr.as_ptr();
unsafe {
llvm::LLVMRustRunRestrictionPass(llmod,
Expand Down
35 changes: 17 additions & 18 deletions src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,24 +1433,23 @@ fn compile_unit_metadata(cx: &CrateContext) {
let producer = format!("rustc version {}",
(option_env!("CFG_VERSION")).expect("CFG_VERSION"));

compile_unit_name.with_ref(|compile_unit_name| {
work_dir.as_vec().with_c_str(|work_dir| {
producer.with_c_str(|producer| {
"".with_c_str(|flags| {
"".with_c_str(|split_name| {
unsafe {
llvm::LLVMDIBuilderCreateCompileUnit(
debug_context(cx).builder,
DW_LANG_RUST,
compile_unit_name,
work_dir,
producer,
cx.sess().opts.optimize != config::No,
flags,
0,
split_name);
}
})
let compile_unit_name = compile_unit_name.as_ptr();
work_dir.as_vec().with_c_str(|work_dir| {
producer.with_c_str(|producer| {
"".with_c_str(|flags| {
"".with_c_str(|split_name| {
unsafe {
llvm::LLVMDIBuilderCreateCompileUnit(
debug_context(cx).builder,
DW_LANG_RUST,
compile_unit_name,
work_dir,
producer,
cx.sess().opts.optimize != config::No,
flags,
0,
split_name);
}
})
})
})
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
s.push_str(highlight::highlight(text.as_slice(), None, id)
.as_slice());
let output = s.to_c_str();
output.with_ref(|r| {
hoedown_buffer_puts(ob, r)
})
hoedown_buffer_puts(ob, output.as_ptr());
}
})
}
Expand Down
Loading