Skip to content

Commit

Permalink
Use ptr::NonNull for Dir
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Susurrus committed Jun 12, 2019
1 parent 3fdf3fe commit 37929c7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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()) };
}
}

Expand All @@ -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() {
Expand All @@ -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()) }
}
}

Expand Down

0 comments on commit 37929c7

Please sign in to comment.