summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorPhilipp Keller <philipp.keller@gmail.com>2016-09-27 22:23:36 +0200
committerPhilipp Keller <philipp.keller@gmail.com>2016-09-27 22:23:36 +0200
commit60a70c6b1e9fa201b686a6121650f27caa6ae932 (patch)
tree720a2eef0d43255e8f416ad0056cd66e47ba4852 /src/unistd.rs
parent700627eae2c5410b5a8aee5151d54e42b9365f73 (diff)
downloadnix-60a70c6b1e9fa201b686a6121650f27caa6ae932.zip
Remove double copy of array (to_owned() and PathBuf::from), use OsString::from_vec on existing path var rather than construct string from pointer
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs15
1 files changed, 7 insertions, 8 deletions
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<P: ?Sized + NixPath>(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"))]