From 60a70c6b1e9fa201b686a6121650f27caa6ae932 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 27 Sep 2016 22:23:36 +0200 Subject: Remove double copy of array (to_owned() and PathBuf::from), use OsString::from_vec on existing path var rather than construct string from pointer --- src/unistd.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/unistd.rs') diff --git a/src/unistd.rs b/src/unistd.rs index 53ba3344..4d4e7529 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,8 +5,8 @@ use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC}; use fcntl::FcntlArg::{F_SETFD, F_SETFL}; use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t}; use std::mem; -use std::ffi::{CString, CStr, OsString, OsStr}; -use std::os::unix::ffi::{OsStringExt, OsStrExt}; +use std::ffi::{CString, CStr, OsString}; +use std::os::unix::ffi::{OsStringExt}; use std::os::unix::io::RawFd; use std::path::{PathBuf}; use void::Void; @@ -537,12 +537,11 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { let mut path = try!(template.with_nix_path(|path| {path.to_bytes_with_nul().to_owned()})); let p = path.as_mut_ptr() as *mut _; - unsafe { - let fd = libc::mkstemp(p); - let pathname = OsStr::from_bytes(CStr::from_ptr(p).to_bytes()); - try!(Errno::result(fd)); - Ok((fd, PathBuf::from(pathname).to_owned())) - } + let fd = unsafe { libc::mkstemp(p) }; + path.pop(); // drop the trailing nul + let pathname = OsString::from_vec(path); + try!(Errno::result(fd)); + Ok((fd, PathBuf::from(pathname))) } #[cfg(any(target_os = "linux", target_os = "android"))] -- cgit v1.2.3