Debian Codegraph @

third_party/rust/enumset/src/macros.rs

raw
1 /// Everything in this module is internal API and may change at any time.
2 #[doc(hidden)]
3 pub mod __internal {
4 /// A reexport of core to allow our macros to be generic to std vs core.
5 pub use ::core as core_export;
6
7 /// A reexport of serde so our users don't have to also have a serde dependency.
8 #[cfg(feature = "serde")]
9 pub use serde2 as serde;
10
11 /// Reexports of internal types
12 pub use crate::{
13 repr::{ArrayRepr, EnumSetTypeRepr},
14 traits::EnumSetTypePrivate,
15 };
16 }
17
18 /// Creates a EnumSet literal, which can be used in const contexts.
19 ///
20 /// The syntax used is `enum_set!(Type::A | Type::B | Type::C)`. Each variant must be of the same
21 /// type, or a error will occur at compile-time.
22 ///
23 /// This macro accepts trailing `|`s to allow easier use in other macros.
24 ///
25 /// # Examples
26 ///
27 /// ```rust
28 /// # use enumset::*;
29 /// # #[derive(EnumSetType, Debug)] enum Enum { A, B, C }
30 /// const CONST_SET: EnumSet<Enum> = enum_set!(Enum::A | Enum::B);
31 /// assert_eq!(CONST_SET, Enum::A | Enum::B);
32 /// ```
33 ///
34 /// This macro is strongly typed. For example, the following will not compile:
35 ///
36 /// ```compile_fail
37 /// # use enumset::*;
38 /// # #[derive(EnumSetType, Debug)] enum Enum { A, B, C }
39 /// # #[derive(EnumSetType, Debug)] enum Enum2 { A, B, C }
40 /// let type_error = enum_set!(Enum::A | Enum2::B);
41 /// ```
42 #[macro_export]
43 macro_rules! enum_set {
44 ($(|)*) => {
45 EnumSet::EMPTY
46 };
47 ($value:path $(|)*) => {
48 {
49 #[allow(deprecated)] let value = $value.__impl_enumset_internal__const_only();
50 value
51 }
52 };
53 ($value:path | $($rest:path)|* $(|)*) => {
54 {
55 #[allow(deprecated)] let value = $value.__impl_enumset_internal__const_only();
56 $(#[allow(deprecated)] let value = $rest.__impl_enumset_internal__const_merge(value);)*
57 value
58 }
59 };
60 }