Add ToCStr method .with_c_str()
.with_c_str() is a replacement for the old .as_c_str(), to avoid unnecessary boilerplate. Replace all usages of .to_c_str().with_ref() with .with_c_str().
This commit is contained in:
parent
48265b779f
commit
03ef71e262
33 changed files with 159 additions and 136 deletions
|
|
@ -133,6 +133,29 @@ pub trait ToCStr {
|
|||
|
||||
/// Unsafe variant of `to_c_str()` that doesn't check for nulls.
|
||||
unsafe fn to_c_str_unchecked(&self) -> CString;
|
||||
|
||||
/// Work with a temporary CString constructed from the receiver.
|
||||
/// The provided `*libc::c_char` will be freed immediately upon return.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ~~~ {.rust}
|
||||
/// let s = "PATH".with_c_str(|path| libc::getenv(path))
|
||||
/// ~~~
|
||||
///
|
||||
/// # Failure
|
||||
///
|
||||
/// Raises the `null_byte` condition if the receiver has an interior null.
|
||||
#[inline]
|
||||
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
||||
self.to_c_str().with_ref(f)
|
||||
}
|
||||
|
||||
/// Unsafe variant of `with_c_str()` that doesn't check for nulls.
|
||||
#[inline]
|
||||
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
||||
self.to_c_str_unchecked().with_ref(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'self> ToCStr for &'self str {
|
||||
|
|
|
|||
|
|
@ -1041,8 +1041,8 @@ pub fn stdin() -> @Reader {
|
|||
}
|
||||
|
||||
pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
|
||||
let f = do path.to_c_str().with_ref |pathbuf| {
|
||||
do "rb".to_c_str().with_ref |modebuf| {
|
||||
let f = do path.with_c_str |pathbuf| {
|
||||
do "rb".with_c_str |modebuf| {
|
||||
unsafe { libc::fopen(pathbuf, modebuf as *libc::c_char) }
|
||||
}
|
||||
};
|
||||
|
|
@ -1291,7 +1291,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
|
|||
}
|
||||
}
|
||||
let fd = unsafe {
|
||||
do path.to_c_str().with_ref |pathbuf| {
|
||||
do path.with_c_str |pathbuf| {
|
||||
libc::open(pathbuf, fflags, (S_IRUSR | S_IWUSR) as c_int)
|
||||
}
|
||||
};
|
||||
|
|
@ -1574,8 +1574,8 @@ pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
|
|||
// FIXME: fileflags // #2004
|
||||
pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> {
|
||||
unsafe {
|
||||
let f = do path.to_c_str().with_ref |pathbuf| {
|
||||
do "w".to_c_str().with_ref |modebuf| {
|
||||
let f = do path.with_c_str |pathbuf| {
|
||||
do "w".with_c_str |modebuf| {
|
||||
libc::fopen(pathbuf, modebuf)
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -239,7 +239,7 @@ pub fn env() -> ~[(~str,~str)] {
|
|||
pub fn getenv(n: &str) -> Option<~str> {
|
||||
unsafe {
|
||||
do with_env_lock {
|
||||
let s = do n.to_c_str().with_ref |buf| {
|
||||
let s = do n.with_c_str |buf| {
|
||||
libc::getenv(buf)
|
||||
};
|
||||
if s.is_null() {
|
||||
|
|
@ -274,8 +274,8 @@ pub fn getenv(n: &str) -> Option<~str> {
|
|||
pub fn setenv(n: &str, v: &str) {
|
||||
unsafe {
|
||||
do with_env_lock {
|
||||
do n.to_c_str().with_ref |nbuf| {
|
||||
do v.to_c_str().with_ref |vbuf| {
|
||||
do n.with_c_str |nbuf| {
|
||||
do v.with_c_str |vbuf| {
|
||||
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -306,7 +306,7 @@ pub fn unsetenv(n: &str) {
|
|||
fn _unsetenv(n: &str) {
|
||||
unsafe {
|
||||
do with_env_lock {
|
||||
do n.to_c_str().with_ref |nbuf| {
|
||||
do n.with_c_str |nbuf| {
|
||||
libc::funcs::posix01::unistd::unsetenv(nbuf);
|
||||
}
|
||||
}
|
||||
|
|
@ -328,7 +328,7 @@ pub fn unsetenv(n: &str) {
|
|||
}
|
||||
|
||||
pub fn fdopen(fd: c_int) -> *FILE {
|
||||
do "r".to_c_str().with_ref |modebuf| {
|
||||
do "r".with_c_str |modebuf| {
|
||||
unsafe {
|
||||
libc::fdopen(fd, modebuf)
|
||||
}
|
||||
|
|
@ -464,7 +464,7 @@ pub fn self_exe_path() -> Option<Path> {
|
|||
let mut path = [0 as c_char, .. TMPBUF_SZ];
|
||||
|
||||
do path.as_mut_buf |buf, len| {
|
||||
let len = do "/proc/self/exe".to_c_str().with_ref |proc_self_buf| {
|
||||
let len = do "/proc/self/exe".with_c_str |proc_self_buf| {
|
||||
readlink(proc_self_buf, buf, len as size_t) as uint
|
||||
};
|
||||
|
||||
|
|
@ -593,7 +593,7 @@ pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool {
|
|||
/// Indicates whether a path represents a directory
|
||||
pub fn path_is_dir(p: &Path) -> bool {
|
||||
unsafe {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
rustrt::rust_path_is_dir(buf) != 0 as c_int
|
||||
}
|
||||
}
|
||||
|
|
@ -602,7 +602,7 @@ pub fn path_is_dir(p: &Path) -> bool {
|
|||
/// Indicates whether a path exists
|
||||
pub fn path_exists(p: &Path) -> bool {
|
||||
unsafe {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
rustrt::rust_path_exists(buf) != 0 as c_int
|
||||
}
|
||||
}
|
||||
|
|
@ -645,7 +645,7 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
|
|||
|
||||
#[cfg(unix)]
|
||||
fn mkdir(p: &Path, mode: c_int) -> bool {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
unsafe {
|
||||
libc::mkdir(buf, mode as libc::mode_t) == (0 as c_int)
|
||||
}
|
||||
|
|
@ -697,7 +697,7 @@ pub fn list_dir(p: &Path) -> ~[~str] {
|
|||
let mut strings = ~[];
|
||||
debug!("os::list_dir -- BEFORE OPENDIR");
|
||||
|
||||
let dir_ptr = do p.to_c_str().with_ref |buf| {
|
||||
let dir_ptr = do p.with_c_str |buf| {
|
||||
opendir(buf)
|
||||
};
|
||||
|
||||
|
|
@ -819,7 +819,7 @@ pub fn remove_dir(p: &Path) -> bool {
|
|||
|
||||
#[cfg(unix)]
|
||||
fn rmdir(p: &Path) -> bool {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
unsafe {
|
||||
libc::rmdir(buf) == (0 as c_int)
|
||||
}
|
||||
|
|
@ -844,7 +844,7 @@ pub fn change_dir(p: &Path) -> bool {
|
|||
|
||||
#[cfg(unix)]
|
||||
fn chdir(p: &Path) -> bool {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
unsafe {
|
||||
libc::chdir(buf) == (0 as c_int)
|
||||
}
|
||||
|
|
@ -872,8 +872,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
|
|||
#[cfg(unix)]
|
||||
fn do_copy_file(from: &Path, to: &Path) -> bool {
|
||||
unsafe {
|
||||
let istream = do from.to_c_str().with_ref |fromp| {
|
||||
do "rb".to_c_str().with_ref |modebuf| {
|
||||
let istream = do from.with_c_str |fromp| {
|
||||
do "rb".with_c_str |modebuf| {
|
||||
libc::fopen(fromp, modebuf)
|
||||
}
|
||||
};
|
||||
|
|
@ -884,8 +884,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
|
|||
let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \
|
||||
for source file");
|
||||
|
||||
let ostream = do to.to_c_str().with_ref |top| {
|
||||
do "w+b".to_c_str().with_ref |modebuf| {
|
||||
let ostream = do to.with_c_str |top| {
|
||||
do "w+b".with_c_str |modebuf| {
|
||||
libc::fopen(top, modebuf)
|
||||
}
|
||||
};
|
||||
|
|
@ -917,7 +917,7 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
|
|||
fclose(ostream);
|
||||
|
||||
// Give the new file the old file's permissions
|
||||
if do to.to_c_str().with_ref |to_buf| {
|
||||
if do to.with_c_str |to_buf| {
|
||||
libc::chmod(to_buf, from_mode as libc::mode_t)
|
||||
} != 0 {
|
||||
return false; // should be a condition...
|
||||
|
|
@ -944,7 +944,7 @@ pub fn remove_file(p: &Path) -> bool {
|
|||
#[cfg(unix)]
|
||||
fn unlink(p: &Path) -> bool {
|
||||
unsafe {
|
||||
do p.to_c_str().with_ref |buf| {
|
||||
do p.with_c_str |buf| {
|
||||
libc::unlink(buf) == (0 as c_int)
|
||||
}
|
||||
}
|
||||
|
|
@ -1282,7 +1282,7 @@ pub fn glob(pattern: &str) -> ~[Path] {
|
|||
}
|
||||
|
||||
let mut g = default_glob_t();
|
||||
do pattern.to_c_str().with_ref |c_pattern| {
|
||||
do pattern.with_c_str |c_pattern| {
|
||||
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
|
||||
};
|
||||
do(|| {
|
||||
|
|
@ -1929,14 +1929,14 @@ mod tests {
|
|||
let out = tempdir.push("out.txt");
|
||||
|
||||
/* Write the temp input file */
|
||||
let ostream = do input.to_c_str().with_ref |fromp| {
|
||||
do "w+b".to_c_str().with_ref |modebuf| {
|
||||
let ostream = do input.with_c_str |fromp| {
|
||||
do "w+b".with_c_str |modebuf| {
|
||||
libc::fopen(fromp, modebuf)
|
||||
}
|
||||
};
|
||||
assert!((ostream as uint != 0u));
|
||||
let s = ~"hello";
|
||||
do "hello".to_c_str().with_ref |buf| {
|
||||
do "hello".with_c_str |buf| {
|
||||
let write_len = libc::fwrite(buf as *c_void,
|
||||
1u as size_t,
|
||||
(s.len() + 1u) as size_t,
|
||||
|
|
@ -2013,11 +2013,11 @@ mod tests {
|
|||
remove_file(&path);
|
||||
|
||||
let fd = unsafe {
|
||||
let fd = do path.to_c_str().with_ref |path| {
|
||||
let fd = do path.with_c_str |path| {
|
||||
open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
|
||||
};
|
||||
lseek_(fd, size);
|
||||
do "x".to_c_str().with_ref |x| {
|
||||
do "x".with_c_str |x| {
|
||||
assert!(write(fd, x as *c_void, 1) == 1);
|
||||
}
|
||||
fd
|
||||
|
|
|
|||
|
|
@ -381,7 +381,7 @@ mod stat {
|
|||
#[cfg(target_os = "win32")]
|
||||
impl WindowsPath {
|
||||
pub fn stat(&self) -> Option<libc::stat> {
|
||||
do self.to_c_str().with_ref |buf| {
|
||||
do self.with_c_str |buf| {
|
||||
let mut st = stat::arch::default_stat();
|
||||
match unsafe { libc::stat(buf, &mut st) } {
|
||||
0 => Some(st),
|
||||
|
|
@ -415,7 +415,7 @@ impl WindowsPath {
|
|||
#[cfg(not(target_os = "win32"))]
|
||||
impl PosixPath {
|
||||
pub fn stat(&self) -> Option<libc::stat> {
|
||||
do self.to_c_str().with_ref |buf| {
|
||||
do self.with_c_str |buf| {
|
||||
let mut st = stat::arch::default_stat();
|
||||
match unsafe { libc::stat(buf as *libc::c_char, &mut st) } {
|
||||
0 => Some(st),
|
||||
|
|
@ -493,7 +493,7 @@ impl PosixPath {
|
|||
#[cfg(unix)]
|
||||
impl PosixPath {
|
||||
pub fn lstat(&self) -> Option<libc::stat> {
|
||||
do self.to_c_str().with_ref |buf| {
|
||||
do self.with_c_str |buf| {
|
||||
let mut st = stat::arch::default_stat();
|
||||
match unsafe { libc::lstat(buf, &mut st) } {
|
||||
0 => Some(st),
|
||||
|
|
|
|||
|
|
@ -481,7 +481,7 @@ pub mod ptr_tests {
|
|||
fn test_position() {
|
||||
use libc::c_char;
|
||||
|
||||
do "hello".to_c_str().with_ref |p| {
|
||||
do "hello".with_c_str |p| {
|
||||
unsafe {
|
||||
assert!(2u == position(p, |c| *c == 'l' as c_char));
|
||||
assert!(4u == position(p, |c| *c == 'o' as c_char));
|
||||
|
|
@ -492,9 +492,9 @@ pub mod ptr_tests {
|
|||
|
||||
#[test]
|
||||
fn test_buf_len() {
|
||||
do "hello".to_c_str().with_ref |p0| {
|
||||
do "there".to_c_str().with_ref |p1| {
|
||||
do "thing".to_c_str().with_ref |p2| {
|
||||
do "hello".with_c_str |p0| {
|
||||
do "there".with_c_str |p1| {
|
||||
do "thing".with_c_str |p2| {
|
||||
let v = ~[p0, p1, p2, null()];
|
||||
do v.as_imm_buf |vp, len| {
|
||||
assert_eq!(unsafe { buf_len(vp) }, 3u);
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) {
|
|||
match try_take_task_borrow_list() {
|
||||
None => { // not recording borrows
|
||||
let msg = "borrowed";
|
||||
do msg.to_c_str().with_ref |msg_p| {
|
||||
do msg.with_c_str |msg_p| {
|
||||
sys::begin_unwind_(msg_p, file, line);
|
||||
}
|
||||
}
|
||||
|
|
@ -68,7 +68,7 @@ unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) {
|
|||
sep = " and at ";
|
||||
}
|
||||
}
|
||||
do msg.to_c_str().with_ref |msg_p| {
|
||||
do msg.with_c_str |msg_p| {
|
||||
sys::begin_unwind_(msg_p, file, line)
|
||||
}
|
||||
}
|
||||
|
|
@ -208,7 +208,7 @@ pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
|
|||
let br = borrow_list.pop();
|
||||
if br.box != a || br.file != file || br.line != line {
|
||||
let err = fmt!("wrong borrow found, br=%?", br);
|
||||
do err.to_c_str().with_ref |msg_p| {
|
||||
do err.with_c_str |msg_p| {
|
||||
sys::begin_unwind_(msg_p, file, line)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ pub fn init(crate_map: *u8) {
|
|||
let log_spec = os::getenv("RUST_LOG");
|
||||
match log_spec {
|
||||
Some(spec) => {
|
||||
do spec.to_c_str().with_ref |buf| {
|
||||
do spec.with_c_str |buf| {
|
||||
unsafe { rust_update_log_settings(crate_map, buf) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -654,7 +654,7 @@ impl RtioUdpSocket for UvUdpSocket {
|
|||
|
||||
fn join_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> {
|
||||
let r = unsafe {
|
||||
do multi.to_str().to_c_str().with_ref |m_addr| {
|
||||
do multi.to_str().with_c_str |m_addr| {
|
||||
uvll::udp_set_membership(self.native_handle(), m_addr,
|
||||
ptr::null(), uvll::UV_JOIN_GROUP)
|
||||
}
|
||||
|
|
@ -668,7 +668,7 @@ impl RtioUdpSocket for UvUdpSocket {
|
|||
|
||||
fn leave_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> {
|
||||
let r = unsafe {
|
||||
do multi.to_str().to_c_str().with_ref |m_addr| {
|
||||
do multi.to_str().with_c_str |m_addr| {
|
||||
uvll::udp_set_membership(self.native_handle(), m_addr,
|
||||
ptr::null(), uvll::UV_LEAVE_GROUP)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -373,12 +373,12 @@ pub unsafe fn is_ip6_addr(addr: *sockaddr) -> bool {
|
|||
}
|
||||
|
||||
pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in {
|
||||
do ip.to_c_str().with_ref |ip_buf| {
|
||||
do ip.with_c_str |ip_buf| {
|
||||
rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int)
|
||||
}
|
||||
}
|
||||
pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 {
|
||||
do ip.to_c_str().with_ref |ip_buf| {
|
||||
do ip.with_c_str |ip_buf| {
|
||||
rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -506,7 +506,7 @@ fn spawn_process_os(prog: &str, args: &[~str],
|
|||
|
||||
do with_envp(env) |envp| {
|
||||
do with_dirp(dir) |dirp| {
|
||||
do cmd.to_c_str().with_ref |cmdp| {
|
||||
do cmd.with_c_str |cmdp| {
|
||||
let created = CreateProcessA(ptr::null(), cast::transmute(cmdp),
|
||||
ptr::mut_null(), ptr::mut_null(), TRUE,
|
||||
0, envp, dirp, &mut si, &mut pi);
|
||||
|
|
@ -775,7 +775,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T {
|
|||
|
||||
fn with_dirp<T>(d: Option<&Path>, cb: &fn(*libc::c_char) -> T) -> T {
|
||||
match d {
|
||||
Some(dir) => dir.to_c_str().with_ref(|buf| cb(buf)),
|
||||
Some(dir) => dir.with_c_str(|buf| cb(buf)),
|
||||
None => cb(ptr::null())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,8 +105,8 @@ pub trait FailWithCause {
|
|||
|
||||
impl FailWithCause for ~str {
|
||||
fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
|
||||
do cause.to_c_str().with_ref |msg_buf| {
|
||||
do file.to_c_str().with_ref |file_buf| {
|
||||
do cause.with_c_str |msg_buf| {
|
||||
do file.with_c_str |file_buf| {
|
||||
begin_unwind_(msg_buf, file_buf, line as libc::size_t)
|
||||
}
|
||||
}
|
||||
|
|
@ -115,8 +115,8 @@ impl FailWithCause for ~str {
|
|||
|
||||
impl FailWithCause for &'static str {
|
||||
fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
|
||||
do cause.to_c_str().with_ref |msg_buf| {
|
||||
do file.to_c_str().with_ref |file_buf| {
|
||||
do cause.with_c_str |msg_buf| {
|
||||
do file.with_c_str |file_buf| {
|
||||
begin_unwind_(msg_buf, file_buf, line as libc::size_t)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ impl DynamicLibrary {
|
|||
// T but that feature is still unimplemented
|
||||
|
||||
let maybe_symbol_value = do dl::check_for_errors_in {
|
||||
do symbol.to_c_str().with_ref |raw_string| {
|
||||
do symbol.with_c_str |raw_string| {
|
||||
dl::symbol(self.handle, raw_string)
|
||||
}
|
||||
};
|
||||
|
|
@ -145,7 +145,7 @@ mod dl {
|
|||
use result::*;
|
||||
|
||||
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
|
||||
do filename.to_c_str().with_ref |raw_name| {
|
||||
do filename.with_c_str |raw_name| {
|
||||
dlopen(raw_name, Lazy as libc::c_int)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
|
|||
index: size_t, len: size_t) {
|
||||
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
|
||||
len as int, index as int);
|
||||
do msg.to_c_str().with_ref |buf| {
|
||||
do msg.with_c_str |buf| {
|
||||
fail_(buf, file, line);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue