Debian Codegraph @

third_party/rust/enumset/src/lib.rs

raw
1 #![no_std]
2 #![deny(missing_docs)]
3 #![allow(clippy::missing_safety_doc)] // The safety requirement is "use the procedural derive".
4 #![allow(clippy::needless_range_loop)] // range loop style is clearer in most places in enumset
5 #![cfg_attr(docsrs, feature(doc_cfg))]
6
7 //! A library for defining enums that can be used in compact bit sets. It supports arbitrarily
8 //! large enums, and has very basic support for using them in constants.
9 //!
10 //! The following feature flags may be used for this crate:
11 //!
12 //! * `serde` enables serialization support for [`EnumSet`].
13 //! * `alloc` enables functions that require allocation.
14 //!
15 //! # Defining enums for use with EnumSet
16 //!
17 //! Enums to be used with [`EnumSet`] should be defined using `#[derive(EnumSetType)]`:
18 //!
19 //! ```rust
20 //! # use enumset::*;
21 //! #[derive(EnumSetType, Debug)]
22 //! pub enum Enum {
23 //! A, B, C, D, E, F, G,
24 //! }
25 //! ```
26 //!
27 //! For more information on more advanced use cases, see the documentation for
28 //! [`#[derive(EnumSetType)]`](./derive.EnumSetType.html).
29 //!
30 //! # Working with EnumSets
31 //!
32 //! EnumSets can be constructed via [`EnumSet::new()`] like a normal set. In addition,
33 //! `#[derive(EnumSetType)]` creates operator overloads that allow you to create EnumSets like so:
34 //!
35 //! ```rust
36 //! # use enumset::*;
37 //! # #[derive(EnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
38 //! let new_set = Enum::A | Enum::C | Enum::G;
39 //! assert_eq!(new_set.len(), 3);
40 //! ```
41 //!
42 //! All bitwise operations you would expect to work on bitsets also work on both EnumSets and
43 //! enums with `#[derive(EnumSetType)]`:
44 //! ```rust
45 //! # use enumset::*;
46 //! # #[derive(EnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
47 //! // Intersection of sets
48 //! assert_eq!((Enum::A | Enum::B) & Enum::C, EnumSet::empty());
49 //! assert_eq!((Enum::A | Enum::B) & Enum::A, Enum::A);
50 //! assert_eq!(Enum::A & Enum::B, EnumSet::empty());
51 //!
52 //! // Symmetric difference of sets
53 //! assert_eq!((Enum::A | Enum::B) ^ (Enum::B | Enum::C), Enum::A | Enum::C);
54 //! assert_eq!(Enum::A ^ Enum::C, Enum::A | Enum::C);
55 //!
56 //! // Difference of sets
57 //! assert_eq!((Enum::A | Enum::B | Enum::C) - Enum::B, Enum::A | Enum::C);
58 //!
59 //! // Complement of sets
60 //! assert_eq!(!(Enum::E | Enum::G), Enum::A | Enum::B | Enum::C | Enum::D | Enum::F);
61 //! ```
62 //!
63 //! The [`enum_set!`] macro allows you to create EnumSets in constant contexts:
64 //!
65 //! ```rust
66 //! # use enumset::*;
67 //! # #[derive(EnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
68 //! const CONST_SET: EnumSet<Enum> = enum_set!(Enum::A | Enum::B);
69 //! assert_eq!(CONST_SET, Enum::A | Enum::B);
70 //! ```
71 //!
72 //! Mutable operations on the [`EnumSet`] otherwise similarly to Rust's builtin sets:
73 //!
74 //! ```rust
75 //! # use enumset::*;
76 //! # #[derive(EnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
77 //! let mut set = EnumSet::new();
78 //! set.insert(Enum::A);
79 //! set.insert_all(Enum::E | Enum::G);
80 //! assert!(set.contains(Enum::A));
81 //! assert!(!set.contains(Enum::B));
82 //! assert_eq!(set, Enum::A | Enum::E | Enum::G);
83 //! ```
84
85 #[cfg(feature = "alloc")]
86 extern crate alloc;
87
88 mod macros;
89
90 mod repr;
91 mod set;
92 mod traits;
93
94 pub use crate::macros::__internal;
95 pub use crate::set::{EnumSet, EnumSetIter};
96 pub use crate::traits::{EnumSetType, EnumSetTypeWithRepr};
97
98 /// The procedural macro used to derive [`EnumSetType`], and allow enums to be used with
99 /// [`EnumSet`].
100 ///
101 /// # Limitations
102 ///
103 /// Currently, the following limitations apply to what kinds of enums this macro may be used with:
104 ///
105 /// * The enum must have no data fields in any variant.
106 /// * Variant discriminators must be zero or positive.
107 /// * No variant discriminator may be larger than `0xFFFFFFBF`. This is chosen to limit problems
108 /// involving overflow and similar edge cases.
109 /// * Variant discriminators must be defined with integer literals. Expressions like `V = 1 + 1`
110 /// are not currently supported.
111 ///
112 /// # Additional Impls
113 ///
114 /// In addition to the implementation of `EnumSetType`, this procedural macro creates multiple
115 /// other impls that are either required for the macro to work, or make the procedural macro more
116 /// ergonomic to use.
117 ///
118 /// A full list of traits implemented as is follows:
119 ///
120 /// * [`Copy`], [`Clone`], [`Eq`], [`PartialEq`] implementations are created to allow `EnumSet`
121 /// to function properly. These automatic implementations may be suppressed using
122 /// `#[enumset(no_super_impls)]`, but these traits must still be implemented in another way.
123 /// * [`PartialEq`], [`Sub`], [`BitAnd`], [`BitOr`], [`BitXor`], and [`Not`] implementations are
124 /// created to allow the crate to be used more ergonomically in expressions. These automatic
125 /// implementations may be suppressed using `#[enumset(no_ops)]`.
126 ///
127 /// # Options
128 ///
129 /// Options are given with `#[enumset(foo)]` annotations attached to the same enum as the derive.
130 /// Multiple options may be given in the same annotation using the `#[enumset(foo, bar)]` syntax.
131 ///
132 /// A full list of options is as follows:
133 ///
134 /// * `#[enumset(no_super_impls)]` prevents the derive from creating implementations required for
135 /// [`EnumSet`] to function. When this attribute is specified, implementations of [`Copy`],
136 /// [`Clone`], [`Eq`], and [`PartialEq`]. This can be useful if you are using a code generator
137 /// that already derives these traits. These impls should function identically to the
138 /// automatically derived versions, or unintentional behavior may be a result.
139 /// * `#[enumset(no_ops)` prevents the derive from implementing any operator traits.
140 /// * `#[enumset(crate_name = "enumset2")]` may be used to change the name of the `enumset` crate
141 /// used in the generated code. When the `std` feature is enabled, enumset parses `Cargo.toml`
142 /// to determine the name of the crate, and this flag is unnecessary.
143 /// * `#[enumset(repr = "u8")]` may be used to specify the in-memory representation of `EnumSet`s
144 /// of this enum type. The effects of this are described in [the `EnumSet` documentation under
145 /// “FFI, Safety and `repr`”][EnumSet#ffi-safety-and-repr]. Allowed types are `u8`, `u16`, `u32`,
146 /// `u64` and `u128`. If this is not used, then the derive macro will choose a type to best fit
147 /// the enum, but there are no guarantees about which type will be chosen.
148 /// * `#[enumset(repr = "array")]` forces the `EnumSet` of this type to be backed with an array,
149 /// even if all the variants could fit into a primitive numeric type.
150 ///
151 /// When the `serde` feature is used, the following features may also be specified. These options
152 /// may be used (with no effect) when building without the feature enabled:
153 ///
154 /// * `#[enumset(serialize_repr = "…")]` may be used to override the way the `EnumSet` is
155 /// serialized. Valid options are `u8`, `u16`, `u32`, `u64`, `list`, `map` and `array`. For more
156 /// information, see the ["Serialization" section of the `EnumSet` documentation]
157 /// (EnumSet#serialization).
158 /// * `#[enumset(serialize_deny_unknown)]` causes the generated deserializer to return an error
159 /// for unknown bits instead of silently ignoring them.
160 ///
161 /// # Examples
162 ///
163 /// Deriving a plain EnumSetType:
164 ///
165 /// ```rust
166 /// # use enumset::*;
167 /// #[derive(EnumSetType)]
168 /// pub enum Enum {
169 /// A, B, C, D, E, F, G,
170 /// }
171 /// ```
172 ///
173 /// Deriving a sparse EnumSetType:
174 ///
175 /// ```rust
176 /// # use enumset::*;
177 /// #[derive(EnumSetType)]
178 /// pub enum SparseEnum {
179 /// A = 10, B = 20, C = 30, D = 127,
180 /// }
181 /// ```
182 ///
183 /// Deriving an EnumSetType without adding ops:
184 ///
185 /// ```rust
186 /// # use enumset::*;
187 /// #[derive(EnumSetType)]
188 /// #[enumset(no_ops)]
189 /// pub enum NoOpsEnum {
190 /// A, B, C, D, E, F, G,
191 /// }
192 /// ```
193 ///
194 /// [`Sub`]: core::ops::Sub
195 /// [`BitAnd`]: core::ops::BitAnd
196 /// [`BitOr`]: core::ops::BitOr
197 /// [`BitXor`]: core::ops::BitXor
198 /// [`Not`]: core::ops::Not
199 pub use enumset_derive::EnumSetType;