@@ -181,6 +181,108 @@ pub trait BitFlag: Copy + Clone + 'static + _internal::RawBitFlags {
181
181
fn all ( ) -> BitFlags < Self > {
182
182
BitFlags :: all ( )
183
183
}
184
+
185
+ /// Create a `BitFlags` if the raw value provided does not contain
186
+ /// any illegal flags.
187
+ ///
188
+ /// This is a convenience reexport of [`BitFlags::from_bits`]. It can be called
189
+ /// with `MyFlag::from_bits(bits)`, thus bypassing the need for type hints in
190
+ /// some situations.
191
+ ///
192
+ /// ```
193
+ /// # use enumflags2::{bitflags, BitFlags};
194
+ /// #[bitflags]
195
+ /// #[repr(u8)]
196
+ /// #[derive(Clone, Copy, PartialEq, Eq, Debug)]
197
+ /// enum MyFlag {
198
+ /// One = 1 << 0,
199
+ /// Two = 1 << 1,
200
+ /// Three = 1 << 2,
201
+ /// }
202
+ ///
203
+ /// use enumflags2::BitFlag;
204
+ ///
205
+ /// let from_bits = MyFlag::from_bits(0b11).unwrap();
206
+ /// assert_eq!(from_bits.contains(MyFlag::One), true);
207
+ /// assert_eq!(from_bits.contains(MyFlag::Two), true);
208
+ /// assert_eq!(from_bits.contains(MyFlag::Three), false);
209
+ /// let invalid = MyFlag::from_bits(1 << 3);
210
+ /// assert!(invalid.is_err());
211
+ /// ```
212
+ #[ inline]
213
+ fn from_bits ( bits : Self :: Numeric ) -> Result < BitFlags < Self > , FromBitsError < Self > > {
214
+ BitFlags :: from_bits ( bits)
215
+ }
216
+
217
+ /// Create a `BitFlags` from an underlying bitwise value. If any
218
+ /// invalid bits are set, ignore them.
219
+ ///
220
+ /// This is a convenience reexport of [`BitFlags::from_bits_truncate`]. It can be
221
+ /// called with `MyFlag::from_bits_truncate(bits)`, thus bypassing the need for
222
+ /// type hints in some situations.
223
+ ///
224
+ /// ```
225
+ /// # use enumflags2::{bitflags, BitFlags};
226
+ /// #[bitflags]
227
+ /// #[repr(u8)]
228
+ /// #[derive(Clone, Copy, PartialEq, Eq)]
229
+ /// enum MyFlag {
230
+ /// One = 1 << 0,
231
+ /// Two = 1 << 1,
232
+ /// Three = 1 << 2,
233
+ /// }
234
+ ///
235
+ /// use enumflags2::BitFlag;
236
+ ///
237
+ /// let from_bits = MyFlag::from_bits_truncate(0b1_1011);
238
+ /// assert_eq!(from_bits.contains(MyFlag::One), true);
239
+ /// assert_eq!(from_bits.contains(MyFlag::Two), true);
240
+ /// assert_eq!(from_bits.contains(MyFlag::Three), false);
241
+ /// ```
242
+ #[ inline]
243
+ fn from_bits_truncate ( bits : Self :: Numeric ) -> BitFlags < Self > {
244
+ BitFlags :: from_bits_truncate ( bits)
245
+ }
246
+
247
+ /// Create a `BitFlags` unsafely, without checking if the bits form
248
+ /// a valid bit pattern for the type.
249
+ ///
250
+ /// Consider using [`from_bits`][BitFlag::from_bits]
251
+ /// or [`from_bits_truncate`][BitFlag::from_bits_truncate] instead.
252
+ ///
253
+ ///
254
+ /// This is a convenience reexport of [`BitFlags::from_bits_unchecked`]. It can be
255
+ /// called with `MyFlag::from_bits_unchecked(bits)`, thus bypassing the need for
256
+ /// type hints in some situations.
257
+ ///
258
+ /// ```
259
+ /// # use enumflags2::{bitflags, BitFlags};
260
+ /// #[bitflags]
261
+ /// #[repr(u8)]
262
+ /// #[derive(Clone, Copy, PartialEq, Eq)]
263
+ /// enum MyFlag {
264
+ /// One = 1 << 0,
265
+ /// Two = 1 << 1,
266
+ /// Three = 1 << 2,
267
+ /// }
268
+ ///
269
+ /// use enumflags2::BitFlag;
270
+ ///
271
+ /// unsafe {
272
+ /// let from_bits = MyFlag::from_bits_unchecked(0b011);
273
+ /// assert_eq!(from_bits.contains(MyFlag::One), true);
274
+ /// assert_eq!(from_bits.contains(MyFlag::Two), true);
275
+ /// assert_eq!(from_bits.contains(MyFlag::Three), false);
276
+ /// }
277
+ /// ```
278
+ ///
279
+ /// # Safety
280
+ ///
281
+ /// All bits set in `val` must correspond to a value of the enum.
282
+ #[ inline]
283
+ unsafe fn from_bits_unchecked ( bits : Self :: Numeric ) -> BitFlags < Self > {
284
+ BitFlags :: from_bits_unchecked ( bits)
285
+ }
184
286
}
185
287
186
288
/// While the module is public, this is only the case because it needs to be
0 commit comments