Skip to content

Commit

Permalink
Upgrade to Bitflags 1.0
Browse files Browse the repository at this point in the history
The libc_bitflags! macro was replaced with a non-recursive one supporting
only public structs. I could not figure out how to make the old macro work
with the upgrade, so I reworked part of the bitflags! macro directly to suit
our needs, much as the original recursive macro was made. There are no uses
of this macro for non-public structs, so this is not a problem for internal code.
  • Loading branch information
Bryant Mairs committed Nov 22, 2017
1 parent 6a2feaa commit cf203e0
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 267 deletions.
58 changes: 29 additions & 29 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,67 @@ matrix:
include:
# Android
- env: TARGET=aarch64-linux-android DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
- env: TARGET=arm-linux-androideabi DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
- env: TARGET=armv7-linux-androideabi DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
- env: TARGET=aarch64-apple-ios DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=i686-linux-android DISABLE_TESTS=1
rust: 1.18.0
rust: 1.20.0
- env: TARGET=x86_64-linux-android DISABLE_TESTS=1
rust: 1.18.0
rust: 1.20.0

# Linux
- env: TARGET=aarch64-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=armv7-apple-ios DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=arm-unknown-linux-gnueabi
rust: 1.13.0
rust: 1.20.0
- env: TARGET=arm-unknown-linux-musleabi DISABLE_TESTS=1
rust: 1.14.0
rust: 1.20.0
- env: TARGET=armv7-unknown-linux-gnueabihf
rust: 1.13.0
rust: 1.20.0
- env: TARGET=armv7s-apple-ios DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=i686-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=i686-unknown-linux-musl
rust: 1.13.0
rust: 1.20.0
- env: TARGET=mips-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=i386-apple-ios DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=mips64-unknown-linux-gnuabi64
rust: 1.13.0
rust: 1.20.0
- env: TARGET=mips64el-unknown-linux-gnuabi64
rust: 1.13.0
rust: 1.20.0
- env: TARGET=mipsel-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=x86_64-apple-ios DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=powerpc-unknown-linux-gnu DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
- env: TARGET=powerpc64-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=powerpc64le-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=i686-apple-darwin
rust: 1.13.0
rust: 1.20.0
os: osx
- env: TARGET=s390x-unknown-linux-gnu DISABLE_TESTS=1
rust: 1.13.0
rust: 1.20.0
- env: TARGET=x86_64-unknown-linux-gnu
rust: 1.13.0
rust: 1.20.0
- env: TARGET=x86_64-unknown-linux-musl
rust: 1.13.0
rust: 1.20.0

# *BSD
# FreeBSD i686 and x86_64 use BuildBot instead of Travis
Expand All @@ -87,10 +87,10 @@ matrix:
# - 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.13.0
rust: 1.20.0

- env: TARGET=x86_64-apple-darwin
rust: 1.13.0
rust: 1.20.0
os: osx

# Testing beta on main targets
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" }
bitflags = "0.9"
bitflags = "1.0"
cfg-if = "0.1.0"
void = "1.0.2"

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ limitations. Support for platforms is split into two 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.13.0 or newer (unless
The following targets are all supported by nix on Rust 1.20.0 or newer (unless
otherwise noted):

Tier 1:
Expand All @@ -72,16 +72,16 @@ Tier 2:
* aarch64-apple-ios
* aarch64-linux-android
* arm-linux-androideabi
* arm-unknown-linux-musleabi (requires Rust >= 1.14)
* arm-unknown-linux-musleabi
* armv7-apple-ios
* armv7-linux-androideabi
* armv7s-apple-ios
* i386-apple-ios
* i686-linux-android (requires Rust >= 1.18)
* i686-linux-android
* powerpc-unknown-linux-gnu
* s390x-unknown-linux-gnu
* x86_64-apple-ios
* x86_64-linux-android (requires Rust >= 1.18)
* x86_64-linux-android
* x86_64-unknown-netbsd

## Usage
Expand Down
211 changes: 16 additions & 195 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// The `libc_bitflags!` macro helps with a common use case of defining bitflags with values from
/// the libc crate. It is used the same way as the `bitflags!` macro, except that only the name of
/// the flag value has to be given.
/// The `libc_bitflags!` macro helps with a common use case of defining a public bitflags type
/// with values from the libc crate. It is used the same way as the `bitflags!` macro, except
/// that only the name of the flag value has to be given.
///
/// The `libc` crate must be in scope with the name `libc`.
///
Expand All @@ -10,6 +10,7 @@
/// pub struct ProtFlags: libc::c_int {
/// PROT_NONE;
/// PROT_READ;
/// /// PROT_WRITE enables write protect
/// PROT_WRITE;
/// PROT_EXEC;
/// #[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down Expand Up @@ -38,205 +39,25 @@
/// }
/// ```
macro_rules! libc_bitflags {
// (non-pub) Exit rule.
(@call_bitflags
{
name: $BitFlags:ident,
type: $T:ty,
attrs: [$($attrs:tt)*],
flags: [$($flags:tt)*],
}
) => {
bitflags! {
$($attrs)*
struct $BitFlags: $T {
$($flags)*
}
}
};

// (pub) Exit rule.
(@call_bitflags
{
pub,
name: $BitFlags:ident,
type: $T:ty,
attrs: [$($attrs:tt)*],
flags: [$($flags:tt)*],
(
$(#[$outer:meta])*
pub struct $BitFlags:ident: $T:ty {
$(
$(#[$inner:ident $($args:tt)*])*
$Flag:ident $(as $cast:ty)*;
)+
}
) => {
bitflags! {
$($attrs)*
$(#[$outer])*
pub struct $BitFlags: $T {
$($flags)*
}
}
};

// (non-pub) Done accumulating.
(@accumulate_flags
{
name: $BitFlags:ident,
type: $T:ty,
attrs: $attrs:tt,
},
$flags:tt;
) => {
libc_bitflags! {
@call_bitflags
{
name: $BitFlags,
type: $T,
attrs: $attrs,
flags: $flags,
$(
$(#[$inner $($args)*])*
const $Flag = libc::$Flag $(as $cast)*;
)+
}
}
};

// (pub) Done accumulating.
(@accumulate_flags
{
pub,
name: $BitFlags:ident,
type: $T:ty,
attrs: $attrs:tt,
},
$flags:tt;
) => {
libc_bitflags! {
@call_bitflags
{
pub,
name: $BitFlags,
type: $T,
attrs: $attrs,
flags: $flags,
}
}
};

// Munch an attr.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
#[$attr:meta] $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
#[$attr]
];
$($tail)*
}
};

// Munch last ident if not followed by a semicolon.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag;
];
}
};

// Munch last ident and cast it to the given type.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
}
};

// Munch an ident; covers terminating semicolon case.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident; $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag;
];
$($tail)*
}
};

// 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)*
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
$($tail)*
}
};

// (non-pub) Entry rule.
(
$(#[$attr:meta])*
struct $BitFlags:ident: $T:ty {
$($vals:tt)*
}
) => {
libc_bitflags! {
@accumulate_flags
{
name: $BitFlags,
type: $T,
attrs: [$(#[$attr])*],
},
[];
$($vals)*
}
};

// (pub) Entry rule.
(
$(#[$attr:meta])*
pub struct $BitFlags:ident: $T:ty {
$($vals:tt)*
}
) => {
libc_bitflags! {
@accumulate_flags
{
pub,
name: $BitFlags,
type: $T,
attrs: [$(#[$attr])*],
},
[];
$($vals)*
}
};
}

/// The `libc_enum!` macro helps with a common use case of defining an enum exclusively using
Expand Down
2 changes: 1 addition & 1 deletion src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
/// Returns the old attributes
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = try!(mq_getattr(mqd));
let newattr = MqAttr::new(O_NONBLOCK.bits() as c_long,
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
oldattr.mq_attr.mq_maxmsg,
oldattr.mq_attr.mq_msgsize,
oldattr.mq_attr.mq_curmsgs);
Expand Down
Loading

0 comments on commit cf203e0

Please sign in to comment.