Skip to content

Commit 1dab769

Browse files
committed
Last minute docs addition for the potentially confusing PartialOrd
Fixes #53
1 parent b5621c2 commit 1dab769

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,38 @@ pub use crate::const_api::ConstToken;
418418
/// A `BitFlags<T>` is as large as the `T` itself,
419419
/// and stores one flag per bit.
420420
///
421+
/// ## Comparison operators, [`PartialOrd`] and [`Ord`]
422+
///
423+
/// To make it possible to use `BitFlags` as the key of a
424+
/// [`BTreeMap`][std::collections::BTreeMap], `BitFlags` implements
425+
/// [`Ord`]. There is no meaningful total order for bitflags,
426+
/// so the implementation simply compares the integer values of the bits.
427+
///
428+
/// Unfortunately, this means that comparing `BitFlags` with an operator
429+
/// like `<=` will compile, and return values that are probably useless
430+
/// and not what you expect. In particular, `<=` does *not* check whether
431+
/// one value is a subset of the other. Use [`BitFlags::contains`] for that.
432+
///
433+
/// ## Customizing `Default`
434+
///
435+
/// By default, creating an instance of `BitFlags<T>` with `Default` will result
436+
/// in an empty set. If that's undesirable, you may customize this:
437+
///
438+
/// ```
439+
/// # use enumflags2::{BitFlags, bitflags};
440+
/// #[bitflags(default = B | C)]
441+
/// #[repr(u8)]
442+
/// #[derive(Copy, Clone, Debug, PartialEq)]
443+
/// enum MyFlag {
444+
/// A = 0b0001,
445+
/// B = 0b0010,
446+
/// C = 0b0100,
447+
/// D = 0b1000,
448+
/// }
449+
///
450+
/// assert_eq!(BitFlags::default(), MyFlag::B | MyFlag::C);
451+
/// ```
452+
///
421453
/// ## Memory layout
422454
///
423455
/// `BitFlags<T>` is marked with the `#[repr(transparent)]` trait, meaning

0 commit comments

Comments
 (0)