Skip to content

Commit 60a70c6

Browse files
committed
Remove double copy of array (to_owned() and PathBuf::from), use OsString::from_vec on existing path var rather than construct string from pointer
1 parent 700627e commit 60a70c6

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/unistd.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
55
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
66
use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t};
77
use std::mem;
8-
use std::ffi::{CString, CStr, OsString, OsStr};
9-
use std::os::unix::ffi::{OsStringExt, OsStrExt};
8+
use std::ffi::{CString, CStr, OsString};
9+
use std::os::unix::ffi::{OsStringExt};
1010
use std::os::unix::io::RawFd;
1111
use std::path::{PathBuf};
1212
use void::Void;
@@ -537,12 +537,11 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint {
537537
pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
538538
let mut path = try!(template.with_nix_path(|path| {path.to_bytes_with_nul().to_owned()}));
539539
let p = path.as_mut_ptr() as *mut _;
540-
unsafe {
541-
let fd = libc::mkstemp(p);
542-
let pathname = OsStr::from_bytes(CStr::from_ptr(p).to_bytes());
543-
try!(Errno::result(fd));
544-
Ok((fd, PathBuf::from(pathname).to_owned()))
545-
}
540+
let fd = unsafe { libc::mkstemp(p) };
541+
path.pop(); // drop the trailing nul
542+
let pathname = OsString::from_vec(path);
543+
try!(Errno::result(fd));
544+
Ok((fd, PathBuf::from(pathname)))
546545
}
547546

548547
#[cfg(any(target_os = "linux", target_os = "android"))]

0 commit comments

Comments
 (0)