|
2 | 2 | //! `enumflags2` defines a `BitFlags<T>` type, which is a `Set<T>` |
3 | 3 | //! for enums without associated data. |
4 | 4 | //! |
| 5 | +//! This means that the type of a single flag is separate from a set of flags. |
| 6 | +//! |
5 | 7 | //! ## Example |
6 | 8 | //! ``` |
7 | 9 | //! use enumflags2::BitFlags; |
@@ -99,7 +101,63 @@ pub use enumflags2_derive::BitFlags_internal as BitFlags; |
99 | 101 |
|
100 | 102 | /// A trait automatically implemented by `derive(BitFlags)` to make the enum |
101 | 103 | /// a valid type parameter for `BitFlags<T>`. |
102 | | -pub trait RawBitFlags: Copy + Clone + 'static + _internal::RawBitFlags {} |
| 104 | +pub trait RawBitFlags: Copy + Clone + 'static + _internal::RawBitFlags { |
| 105 | + /// Create a `BitFlags` with no flags set (in other words, with a value of 0). |
| 106 | + /// |
| 107 | + /// This is a convenience reexport of [`BitFlags::empty`]. It can be called with |
| 108 | + /// `MyFlag::empty()`, thus bypassing the need for type hints in some situations. |
| 109 | + /// |
| 110 | + /// [`BitFlags::empty`]: struct.BitFlags.html#method.empty |
| 111 | + /// |
| 112 | + /// ``` |
| 113 | + /// # use enumflags2::BitFlags; |
| 114 | + /// #[derive(Clone, Copy, PartialEq, Eq, BitFlags)] |
| 115 | + /// enum MyFlag { |
| 116 | + /// One = 1 << 0, |
| 117 | + /// Two = 1 << 1, |
| 118 | + /// Three = 1 << 2, |
| 119 | + /// } |
| 120 | + /// |
| 121 | + /// use enumflags2::RawBitFlags; |
| 122 | + /// |
| 123 | + /// let empty = MyFlag::empty(); |
| 124 | + /// assert!(empty.is_empty()); |
| 125 | + /// assert_eq!(empty.contains(MyFlag::One), false); |
| 126 | + /// assert_eq!(empty.contains(MyFlag::Two), false); |
| 127 | + /// assert_eq!(empty.contains(MyFlag::Three), false); |
| 128 | + /// ``` |
| 129 | + fn empty() -> BitFlags<Self> { |
| 130 | + BitFlags::empty() |
| 131 | + } |
| 132 | + |
| 133 | + /// Create a `BitFlags` with all flags set. |
| 134 | + /// |
| 135 | + /// This is a convenience reexport of [`BitFlags::all`]. It can be called with |
| 136 | + /// `MyFlag::all()`, thus bypassing the need for type hints in some situations. |
| 137 | + /// |
| 138 | + /// [`BitFlags::all`]: struct.BitFlags.html#method.all |
| 139 | + /// |
| 140 | + /// ``` |
| 141 | + /// # use enumflags2::BitFlags; |
| 142 | + /// #[derive(Clone, Copy, PartialEq, Eq, BitFlags)] |
| 143 | + /// enum MyFlag { |
| 144 | + /// One = 1 << 0, |
| 145 | + /// Two = 1 << 1, |
| 146 | + /// Three = 1 << 2, |
| 147 | + /// } |
| 148 | + /// |
| 149 | + /// use enumflags2::RawBitFlags; |
| 150 | + /// |
| 151 | + /// let empty = MyFlag::all(); |
| 152 | + /// assert!(empty.is_all()); |
| 153 | + /// assert_eq!(empty.contains(MyFlag::One), true); |
| 154 | + /// assert_eq!(empty.contains(MyFlag::Two), true); |
| 155 | + /// assert_eq!(empty.contains(MyFlag::Three), true); |
| 156 | + /// ``` |
| 157 | + fn all() -> BitFlags<Self> { |
| 158 | + BitFlags::all() |
| 159 | + } |
| 160 | +} |
103 | 161 |
|
104 | 162 | /// While the module is public, this is only the case because it needs to be |
105 | 163 | /// accessed by the derive macro. Do not use this directly. Stability guarantees |
@@ -206,12 +264,52 @@ where |
206 | 264 | BitFlags { val } |
207 | 265 | } |
208 | 266 |
|
209 | | - /// Create an empty BitFlags. Empty means `0`. |
| 267 | + /// Create a `BitFlags` with no flags set (in other words, with a value of `0`). |
| 268 | + /// |
| 269 | + /// See also: [`RawBitFlags::empty`], a convenience reexport. |
| 270 | + /// |
| 271 | + /// [`RawBitFlags::empty`]: trait.RawBitFlags.html#method.empty |
| 272 | + /// |
| 273 | + /// ``` |
| 274 | + /// # use enumflags2::BitFlags; |
| 275 | + /// #[derive(Clone, Copy, PartialEq, Eq, BitFlags)] |
| 276 | + /// enum MyFlag { |
| 277 | + /// One = 1 << 0, |
| 278 | + /// Two = 1 << 1, |
| 279 | + /// Three = 1 << 2, |
| 280 | + /// } |
| 281 | + /// |
| 282 | + /// let empty: BitFlags<MyFlag> = BitFlags::empty(); |
| 283 | + /// assert!(empty.is_empty()); |
| 284 | + /// assert_eq!(empty.contains(MyFlag::One), false); |
| 285 | + /// assert_eq!(empty.contains(MyFlag::Two), false); |
| 286 | + /// assert_eq!(empty.contains(MyFlag::Three), false); |
| 287 | + /// ``` |
210 | 288 | pub fn empty() -> Self { |
211 | 289 | unsafe { BitFlags::new(T::Type::default()) } |
212 | 290 | } |
213 | 291 |
|
214 | | - /// Create a BitFlags with all flags set. |
| 292 | + /// Create a `BitFlags` with all flags set. |
| 293 | + /// |
| 294 | + /// See also: [`RawBitFlags::all`], a convenience reexport. |
| 295 | + /// |
| 296 | + /// [`RawBitFlags::all`]: trait.RawBitFlags.html#method.all |
| 297 | + /// |
| 298 | + /// ``` |
| 299 | + /// # use enumflags2::BitFlags; |
| 300 | + /// #[derive(Clone, Copy, PartialEq, Eq, BitFlags)] |
| 301 | + /// enum MyFlag { |
| 302 | + /// One = 1 << 0, |
| 303 | + /// Two = 1 << 1, |
| 304 | + /// Three = 1 << 2, |
| 305 | + /// } |
| 306 | + /// |
| 307 | + /// let empty: BitFlags<MyFlag> = BitFlags::all(); |
| 308 | + /// assert!(empty.is_all()); |
| 309 | + /// assert_eq!(empty.contains(MyFlag::One), true); |
| 310 | + /// assert_eq!(empty.contains(MyFlag::Two), true); |
| 311 | + /// assert_eq!(empty.contains(MyFlag::Three), true); |
| 312 | + /// ``` |
215 | 313 | pub fn all() -> Self { |
216 | 314 | unsafe { BitFlags::new(T::all_bits()) } |
217 | 315 | } |
|
0 commit comments