Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple bitflags in one macro invocation #212

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions .github/workflows/rust.yml
@@ -0,0 +1,81 @@
name: Rust

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
check:
name: Test
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
rust:
- stable
- beta
- nightly
- 1.32.0
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true

- name: Default features
uses: actions-rs/cargo@v1
with:
command: test
args: --features example_generated

embedded:
name: Build (embedded)
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
target: thumbv6m-none-eabi
override: true

- name: Default features
uses: actions-rs/cargo@v1
with:
command: build
args: -Z avoid-dev-deps --features example_generated --target thumbv6m-none-eabi

suite:
name: Test suite
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
rust:
- nightly
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true

- name: Default features
uses: actions-rs/cargo@v1
with:
command: test
args: -p test_suite
39 changes: 0 additions & 39 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 1.3.0

- Add support for multiple bitflags in one macro invocation.

# 1.2.1

- Remove extraneous `#[inline]` attributes ([#194])
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Expand Up @@ -4,7 +4,7 @@ name = "bitflags"
# NB: When modifying, also modify:
# 1. html_root_url in lib.rs
# 2. number in readme (for breaking changes)
version = "1.2.1"
version = "1.3.0"
authors = ["The Rust Project Developers"]
license = "MIT/Apache-2.0"
keywords = ["bit", "bitmask", "bitflags", "flags"]
Expand All @@ -23,12 +23,17 @@ exclude = [
]
build = "build.rs"

[dependencies]
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
compiler_builtins = { version = '0.1.2', optional = true }

[badges]
travis-ci = { repository = "bitflags/bitflags" }

[features]
default = []
example_generated = []
rustc-dep-of-std = ["core", "compiler_builtins"]

[package.metadata.docs.rs]
features = [ "example_generated" ]
Expand Down
5 changes: 2 additions & 3 deletions README.md
@@ -1,11 +1,10 @@
bitflags
========

[![Build Status](https://travis-ci.com/bitflags/bitflags.svg?branch=master)](https://travis-ci.com/bitflags/bitflags)
[![Rust](https://github.com/bitflags/bitflags/workflows/Rust/badge.svg)](https://github.com/bitflags/bitflags/actions)
[![Join the chat at https://gitter.im/bitflags/Lobby](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bitflags/Lobby?utm_source=badge&utm_medium=badge&utm_content=badge)
[![Latest version](https://img.shields.io/crates/v/bitflags.svg)](https://crates.io/crates/bitflags)
[![Documentation](https://docs.rs/bitflags/badge.svg)](https://docs.rs/bitflags)
![Minimum rustc version](https://img.shields.io/badge/rustc-1.20+-yellow.svg)
![License](https://img.shields.io/crates/l/bitflags.svg)

A Rust macro to generate structures which behave like a set of bitflags
Expand All @@ -19,7 +18,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
bitflags = "1.2"
bitflags = "1.3"
```

and this to your crate root:
Expand Down
48 changes: 25 additions & 23 deletions src/lib.rs
Expand Up @@ -11,7 +11,7 @@
//! A typesafe bitmask flag generator useful for sets of C-style bitmask flags.
//! It can be used for creating typesafe wrappers around C APIs.
//!
//! The `bitflags!` macro generates a `struct` that manages a set of flags. The
//! The `bitflags!` macro generates `struct`s that manage a set of flags. The
//! flags should only be defined for integer types, otherwise unexpected type
//! errors may occur at compile time.
//!
Expand Down Expand Up @@ -84,9 +84,9 @@
//!
//! # Visibility
//!
//! The generated struct and its associated flag constants are not exported
//! The generated structs and their associated flag constants are not exported
//! out of the current module by default. A definition can be exported out of
//! the current module by adding `pub` before `flags`:
//! the current module by adding `pub` before `struct`:
//!
//! ```
//! #[macro_use]
Expand All @@ -97,8 +97,7 @@
//! pub struct Flags1: u32 {
//! const A = 0b00000001;
//! }
//! }
//! bitflags! {
//!
//! # pub
//! struct Flags2: u32 {
//! const B = 0b00000010;
Expand All @@ -114,17 +113,17 @@
//!
//! # Attributes
//!
//! Attributes can be attached to the generated `struct` by placing them
//! before the `flags` keyword.
//! Attributes can be attached to the generated `struct`s by placing them
//! before the `struct` keyword.
//!
//! # Trait implementations
//!
//! The `Copy`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`
//! traits automatically derived for the `struct` using the `derive` attribute.
//! traits are automatically derived for the `struct`s using the `derive` attribute.
//! Additional traits can be derived by providing an explicit `derive`
//! attribute on `flags`.
//! attribute on `struct`.
//!
//! The `Extend` and `FromIterator` traits are implemented for the `struct`,
//! The `Extend` and `FromIterator` traits are implemented for the `struct`s,
//! too: `Extend` adds the union of the instances of the `struct` iterated over,
//! while `FromIterator` calculates the union.
//!
Expand All @@ -133,7 +132,7 @@
//!
//! ## Operators
//!
//! The following operator traits are implemented for the generated `struct`:
//! The following operator traits are implemented for the generated `struct`s:
//!
//! - `BitOr` and `BitOrAssign`: union
//! - `BitAnd` and `BitAndAssign`: intersection
Expand All @@ -143,7 +142,7 @@
//!
//! # Methods
//!
//! The following methods are defined for the generated `struct`:
//! The following methods are defined for the generated `struct`s:
//!
//! - `empty`: an empty set of flags
//! - `all`: the set of all defined flags
Expand All @@ -168,7 +167,7 @@
//!
//! ## Default
//!
//! The `Default` trait is not automatically implemented for the generated struct.
//! The `Default` trait is not automatically implemented for the generated structs.
//!
//! If your default value is equal to `0` (which is the same value as calling `empty()`
//! on the generated struct), you can simply derive `Default`:
Expand Down Expand Up @@ -251,7 +250,7 @@
//! ```

#![no_std]
#![doc(html_root_url = "https://docs.rs/bitflags/1.2.1")]
#![doc(html_root_url = "https://docs.rs/bitflags/1.3.0")]

#[cfg(test)]
#[macro_use]
Expand All @@ -262,7 +261,7 @@ extern crate std;
#[doc(hidden)]
pub extern crate core as _core;

/// The macro used to generate the flag structure.
/// The macro used to generate the flag structures.
///
/// See the [crate level docs](../bitflags/index.html) for complete documentation.
///
Expand Down Expand Up @@ -339,6 +338,7 @@ macro_rules! bitflags {
const $Flag:ident = $value:expr;
)+
}
$($t:tt)*
) => {
__bitflags! {
$(#[$outer])*
Expand All @@ -348,6 +348,7 @@ macro_rules! bitflags {
$Flag = $value;
)+
}
$($t)*
}
};
(
Expand All @@ -358,6 +359,7 @@ macro_rules! bitflags {
const $Flag:ident = $value:expr;
)+
}
$($t:tt)*
) => {
__bitflags! {
$(#[$outer])*
Expand All @@ -367,6 +369,7 @@ macro_rules! bitflags {
$Flag = $value;
)+
}
$($t)*
}
};
(
Expand All @@ -377,6 +380,7 @@ macro_rules! bitflags {
const $Flag:ident = $value:expr;
)+
}
$($t:tt)*
) => {
__bitflags! {
$(#[$outer])*
Expand All @@ -386,8 +390,10 @@ macro_rules! bitflags {
$Flag = $value;
)+
}
$($t)*
}
};
() => {};
}

#[macro_export(local_inner_macros)]
Expand All @@ -401,6 +407,7 @@ macro_rules! __bitflags {
$Flag:ident = $value:expr;
)+
}
$($t:tt)*
) => {
$(#[$outer])*
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
Expand All @@ -416,6 +423,8 @@ macro_rules! __bitflags {
)+
}
}

bitflags!($($t)*);
};
}

Expand Down Expand Up @@ -958,9 +967,7 @@ mod tests {
#[doc = "<strcat> wait what?"]
const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
}
}

bitflags! {
struct _CfgFlags: u32 {
#[cfg(unix)]
const _CFG_A = 0b01;
Expand All @@ -969,15 +976,11 @@ mod tests {
#[cfg(unix)]
const _CFG_C = Self::_CFG_A.bits | 0b10;
}
}

bitflags! {
struct AnotherSetOfFlags: i8 {
const ANOTHER_FLAG = -1_i8;
}
}

bitflags! {
struct LongFlags: u32 {
const LONG_A = 0b1111111111111111;
}
Expand Down Expand Up @@ -1320,8 +1323,7 @@ mod tests {
pub struct PublicFlags: i8 {
const X = 0;
}
}
bitflags! {

struct PrivateFlags: i8 {
const Y = 0;
}
Expand Down
13 changes: 12 additions & 1 deletion test_suite/tests/compile-fail/private_flags.stderr
Expand Up @@ -2,4 +2,15 @@ error[E0603]: struct `Flags2` is private
--> $DIR/private_flags.rs:19:26
|
19 | let flag2 = example::Flags2::FLAG_B;
| ^^^^^^
| ^^^^^^ private struct
|
note: the struct `Flags2` is defined here
--> $DIR/private_flags.rs:10:5
|
10 | / bitflags! {
11 | | struct Flags2: u32 {
12 | | const FLAG_B = 0b00000010;
13 | | }
14 | | }
| |_____^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)