Skip to content

Commit

Permalink
Merge #1035
Browse files Browse the repository at this point in the history
1035: Implement extra traits for all types r=asomers a=Susurrus

Now that I've gotten all the extra traits implemented for `libc`, they can be easily derived for `nix`'s types. Note that this requires a bump to the minimum supported Rust version, but it's still at least 2 versions behind, so it fits in with policy.

One thing I did notice is that we have an inconsistent approach to our newtypes, where some use a struct and some a tuple struct. We should be consistent here, and should probably use a tuple struct since the name of the single field is irrelevant. This style is already suggested in our `CONVENTIONS.md` doc, so this should be uncontroversial. I'll file a PR after this is merged adding that.

Co-authored-by: Bryant Mairs <bryant@mai.rs>
  • Loading branch information
bors[bot] and Susurrus committed Jun 12, 2019
2 parents 83407c5 + 37929c7 commit 50f55e7
Show file tree
Hide file tree
Showing 34 changed files with 202 additions and 647 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ task:
setup_script:
- pkg install -y curl
- curl https://sh.rustup.rs -sSf --output rustup.sh
- sh rustup.sh -y --default-toolchain 1.24.1
- sh rustup.sh -y --default-toolchain 1.25.0
- $HOME/.cargo/bin/rustup target add i686-unknown-freebsd
amd64_test_script:
- . $HOME/.cargo/env
Expand Down
50 changes: 25 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,71 @@ matrix:
# week. Additionally they're moved to the front of the line to get them in
# the Travis OS X build queue first.
- env: TARGET="aarch64-apple-ios;armv7-apple-ios;armv7s-apple-ios;i386-apple-ios;x86_64-apple-ios" DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
os: osx

# Mac builds
# These are also moved to be first because they wait in a long queue with
# Travis
- env: TARGET=i686-apple-darwin
rust: 1.24.1
rust: 1.25.0
os: osx
- env: TARGET=x86_64-apple-darwin
rust: 1.24.1
rust: 1.25.0
os: osx

# Android
- env: TARGET=aarch64-linux-android DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=arm-linux-androideabi DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=armv7-linux-androideabi DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=i686-linux-android DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=x86_64-linux-android DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0

# Linux
- env: TARGET=aarch64-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=arm-unknown-linux-gnueabi
rust: 1.24.1
rust: 1.25.0
- env: TARGET=arm-unknown-linux-musleabi DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=armv7-unknown-linux-gnueabihf
rust: 1.24.1
rust: 1.25.0
- env: TARGET=i686-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=i686-unknown-linux-musl
rust: 1.24.1
rust: 1.25.0
- env: TARGET=mips-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=mips64-unknown-linux-gnuabi64
rust: 1.24.1
rust: 1.25.0
- env: TARGET=mips64el-unknown-linux-gnuabi64
rust: 1.24.1
rust: 1.25.0
- env: TARGET=mipsel-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=powerpc-unknown-linux-gnu DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=powerpc64-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=powerpc64le-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=s390x-unknown-linux-gnu DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0
- env: TARGET=x86_64-unknown-linux-gnu
rust: 1.24.1
rust: 1.25.0
- env: TARGET=x86_64-unknown-linux-musl
rust: 1.24.1
rust: 1.25.0

# *BSD
# FreeBSD i686 and x86_64 use Cirrus instead of Travis
# - env: TARGET=i686-unknown-freebsd DISABLE_TESTS=1
# - env: TARGET=x86_64-unknown-freebsd DISABLE_TESTS=1
- env: TARGET=x86_64-unknown-netbsd DISABLE_TESTS=1
rust: 1.24.1
rust: 1.25.0

# Make sure stable is always working too
- env: TARGET=x86_64-unknown-linux-gnu
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Added `MSG_WAITALL` to `MsgFlags` in `sys::socket`.
([#1079](https://github.com/nix-rust/nix/pull/1079))
- Implemented `Clone`, `Copy`, `Debug`, `Eq`, `Hash`, and `PartialEq` for most
types that support them. ([#1035](https://github.com/nix-rust/nix/pull/1035))

### Changed
- Support for `ifaddrs` now present when building for Android.
([#1077](https://github.com/nix-rust/nix/pull/1077))
- Minimum supported Rust version is now 1.25.0
([#1035](https://github.com/nix-rust/nix/pull/1035))

### Fixed
### Removed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = [
]

[dependencies]
libc = { git = "https://github.com/rust-lang/libc" }
libc = { git = "https://github.com/rust-lang/libc", features = [ "extra_traits" ] }
bitflags = "1.0"
cfg-if = "0.1.0"
void = "1.0.2"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ limitations. Support for platforms is split into three tiers:
*do not* block the inclusion of new code. Testing may be run, but
failures in tests don't block the inclusion of new code.

The following targets are all supported by nix on Rust 1.24.1 or newer (unless
otherwise noted):
The following targets are supported by `nix`:

Tier 1:
* aarch64-unknown-linux-gnu
Expand Down Expand Up @@ -84,6 +83,8 @@ Tier 2:

## Usage

`nix` requires Rust 1.25.0 or newer.

To use `nix`, first add this to your `Cargo.toml`:

```toml
Expand Down
41 changes: 12 additions & 29 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use errno::Errno;
use fcntl::{self, OFlag};
use libc;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::{ffi, fmt, ptr};
use std::{ffi, ptr};
use sys;

#[cfg(target_os = "linux")]
Expand All @@ -25,9 +25,9 @@ use libc::{dirent, readdir_r};
/// * returns entries for `.` (current directory) and `..` (parent directory).
/// * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc
/// 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 @@ -59,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 @@ -78,25 +79,17 @@ unsafe impl Send for Dir {}

impl AsRawFd for Dir {
fn as_raw_fd(&self) -> RawFd {
unsafe { libc::dirfd(self.0) }
}
}

impl fmt::Debug for Dir {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Dir")
.field("fd", &self.as_raw_fd())
.finish()
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()) };
}
}

#[derive(Debug)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Iter<'d>(&'d mut Dir);

impl<'d> Iterator for Iter<'d> {
Expand All @@ -111,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 @@ -125,17 +118,17 @@ 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()) }
}
}

/// A directory entry, similar to `std::fs::DirEntry`.
///
/// Note that unlike the std version, this may represent the `.` or `..` entries.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Entry(dirent);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum Type {
Fifo,
CharacterDevice,
Expand Down Expand Up @@ -198,13 +191,3 @@ impl Entry {
}
}
}

impl fmt::Debug for Entry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Entry")
.field("ino", &self.ino())
.field("file_name", &self.file_name())
.field("file_type", &self.file_type())
.finish()
}
}
5 changes: 2 additions & 3 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ libc_bitflags!(
}
);

#[allow(missing_debug_implementations)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub enum FcntlArg<'a> {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down Expand Up @@ -277,8 +277,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
Errno::result(res)
}

#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum FlockArg {
LockShared,
LockExclusive,
Expand Down
2 changes: 1 addition & 1 deletion src/ifaddrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sys::socket::SockAddr;
use net::if_::*;

/// Describes a single address for an interface as returned by `getifaddrs`.
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct InterfaceAddress {
/// Name of the network interface
pub interface_name: String,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub type Result<T> = result::Result<T, Error>;
/// error has a corresponding errno (usually the one from the
/// underlying OS) to which it can be mapped in addition to
/// implementing other common traits.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Error {
Sys(Errno),
InvalidPath,
Expand Down
13 changes: 1 addition & 12 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,11 @@ libc_bitflags!{
}

#[repr(C)]
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct MqAttr {
mq_attr: libc::mq_attr,
}

impl PartialEq<MqAttr> for MqAttr {
fn eq(&self, other: &MqAttr) -> bool {
let self_attr = self.mq_attr;
let other_attr = other.mq_attr;
self_attr.mq_flags == other_attr.mq_flags && self_attr.mq_maxmsg == other_attr.mq_maxmsg &&
self_attr.mq_msgsize == other_attr.mq_msgsize &&
self_attr.mq_curmsgs == other_attr.mq_curmsgs
}
}

impl MqAttr {
pub fn new(mq_flags: c_long,
mq_maxmsg: c_long,
Expand Down
20 changes: 1 addition & 19 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use sys::time::TimeSpec;
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
use sys::signal::SigSet;
use std::os::unix::io::RawFd;
use std::fmt;

use libc;
use Result;
Expand All @@ -19,7 +18,7 @@ use errno::Errno;
/// After a call to `poll` or `ppoll`, the events that occured can be
/// retrieved by calling [`revents()`](#method.revents) on the `PollFd`.
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PollFd {
pollfd: libc::pollfd,
}
Expand All @@ -43,23 +42,6 @@ impl PollFd {
}
}

impl fmt::Debug for PollFd {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let pfd = self.pollfd;
let mut ds = f.debug_struct("PollFd");
ds.field("fd", &pfd.fd);
match PollFlags::from_bits(pfd.events) {
None => ds.field("events", &pfd.events),
Some(ef) => ds.field("events", &ef),
};
match PollFlags::from_bits(pfd.revents) {
None => ds.field("revents", &pfd.revents),
Some(ef) => ds.field("revents", &ef),
};
ds.finish()
}
}

libc_bitflags! {
/// These flags define the different events that can be monitored by `poll` and `ppoll`
pub struct PollFlags: libc::c_short {
Expand Down
5 changes: 2 additions & 3 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use errno::Errno;
///
/// This is returned by `openpty`. Note that this type does *not* implement `Drop`, so the user
/// must manually close the file descriptors.
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct OpenptyResult {
/// The master port in a virtual pty pair
pub master: RawFd,
Expand All @@ -45,7 +44,7 @@ pub struct ForkptyResult {
/// While this datatype is a thin wrapper around `RawFd`, it enforces that the available PTY
/// functions are given the correct file descriptor. Additionally this type implements `Drop`,
/// so that when it's consumed or goes out of scope, it's automatically cleaned-up.
#[derive(Debug)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PtyMaster(RawFd);

impl AsRawFd for PtyMaster {
Expand Down
3 changes: 1 addition & 2 deletions src/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ libc_bitflags!{
pub type CloneCb<'a> = Box<FnMut() -> isize + 'a>;

#[repr(C)]
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CpuSet {
cpu_set: libc::cpu_set_t,
}
Expand Down
Loading

0 comments on commit 50f55e7

Please sign in to comment.