Skip to content

Commit 754a8de

Browse files
committed
Expand some aspects of the documentation
- Emphasize the distinction between `T` and `BitFlags<T>`. - Mention `make_bitflags` in the section about `const fn` APIs. - Add a `const` to the `make_bitflags` example. This is motivated by the discussion in #63.
1 parent aec9558 commit 754a8de

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
with `#[bitflags]`, and `BitFlags<YourEnum>` will be able to hold arbitrary combinations
1010
of your enum within the space of a single integer.
1111

12+
Unlike other crates, `enumflags2` makes the type-level distinction between
13+
a single flag (`YourEnum`) and a set of flags (`BitFlags<YourEnum>`).
14+
This allows idiomatic handling of bitflags, such as with `match` and `iter`.
15+
1216
## Features
1317

1418
- [x] Uses enums to represent individual flags&mdash;a set of flags is a separate type from a single flag.
@@ -75,13 +79,18 @@ context.
7579
**Naming convention:** If a separate, more limited function is provided
7680
for usage in a `const fn`, the name is suffixed with `_c`.
7781

82+
Apart from functions whose name ends with `_c`, the [`make_bitflags!`] macro
83+
is often useful for many `const` and `const fn` usecases.
84+
7885
**Blanket implementations:** If you attempt to write a `const fn` ranging
7986
over `T: BitFlag`, you will be met with an error explaining that currently,
8087
the only allowed trait bound for a `const fn` is `?Sized`. You will probably
8188
want to write a separate implementation for `BitFlags<T, u8>`,
82-
`BitFlags<T, u16>`, etc — probably generated by a macro.
83-
This strategy is often used by `enumflags2` itself; to avoid clutter, only
84-
one of the copies is shown in the documentation.
89+
`BitFlags<T, u16>`, etc — best accomplished by a simple macro.
90+
91+
**Documentation considerations:** The strategy described above is often used
92+
by `enumflags2` itself. To avoid clutter in the auto-generated documentation,
93+
the implementations for widths other than `u8` are marked with `#[doc(hidden)]`.
8594

8695
## Customizing `Default`
8796

src/lib.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
//! with `#[bitflags]`, and `BitFlags<YourEnum>` will be able to hold arbitrary combinations
44
//! of your enum within the space of a single integer.
55
//!
6+
//! Unlike other crates, `enumflags2` makes the type-level distinction between
7+
//! a single flag (`YourEnum`) and a set of flags (`BitFlags<YourEnum>`).
8+
//! This allows idiomatic handling of bitflags, such as with `match` and `iter`.
9+
//!
610
//! ## Example
711
//! ```
812
//! use enumflags2::{bitflags, make_bitflags, BitFlags};
@@ -58,6 +62,9 @@
5862
//! **Naming convention:** If a separate, more limited function is provided
5963
//! for usage in a `const fn`, the name is suffixed with `_c`.
6064
//!
65+
//! Apart from functions whose name ends with `_c`, the [`make_bitflags!`] macro
66+
//! is often useful for many `const` and `const fn` usecases.
67+
//!
6168
//! **Blanket implementations:** If you attempt to write a `const fn` ranging
6269
//! over `T: BitFlag`, you will be met with an error explaining that currently,
6370
//! the only allowed trait bound for a `const fn` is `?Sized`. You will probably
@@ -298,6 +305,11 @@ pub mod _internal {
298305
///
299306
/// The values should reflect reality, like they do if the implementation
300307
/// is generated by the procmacro.
308+
///
309+
/// `bits` must return the same value as
310+
/// [`transmute_copy`][std::mem::transmute_copy].
311+
///
312+
/// Representations for all values of `T` must have exactly one bit set.
301313
pub unsafe trait RawBitFlags: Copy + Clone + 'static {
302314
/// The underlying integer type.
303315
type Numeric: BitFlagNum;
@@ -533,17 +545,20 @@ pub struct BitFlags<T, N = <T as _internal::RawBitFlags>::Numeric> {
533545
/// `BitFlags<T>`. Instead of repeating the name of your type for each flag
534546
/// you want to add, try `make_bitflags!(Flags::{Foo | Bar})`.
535547
/// ```
536-
/// use enumflags2::{bitflags, make_bitflags};
537-
/// #[bitflags]
538-
/// #[repr(u8)]
539-
/// #[derive(Clone, Copy, Debug)]
540-
/// enum Test {
541-
/// A = 1 << 0,
542-
/// B = 1 << 1,
543-
/// C = 1 << 2,
544-
/// }
548+
/// # use enumflags2::{bitflags, BitFlags, make_bitflags};
549+
/// # #[bitflags]
550+
/// # #[repr(u8)]
551+
/// # #[derive(Clone, Copy, Debug)]
552+
/// # enum Test {
553+
/// # A = 1 << 0,
554+
/// # B = 1 << 1,
555+
/// # C = 1 << 2,
556+
/// # }
545557
/// let x = make_bitflags!(Test::{A | C});
546558
/// assert_eq!(x, Test::A | Test::C);
559+
///
560+
/// // Also works in const contexts:
561+
/// const X: BitFlags<Test> = make_bitflags!(Test::{A | C});
547562
/// ```
548563
#[macro_export]
549564
macro_rules! make_bitflags {

0 commit comments

Comments
 (0)