summaryrefslogtreecommitdiff
path: root/src/dir.rs
diff options
context:
space:
mode:
authorBryant Mairs <bryant@mai.rs>2019-06-09 11:27:44 -0700
committerBryant Mairs <bryant@mai.rs>2019-06-11 19:45:05 -0700
commit37929c7dafd86571730e4f566ef520cf0139e008 (patch)
tree87cee38dfd2eb8c7995a1afd3dc3f479b74412ed /src/dir.rs
parent3fdf3fe02d0a5816afc7b31db942cfe507e828d1 (diff)
downloadnix-37929c7dafd86571730e4f566ef520cf0139e008.zip
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
Diffstat (limited to 'src/dir.rs')
-rw-r--r--src/dir.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/dir.rs b/src/dir.rs
index d4fc43a4..1820b533 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<libc::DIR>
);
impl Dir {
@@ -60,7 +59,8 @@ impl Dir {
unsafe { libc::close(fd) };
return Err(e);
};
- Ok(Dir(d))
+ // Always guaranteed to be non-null by the previous check
+ Ok(Dir(ptr::NonNull::new(d).unwrap()))
}
/// Returns an iterator of `Result<Entry>` which rewinds when finished.
@@ -79,13 +79,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 +104,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 +118,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()) }
}
}