From 2946b319dd75220b4a32c43952388a0df90eee98 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Sun, 9 Jun 2019 11:27:44 -0700 Subject: [PATCH] Use ptr::NonNull for Dir This is a minor optimization that allows for reduced sizes of datatypes. Since this pointer will never be NULL, it's safe to use here --- src/dir.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index d4fc43a4d0..bdeb00f9ef 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -27,8 +27,7 @@ use libc::{dirent, readdir_r}; /// does). #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct Dir( - // This could be ptr::NonNull once nix requires Rust 1.25. - *mut libc::DIR + ptr::NonNull ); impl Dir { @@ -60,7 +59,7 @@ impl Dir { unsafe { libc::close(fd) }; return Err(e); }; - Ok(Dir(d)) + Ok(Dir(unsafe { ptr::NonNull::new_unchecked(d) })) } /// Returns an iterator of `Result` which rewinds when finished. @@ -79,13 +78,13 @@ unsafe impl Send for Dir {} impl AsRawFd for Dir { fn as_raw_fd(&self) -> RawFd { - unsafe { libc::dirfd(self.0) } + unsafe { libc::dirfd(self.0.as_ptr()) } } } impl Drop for Dir { fn drop(&mut self) { - unsafe { libc::closedir(self.0) }; + unsafe { libc::closedir(self.0.as_ptr()) }; } } @@ -104,7 +103,7 @@ impl<'d> Iterator for Iter<'d> { // Probably fine here too then. let mut ent: Entry = Entry(::std::mem::uninitialized()); let mut result = ptr::null_mut(); - if let Err(e) = Errno::result(readdir_r((self.0).0, &mut ent.0, &mut result)) { + if let Err(e) = Errno::result(readdir_r((self.0).0.as_ptr(), &mut ent.0, &mut result)) { return Some(Err(e)); } if result == ptr::null_mut() { @@ -118,7 +117,7 @@ impl<'d> Iterator for Iter<'d> { impl<'d> Drop for Iter<'d> { fn drop(&mut self) { - unsafe { libc::rewinddir((self.0).0) } + unsafe { libc::rewinddir((self.0).0.as_ptr()) } } }