Skip to content

Commit

Permalink
Merge #700 #725
Browse files Browse the repository at this point in the history
700: Get rid of a lot of transmutes r=asomers

Most could be replaced by simple raw pointer casts (or even perfectly
safe coercions!).

cc #373

725: Match syntax of libc_bitflags! with bitflags! r=asomers

Also update a couple of constant declarations while we're at it.
  • Loading branch information
bors[bot] committed Aug 17, 2017
3 parents c48d48b + 9c9af2c + 4e2bf09 commit 16fe29d
Show file tree
Hide file tree
Showing 23 changed files with 614 additions and 631 deletions.
14 changes: 7 additions & 7 deletions CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ For example,

```rust
libc_bitflags!{
pub flags ProtFlags: libc::c_int {
PROT_NONE,
PROT_READ,
PROT_WRITE,
PROT_EXEC,
pub struct ProtFlags: libc::c_int {
PROT_NONE;
PROT_READ;
PROT_WRITE;
PROT_EXEC;
#[cfg(any(target_os = "linux", target_os = "android"))]
PROT_GROWSDOWN,
PROT_GROWSDOWN;
#[cfg(any(target_os = "linux", target_os = "android"))]
PROT_GROWSUP,
PROT_GROWSUP;
}
}
```
Expand Down
93 changes: 42 additions & 51 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,16 @@ mod ffi {
pub const F_GET_SEALS: c_int = 1034;
}

#[cfg(not(any(target_os = "ios", target_os = "macos")))]
libc_bitflags!{
pub flags AtFlags: c_int {
AT_SYMLINK_NOFOLLOW,
pub struct AtFlags: c_int {
AT_SYMLINK_NOFOLLOW;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_NO_AUTOMOUNT,
AT_NO_AUTOMOUNT;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_EMPTY_PATH
AT_EMPTY_PATH;
}
}

#[cfg(any(target_os = "ios", target_os = "macos"))]
bitflags!(
pub struct AtFlags: c_int {
// hack because bitflags require one entry
const EMPTY = 0x0;
}
);

pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| {
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
Expand All @@ -54,7 +45,7 @@ pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: M
Errno::result(fd)
}

fn wrap_readlink_result<'a>(buffer: &'a mut[u8], res: ssize_t)
fn wrap_readlink_result<'a>(buffer: &'a mut[u8], res: ssize_t)
-> Result<&'a OsStr> {
match Errno::result(res) {
Err(err) => Err(err),
Expand Down Expand Up @@ -204,11 +195,11 @@ mod consts {
use libc::{self, c_int, c_uint};

libc_bitflags! {
pub flags SpliceFFlags: c_uint {
SPLICE_F_MOVE,
SPLICE_F_NONBLOCK,
SPLICE_F_MORE,
SPLICE_F_GIFT,
pub struct SpliceFFlags: c_uint {
SPLICE_F_MOVE;
SPLICE_F_NONBLOCK;
SPLICE_F_MORE;
SPLICE_F_GIFT;
}
}

Expand Down Expand Up @@ -239,8 +230,8 @@ mod consts {
);

libc_bitflags!(
pub flags FdFlag: c_int {
FD_CLOEXEC
pub struct FdFlag: c_int {
FD_CLOEXEC;
}
);

Expand All @@ -261,49 +252,49 @@ mod consts {
use libc::{self,c_int};

libc_bitflags!(
pub flags OFlag: c_int {
O_ACCMODE,
O_RDONLY,
O_WRONLY,
O_RDWR,
O_NONBLOCK,
O_APPEND,
O_SHLOCK,
O_EXLOCK,
O_ASYNC,
O_SYNC,
O_NOFOLLOW,
O_CREAT,
O_TRUNC,
O_EXCL,
O_NOCTTY,
O_DIRECTORY,
O_CLOEXEC,
O_FSYNC,
O_NDELAY,
pub struct OFlag: c_int {
O_ACCMODE;
O_RDONLY;
O_WRONLY;
O_RDWR;
O_NONBLOCK;
O_APPEND;
O_SHLOCK;
O_EXLOCK;
O_ASYNC;
O_SYNC;
O_NOFOLLOW;
O_CREAT;
O_TRUNC;
O_EXCL;
O_NOCTTY;
O_DIRECTORY;
O_CLOEXEC;
O_FSYNC;
O_NDELAY;
#[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "macos",
target_os = "ios"))]
O_DSYNC,
O_DSYNC;
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "freebsd"))]
O_DIRECT,
O_DIRECT;
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
O_RSYNC,
O_RSYNC;
#[cfg(target_os = "freebsd")]
O_EXEC,
O_EXEC;
#[cfg(target_os = "freebsd")]
O_TTY_INIT,
O_TTY_INIT;
#[cfg(target_os = "netbsd")]
O_ALT_IO,
O_ALT_IO;
#[cfg(target_os = "netbsd")]
O_NOSIGPIPE,
O_NOSIGPIPE;
#[cfg(target_os = "netbsd")]
O_SEARCH,
O_SEARCH;
}
);

libc_bitflags!(
pub flags FdFlag: c_int {
FD_CLOEXEC
pub struct FdFlag: c_int {
FD_CLOEXEC;
}
);
}
46 changes: 23 additions & 23 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
/// # Example
/// ```
/// libc_bitflags!{
/// pub flags ProtFlags: libc::c_int {
/// PROT_NONE,
/// PROT_READ,
/// PROT_WRITE,
/// PROT_EXEC,
/// pub struct ProtFlags: libc::c_int {
/// PROT_NONE;
/// PROT_READ;
/// PROT_WRITE;
/// PROT_EXEC;
/// #[cfg(any(target_os = "linux", target_os = "android"))]
/// PROT_GROWSDOWN,
/// PROT_GROWSDOWN;
/// #[cfg(any(target_os = "linux", target_os = "android"))]
/// PROT_GROWSUP,
/// PROT_GROWSUP;
/// }
/// }
/// ```
Expand All @@ -26,14 +26,14 @@
///
/// ```
/// libc_bitflags!{
/// pub flags SaFlags: libc::c_ulong {
/// SA_NOCLDSTOP as libc::c_ulong,
/// SA_NOCLDWAIT,
/// SA_NODEFER as libc::c_ulong,
/// SA_ONSTACK,
/// SA_RESETHAND as libc::c_ulong,
/// SA_RESTART as libc::c_ulong,
/// SA_SIGINFO,
/// pub struct SaFlags: libc::c_ulong {
/// SA_NOCLDSTOP as libc::c_ulong;
/// SA_NOCLDWAIT;
/// SA_NODEFER as libc::c_ulong;
/// SA_ONSTACK;
/// SA_RESETHAND as libc::c_ulong;
/// SA_RESTART as libc::c_ulong;
/// SA_SIGINFO;
/// }
/// }
/// ```
Expand All @@ -49,7 +49,7 @@ macro_rules! libc_bitflags {
) => {
bitflags! {
$($attrs)*
flags $BitFlags: $T {
struct $BitFlags: $T {
$($flags)*
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ macro_rules! libc_bitflags {
}
};

// Munch last ident if not followed by a comma.
// Munch last ident if not followed by a semicolon.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
Expand Down Expand Up @@ -164,11 +164,11 @@ macro_rules! libc_bitflags {
}
};

// Munch an ident; covers terminating comma case.
// Munch an ident; covers terminating semicolon case.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident, $($tail:tt)*
$flag:ident; $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
Expand All @@ -181,12 +181,12 @@ macro_rules! libc_bitflags {
}
};

// Munch an ident and cast it to the given type; covers terminating comma
// Munch an ident and cast it to the given type; covers terminating semicolon
// case.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty, $($tail:tt)*
$flag:ident as $ty:ty; $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
Expand All @@ -202,7 +202,7 @@ macro_rules! libc_bitflags {
// (non-pub) Entry rule.
(
$(#[$attr:meta])*
flags $BitFlags:ident: $T:ty {
struct $BitFlags:ident: $T:ty {
$($vals:tt)*
}
) => {
Expand All @@ -221,7 +221,7 @@ macro_rules! libc_bitflags {
// (pub) Entry rule.
(
$(#[$attr:meta])*
pub flags $BitFlags:ident: $T:ty {
pub struct $BitFlags:ident: $T:ty {
$($vals:tt)*
}
) => {
Expand Down
8 changes: 4 additions & 4 deletions src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ bitflags!(
);

libc_bitflags!(
pub flags MntFlags: c_int {
MNT_FORCE,
MNT_DETACH,
MNT_EXPIRE,
pub struct MntFlags: c_int {
MNT_FORCE;
MNT_DETACH;
MNT_EXPIRE;
}
);

Expand Down
20 changes: 10 additions & 10 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ use sys::stat::Mode;
use std::mem;

libc_bitflags!{
pub flags MQ_OFlag: libc::c_int {
O_RDONLY,
O_WRONLY,
O_RDWR,
O_CREAT,
O_EXCL,
O_NONBLOCK,
O_CLOEXEC,
pub struct MQ_OFlag: libc::c_int {
O_RDONLY;
O_WRONLY;
O_RDWR;
O_CREAT;
O_EXCL;
O_NONBLOCK;
O_CLOEXEC;
}
}

libc_bitflags!{
pub flags FdFlag: libc::c_int {
FD_CLOEXEC,
pub struct FdFlag: libc::c_int {
FD_CLOEXEC;
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl PollFd {

libc_bitflags! {
/// These flags define the different events that can be monitored by `poll` and `ppoll`
pub flags EventFlags: libc::c_short {
pub struct EventFlags: libc::c_short {
/// There is data to read.
POLLIN,
POLLIN;
/// There is some exceptional condition on the file descriptor.
///
/// Possibilities include:
Expand All @@ -56,37 +56,37 @@ libc_bitflags! {
/// [ioctl_tty(2)](http://man7.org/linux/man-pages/man2/ioctl_tty.2.html)).
/// * A cgroup.events file has been modified (see
/// [cgroups(7)](http://man7.org/linux/man-pages/man7/cgroups.7.html)).
POLLPRI,
POLLPRI;
/// Writing is now possible, though a write larger that the
/// available space in a socket or pipe will still block (unless
/// `O_NONBLOCK` is set).
POLLOUT,
POLLOUT;
/// Equivalent to [`POLLIN`](constant.POLLIN.html)
POLLRDNORM,
POLLRDNORM;
/// Equivalent to [`POLLOUT`](constant.POLLOUT.html)
POLLWRNORM,
POLLWRNORM;
/// Priority band data can be read (generally unused on Linux).
POLLRDBAND,
POLLRDBAND;
/// Priority data may be written.
POLLWRBAND,
POLLWRBAND;
/// Error condition (only returned in
/// [`PollFd::revents`](struct.PollFd.html#method.revents);
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
/// This bit is also set for a file descriptor referring to the
/// write end of a pipe when the read end has been closed.
POLLERR,
POLLERR;
/// Hang up (only returned in [`PollFd::revents`](struct.PollFd.html#method.revents);
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
/// Note that when reading from a channel such as a pipe or a stream
/// socket, this event merely indicates that the peer closed its
/// end of the channel. Subsequent reads from the channel will
/// return 0 (end of file) only after all outstanding data in the
/// channel has been consumed.
POLLHUP,
POLLHUP;
/// Invalid request: `fd` not open (only returned in
/// [`PollFd::revents`](struct.PollFd.html#method.revents);
/// ignored in [`PollFd::new`](struct.PollFd.html#method.new)).
POLLNVAL,
POLLNVAL;
}
}

Expand Down
Loading

0 comments on commit 16fe29d

Please sign in to comment.