]> git.lizzy.rs Git - enumset.git/commitdiff
Initial commit.
authorLymia Aluysia <lymia@lymiahugs.com>
Sun, 29 Oct 2017 10:07:42 +0000 (05:07 -0500)
committerLymia Aluysia <lymia@lymiahugs.com>
Sun, 29 Oct 2017 10:07:42 +0000 (05:07 -0500)
.gitignore [new file with mode: 0644]
Cargo.toml [new file with mode: 0644]
LICENSE-APACHE [new file with mode: 0644]
LICENSE-MIT [new file with mode: 0644]
README.md [new file with mode: 0644]
src/lib.rs [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..b720ece
--- /dev/null
@@ -0,0 +1,4 @@
+/target
+/.idea
+/Cargo.lock
+/*.iml
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644 (file)
index 0000000..67fb06f
--- /dev/null
@@ -0,0 +1,17 @@
+[package]
+name = "enumset"
+version = "0.1.0"
+authors = ["Lymia Aluysia <lymia@lymiahugs.com>"]
+
+description = "A library for creating compact sets of enums."
+keywords = ["enum", "bitset"]
+categories = ["data-structures"]
+
+documentation = "https://docs.rs/enumset"
+repository = "https://github.com/Lymia/enumset"
+readme = "README.md"
+
+license = "MIT/Apache-2.0"
+
+[features]
+i128 = []
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644 (file)
index 0000000..16fe87b
--- /dev/null
@@ -0,0 +1,201 @@
+                              Apache License
+                        Version 2.0, January 2004
+                     http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+   "License" shall mean the terms and conditions for use, reproduction,
+   and distribution as defined by Sections 1 through 9 of this document.
+
+   "Licensor" shall mean the copyright owner or entity authorized by
+   the copyright owner that is granting the License.
+
+   "Legal Entity" shall mean the union of the acting entity and all
+   other entities that control, are controlled by, or are under common
+   control with that entity. For the purposes of this definition,
+   "control" means (i) the power, direct or indirect, to cause the
+   direction or management of such entity, whether by contract or
+   otherwise, or (ii) ownership of fifty percent (50%) or more of the
+   outstanding shares, or (iii) beneficial ownership of such entity.
+
+   "You" (or "Your") shall mean an individual or Legal Entity
+   exercising permissions granted by this License.
+
+   "Source" form shall mean the preferred form for making modifications,
+   including but not limited to software source code, documentation
+   source, and configuration files.
+
+   "Object" form shall mean any form resulting from mechanical
+   transformation or translation of a Source form, including but
+   not limited to compiled object code, generated documentation,
+   and conversions to other media types.
+
+   "Work" shall mean the work of authorship, whether in Source or
+   Object form, made available under the License, as indicated by a
+   copyright notice that is included in or attached to the work
+   (an example is provided in the Appendix below).
+
+   "Derivative Works" shall mean any work, whether in Source or Object
+   form, that is based on (or derived from) the Work and for which the
+   editorial revisions, annotations, elaborations, or other modifications
+   represent, as a whole, an original work of authorship. For the purposes
+   of this License, Derivative Works shall not include works that remain
+   separable from, or merely link (or bind by name) to the interfaces of,
+   the Work and Derivative Works thereof.
+
+   "Contribution" shall mean any work of authorship, including
+   the original version of the Work and any modifications or additions
+   to that Work or Derivative Works thereof, that is intentionally
+   submitted to Licensor for inclusion in the Work by the copyright owner
+   or by an individual or Legal Entity authorized to submit on behalf of
+   the copyright owner. For the purposes of this definition, "submitted"
+   means any form of electronic, verbal, or written communication sent
+   to the Licensor or its representatives, including but not limited to
+   communication on electronic mailing lists, source code control systems,
+   and issue tracking systems that are managed by, or on behalf of, the
+   Licensor for the purpose of discussing and improving the Work, but
+   excluding communication that is conspicuously marked or otherwise
+   designated in writing by the copyright owner as "Not a Contribution."
+
+   "Contributor" shall mean Licensor and any individual or Legal Entity
+   on behalf of whom a Contribution has been received by Licensor and
+   subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   copyright license to reproduce, prepare Derivative Works of,
+   publicly display, publicly perform, sublicense, and distribute the
+   Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   (except as stated in this section) patent license to make, have made,
+   use, offer to sell, sell, import, and otherwise transfer the Work,
+   where such license applies only to those patent claims licensable
+   by such Contributor that are necessarily infringed by their
+   Contribution(s) alone or by combination of their Contribution(s)
+   with the Work to which such Contribution(s) was submitted. If You
+   institute patent litigation against any entity (including a
+   cross-claim or counterclaim in a lawsuit) alleging that the Work
+   or a Contribution incorporated within the Work constitutes direct
+   or contributory patent infringement, then any patent licenses
+   granted to You under this License for that Work shall terminate
+   as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+   Work or Derivative Works thereof in any medium, with or without
+   modifications, and in Source or Object form, provided that You
+   meet the following conditions:
+
+   (a) You must give any other recipients of the Work or
+       Derivative Works a copy of this License; and
+
+   (b) You must cause any modified files to carry prominent notices
+       stating that You changed the files; and
+
+   (c) You must retain, in the Source form of any Derivative Works
+       that You distribute, all copyright, patent, trademark, and
+       attribution notices from the Source form of the Work,
+       excluding those notices that do not pertain to any part of
+       the Derivative Works; and
+
+   (d) If the Work includes a "NOTICE" text file as part of its
+       distribution, then any Derivative Works that You distribute must
+       include a readable copy of the attribution notices contained
+       within such NOTICE file, excluding those notices that do not
+       pertain to any part of the Derivative Works, in at least one
+       of the following places: within a NOTICE text file distributed
+       as part of the Derivative Works; within the Source form or
+       documentation, if provided along with the Derivative Works; or,
+       within a display generated by the Derivative Works, if and
+       wherever such third-party notices normally appear. The contents
+       of the NOTICE file are for informational purposes only and
+       do not modify the License. You may add Your own attribution
+       notices within Derivative Works that You distribute, alongside
+       or as an addendum to the NOTICE text from the Work, provided
+       that such additional attribution notices cannot be construed
+       as modifying the License.
+
+   You may add Your own copyright statement to Your modifications and
+   may provide additional or different license terms and conditions
+   for use, reproduction, or distribution of Your modifications, or
+   for any such Derivative Works as a whole, provided Your use,
+   reproduction, and distribution of the Work otherwise complies with
+   the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+   any Contribution intentionally submitted for inclusion in the Work
+   by You to the Licensor shall be under the terms and conditions of
+   this License, without any additional terms or conditions.
+   Notwithstanding the above, nothing herein shall supersede or modify
+   the terms of any separate license agreement you may have executed
+   with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+   names, trademarks, service marks, or product names of the Licensor,
+   except as required for reasonable and customary use in describing the
+   origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+   agreed to in writing, Licensor provides the Work (and each
+   Contributor provides its Contributions) on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+   implied, including, without limitation, any warranties or conditions
+   of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+   PARTICULAR PURPOSE. You are solely responsible for determining the
+   appropriateness of using or redistributing the Work and assume any
+   risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+   whether in tort (including negligence), contract, or otherwise,
+   unless required by applicable law (such as deliberate and grossly
+   negligent acts) or agreed to in writing, shall any Contributor be
+   liable to You for damages, including any direct, indirect, special,
+   incidental, or consequential damages of any character arising as a
+   result of this License or out of the use or inability to use the
+   Work (including but not limited to damages for loss of goodwill,
+   work stoppage, computer failure or malfunction, or any and all
+   other commercial damages or losses), even if such Contributor
+   has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+   the Work or Derivative Works thereof, You may choose to offer,
+   and charge a fee for, acceptance of support, warranty, indemnity,
+   or other liability obligations and/or rights consistent with this
+   License. However, in accepting such obligations, You may act only
+   on Your own behalf and on Your sole responsibility, not on behalf
+   of any other Contributor, and only if You agree to indemnify,
+   defend, and hold each Contributor harmless for any liability
+   incurred by, or claims asserted against, such Contributor by reason
+   of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+   To apply the Apache License to your work, attach the following
+   boilerplate notice, with the fields enclosed by brackets "[]"
+   replaced with your own identifying information. (Don't include
+   the brackets!)  The text should be enclosed in the appropriate
+   comment syntax for the file format. We also recommend that a
+   file or class name and description of purpose be included on the
+   same "printed page" as the copyright notice for easier
+   identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644 (file)
index 0000000..64a643b
--- /dev/null
@@ -0,0 +1,26 @@
+Copyright (c) 2017 Lymia Aluysia <lymiahugs@gmail.com>
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..5cbc59c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,7 @@
+A library for defining enums that can be used in compact bit sets.
+
+It supports enums up to 64 variants on stable Rust, and up to 128 variants on nightly Rust with the `i128` feature enabled, and has a macro to use these sets in constants.
+
+See the documentation for more information.
+
+This library is dual licenced under the MIT and Apache 2.0 licenses.
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644 (file)
index 0000000..36a08f1
--- /dev/null
@@ -0,0 +1,625 @@
+#![cfg_attr(all(test, feature = "i128"), feature(i128, i128_type))]
+
+//! A library for defining enums that can be used in compact bit sets.
+//!
+//! It supports enums up to 64 variants on stable Rust, and up to 128 variants on nightly Rust with
+//! the `i128` feature enabled.
+//!
+//! # Defining enums for use with EnumSet
+//!
+//! Enums to be used with `EnumSet` should be defined through the `enum_set_type!` macro:
+//!
+//! ```rust
+//! # #[macro_use] extern crate enumset;
+//! # use enumset::*;
+//! enum_set_type! {
+//!     /// Documentation for the enum
+//!     pub enum Enum {
+//!         A, B, C, D, E, F, G,
+//!         #[doc(hidden)] __NonExhaustive,
+//!     }
+//! }
+//! # fn main() { }
+//! ```
+//!
+//! # Working with EnumSets
+//!
+//! EnumSets can be constructed via `EnumSet::new()` like a normal set. In addition,
+//! `enum_set_type!` creates operator overloads that allow you to create EnumSets like so:
+//!
+//! ```rust
+//! # #[macro_use] extern crate enumset;
+//! # use enumset::*;
+//! # enum_set_type! {
+//! #      pub enum Enum {
+//! #        A, B, C, D, E, F, G,
+//! #    }
+//! # }
+//! # fn main() {
+//! let new_set = Enum::A | Enum::C | Enum::G;
+//! assert_eq!(new_set.len(), 3);
+//! # }
+//! ```
+//!
+//! The `enum_set!` macro also allows you to create constant EnumSets:
+//!
+//! ```rust
+//! # #[macro_use] extern crate enumset;
+//! # use enumset::*;
+//! # enum_set_type! {
+//! #     enum Enum { A, B, C }
+//! # }
+//! # fn main() {
+//! const CONST_SET: EnumSet<Enum> = enum_set!(Enum, Enum::A | Enum::B);
+//! assert_eq!(CONST_SET, Enum::A | Enum::B);
+//! # }
+//! ```
+//!
+//! Mutable operations on the HashSet otherwise work basically as expected:
+//!
+//! ```rust
+//! # #[macro_use] extern crate enumset;
+//! # use enumset::*;
+//! # enum_set_type! {
+//! #      pub enum Enum {
+//! #        A, B, C, D, E, F, G,
+//! #    }
+//! # }
+//! # fn main() {
+//! let mut set = EnumSet::new();
+//! set.insert(Enum::A);
+//! set.insert_all(Enum::E | Enum::G);
+//! assert!(set.contains(Enum::A));
+//! assert!(!set.contains(Enum::B));
+//! assert_eq!(set, Enum::A | Enum::E | Enum::G);
+//! # }
+//! ```
+
+use std::fmt;
+use std::fmt::{Debug, Formatter};
+use std::hash::Hash;
+use std::ops::*;
+
+#[doc(hidden)]
+pub trait EnumSetType : Copy {
+    type Repr: Shl<u8, Output = Self::Repr> + Eq + Not<Output = Self::Repr> +
+               Sub<Output = Self::Repr> + BitOr<Output = Self::Repr> +
+               BitAnd<Output = Self::Repr> + BitXor<Output = Self::Repr> +
+               BitOrAssign + BitAndAssign + Copy + Debug + Ord + Eq + Hash;
+    const ZERO: Self::Repr;
+    const ONE : Self::Repr;
+    const VARIANT_COUNT: u8;
+
+    fn count_ones(val: Self::Repr) -> usize;
+    fn into_u8(self) -> u8;
+    fn from_u8(val: u8) -> Self;
+}
+
+/// An efficient set type for enums created with the `enum_set_type!` macro.
+#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
+pub struct EnumSet<T : EnumSetType>(#[doc(hidden)] pub T::Repr);
+impl <T : EnumSetType> EnumSet<T> {
+    fn mask(bit: u8) -> T::Repr {
+        T::ONE << bit
+    }
+    fn has_bit(&self, bit: u8) -> bool {
+        let mask = Self::mask(bit);
+        self.0 & mask == mask
+    }
+
+    /// Returns an empty set.
+    pub fn new() -> Self {
+        EnumSet(T::ZERO)
+    }
+
+    /// Returns the number of values in this set.
+    pub fn len(&self) -> usize {
+        T::count_ones(self.0)
+    }
+    /// Checks if the set is empty.
+    pub fn is_empty(&self) -> bool {
+        self.0 == T::ZERO
+    }
+    /// Removes all elements from the set.
+    pub fn clear(&mut self) {
+        self.0 = T::ZERO
+    }
+
+    /// Checks if this set shares no elements with another.
+    pub fn is_disjoint(&self, other: Self) -> bool {
+        self.0 & other.0 == T::ZERO
+    }
+    /// Checks if all elements in another set are in this set.
+    pub fn is_superset(&self, other: Self) -> bool {
+        other.0 & self.0 == other.0
+    }
+    /// Checks if all elements of this set are in another set.
+    pub fn is_subset(&self, other: Self) -> bool {
+        other.is_superset(*self)
+    }
+
+    /// Returns a set containing the union of all elements in both sets.
+    pub fn union(&self, other: Self) -> Self {
+        EnumSet(self.0 | other.0)
+    }
+    /// Returns a set containing all elements in common with another set.
+    pub fn intersection(&self, other: Self) -> Self {
+        EnumSet(self.0 & other.0)
+    }
+    /// Returns a set with all elements of the other set removed.
+    pub fn difference(&self, other: Self) -> Self {
+        EnumSet(self.0 & !other.0)
+    }
+    /// Returns a set with all elements not contained in both sets.
+    pub fn symmetrical_difference(&self, other: Self) -> Self {
+        EnumSet(self.0 ^ other.0)
+    }
+
+    /// Checks whether this set contains a value.
+    pub fn contains(&self, value: T) -> bool {
+        self.has_bit(value.into_u8())
+    }
+    /// Adds a value to this set.
+    pub fn insert(&mut self, value: T) -> bool {
+        let contains = self.contains(value);
+        self.0 |= Self::mask(value.into_u8());
+        contains
+    }
+    /// Removes a value from this set.
+    pub fn remove(&mut self, value: T) -> bool {
+        let contains = self.contains(value);
+        self.0 &= !Self::mask(value.into_u8());
+        contains
+    }
+
+    /// Adds all elements in another set to this one.
+    pub fn insert_all(&mut self, other: Self) {
+        self.0 |= other.0
+    }
+    /// Removes all values in another set from this one.
+    pub fn remove_all(&mut self, other: Self) {
+        self.0 &= !other.0
+    }
+
+    pub fn iter(&self) -> EnumSetIter<T> {
+        EnumSetIter(*self, 0)
+    }
+}
+impl <T : EnumSetType> IntoIterator for EnumSet<T> {
+    type Item = T;
+    type IntoIter = EnumSetIter<T>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        self.iter()
+    }
+}
+impl <T : EnumSetType> Sub<EnumSet<T>> for EnumSet<T> {
+    type Output = Self;
+    fn sub(self, other: Self) -> Self::Output {
+        self.difference(other)
+    }
+}
+impl <T : EnumSetType> BitAnd<EnumSet<T>> for EnumSet<T> {
+    type Output = Self;
+    fn bitand(self, other: Self) -> Self::Output {
+        self.intersection(other)
+    }
+}
+impl <T : EnumSetType> BitOr<EnumSet<T>> for EnumSet<T> {
+    type Output = Self;
+    fn bitor(self, other: Self) -> Self::Output {
+        self.union(other)
+    }
+}
+impl <T : EnumSetType> BitXor<EnumSet<T>> for EnumSet<T> {
+    type Output = Self;
+    fn bitxor(self, other: Self) -> Self::Output {
+        self.symmetrical_difference(other)
+    }
+}
+impl <T : EnumSetType> BitOr<T> for EnumSet<T> {
+    type Output = Self;
+    fn bitor(self, other: T) -> Self::Output {
+        EnumSet(self.0 | Self::mask(other.into_u8()))
+    }
+}
+
+impl <T : EnumSetType + Debug> Debug for EnumSet<T> {
+    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+        let mut is_first = true;
+        f.write_str("EnumSet(")?;
+        for v in self.iter() {
+            if !is_first { f.write_str(" | ")?; }
+            is_first = false;
+            v.fmt(f)?;
+        }
+        f.write_str(")")?;
+        Ok(())
+    }
+}
+
+/// An iterator for enums created with the `enum_set_type!` macro.
+#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
+pub struct EnumSetIter<T : EnumSetType>(EnumSet<T>, u8);
+impl <T : EnumSetType> Iterator for EnumSetIter<T> {
+    type Item = T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.1 < T::VARIANT_COUNT {
+            let bit = self.1;
+            self.1 += 1;
+            if self.0.has_bit(bit) {
+                return Some(T::from_u8(bit))
+            }
+        }
+        None
+    }
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        let left = T::count_ones((self.0).0 & !((T::ONE << self.1) - T::ONE));
+        (left, Some(left))
+    }
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(feature = "i128")]
+macro_rules! enum_set_type_internal_count_variants {
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $_20:ident $_21:ident $_22:ident $_23:ident $_24:ident $_25:ident $_26:ident $_27:ident
+        $_30:ident $_31:ident $_32:ident $_33:ident $_34:ident $_35:ident $_36:ident $_37:ident
+        $_40:ident $_41:ident $_42:ident $_43:ident $_44:ident $_45:ident $_46:ident $_47:ident
+        $_50:ident $_51:ident $_52:ident $_53:ident $_54:ident $_55:ident $_56:ident $_57:ident
+        $_60:ident $_61:ident $_62:ident $_63:ident $_64:ident $_65:ident $_66:ident $_67:ident
+        $_70:ident $_71:ident $_72:ident $_73:ident $_74:ident $_75:ident $_76:ident $_77:ident
+        $_80:ident $_81:ident $_82:ident $_83:ident $_84:ident $_85:ident $_86:ident $_87:ident
+        $_90:ident $_91:ident $_92:ident $_93:ident $_94:ident $_95:ident $_96:ident $_97:ident
+        $_a0:ident $_a1:ident $_a2:ident $_a3:ident $_a4:ident $_a5:ident $_a6:ident $_a7:ident
+        $_b0:ident $_b1:ident $_b2:ident $_b3:ident $_b4:ident $_b5:ident $_b6:ident $_b7:ident
+        $_c0:ident $_c1:ident $_c2:ident $_c3:ident $_c4:ident $_c5:ident $_c6:ident $_c7:ident
+        $_d0:ident $_d1:ident $_d2:ident $_d3:ident $_d4:ident $_d5:ident $_d6:ident $_d7:ident
+        $_e0:ident $_e1:ident $_e2:ident $_e3:ident $_e4:ident $_e5:ident $_e6:ident $_e7:ident
+        $_f0:ident $_f1:ident $_f2:ident $_f3:ident $_f4:ident $_f5:ident $_f6:ident $_f7:ident
+        $($rest:ident)+
+    ) => {
+        ENUM_SET___TOO_MANY_ENUM_VARIANTS___MAX_IS_128
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $_20:ident $_21:ident $_22:ident $_23:ident $_24:ident $_25:ident $_26:ident $_27:ident
+        $_30:ident $_31:ident $_32:ident $_33:ident $_34:ident $_35:ident $_36:ident $_37:ident
+        $_40:ident $_41:ident $_42:ident $_43:ident $_44:ident $_45:ident $_46:ident $_47:ident
+        $_50:ident $_51:ident $_52:ident $_53:ident $_54:ident $_55:ident $_56:ident $_57:ident
+        $_60:ident $_61:ident $_62:ident $_63:ident $_64:ident $_65:ident $_66:ident $_67:ident
+        $_70:ident $_71:ident $_72:ident $_73:ident $_74:ident $_75:ident $_76:ident $_77:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u128 $($args)* }
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $_20:ident $_21:ident $_22:ident $_23:ident $_24:ident $_25:ident $_26:ident $_27:ident
+        $_30:ident $_31:ident $_32:ident $_33:ident $_34:ident $_35:ident $_36:ident $_37:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u64 $($args)* }
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u32 $($args)* }
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u16 $($args)* }
+    };
+    ($next:ident ($($args:tt)*) $($rest:ident)*) => {
+        enum_set_type_internal! { @$next u8 $($args)* }
+    };
+}
+
+#[macro_export]
+#[doc(hidden)]
+#[cfg(not(feature = "i128"))]
+macro_rules! enum_set_type_internal_count_variants {
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $_20:ident $_21:ident $_22:ident $_23:ident $_24:ident $_25:ident $_26:ident $_27:ident
+        $_30:ident $_31:ident $_32:ident $_33:ident $_34:ident $_35:ident $_36:ident $_37:ident
+        $_40:ident $_41:ident $_42:ident $_43:ident $_44:ident $_45:ident $_46:ident $_47:ident
+        $_50:ident $_51:ident $_52:ident $_53:ident $_54:ident $_55:ident $_56:ident $_57:ident
+        $_60:ident $_61:ident $_62:ident $_63:ident $_64:ident $_65:ident $_66:ident $_67:ident
+        $_70:ident $_71:ident $_72:ident $_73:ident $_74:ident $_75:ident $_76:ident $_77:ident
+        $($rest:ident)+
+    ) => {
+        ENUM_SET___TOO_MANY_ENUM_VARIANTS___MAX_IS_64
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $_20:ident $_21:ident $_22:ident $_23:ident $_24:ident $_25:ident $_26:ident $_27:ident
+        $_30:ident $_31:ident $_32:ident $_33:ident $_34:ident $_35:ident $_36:ident $_37:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u64 $($args)* }
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $_10:ident $_11:ident $_12:ident $_13:ident $_14:ident $_15:ident $_16:ident $_17:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u32 $($args)* }
+    };
+    ($next:ident ($($args:tt)*)
+        $_00:ident $_01:ident $_02:ident $_03:ident $_04:ident $_05:ident $_06:ident $_07:ident
+        $($rest:ident)+
+    ) => {
+        enum_set_type_internal! { @$next u16 $($args)* }
+    };
+    ($next:ident ($($args:tt)*) $($rest:ident)*) => {
+        enum_set_type_internal! { @$next u8 $($args)* }
+    };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! enum_set_type_internal {
+    // Counting functions
+    (@ident ($($random:tt)*) $value:expr) => { $value };
+    (@count $($value:tt)*) => {
+        0u8 $(+ enum_set_type_internal!(@ident ($value) 1u8))*
+    };
+
+    // Codegen
+    (@body $repr:ident ($(#[$enum_attr:meta])*) ($($vis:tt)*) $enum_name:ident {
+        $($(#[$attr:meta])* $variant:ident,)*
+    }) => {
+        $(#[$enum_attr])* #[repr(u8)]
+        #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
+        $($vis)* enum $enum_name {
+            $($(#[$attr])* $variant,)*
+        }
+        impl $crate::EnumSetType for $enum_name {
+            type Repr = $repr;
+            const ZERO: Self::Repr = 0;
+            const ONE : Self::Repr = 1;
+            const VARIANT_COUNT: u8 = enum_set_type_internal!(@count $($variant)*);
+
+            fn count_ones(val: Self::Repr) -> usize {
+                val.count_ones() as usize
+            }
+            fn into_u8(self) -> u8 {
+                self as u8
+            }
+            fn from_u8(val: u8) -> Self {
+                unsafe { ::std::mem::transmute(val) }
+            }
+        }
+        impl ::std::ops::BitOr<$enum_name> for $enum_name {
+            type Output = $crate::EnumSet<$enum_name>;
+            fn bitor(self, other: $enum_name) -> Self::Output {
+                enum_set!($enum_name, self | other)
+            }
+        }
+    };
+}
+
+/// Defines enums which can be used with EnumSet. While attributes and documentation can be
+/// attached to the enums, the variants may not contain data.
+///
+/// # Examples
+///
+/// ```rust
+/// # #[macro_use] extern crate enumset;
+/// # use enumset::*;
+/// enum_set_type! {
+///     enum Enum {
+///         A, B, C, D, E, F, G
+///     }
+///
+///     /// Documentation
+///     pub enum Enum2 {
+///         A, B, C, D, E, F, G,
+///         #[doc(hidden)] __NonExhaustive,
+///     }
+/// }
+/// # fn main() { }
+/// ```
+#[macro_export]
+macro_rules! enum_set_type {
+    ($(#[$enum_attr:meta])* pub enum $enum_name:ident {
+        $($(#[$attr:meta])* $variant:ident),* $(,)*
+    } $($rest:tt)*) => {
+        enum_set_type_internal_count_variants!(body (($(#[$enum_attr])*) (pub) $enum_name {
+            $($(#[$attr])* $variant,)*
+        }) $($variant)*);
+        enum_set_type!($($rest)*);
+    };
+    ($(#[$enum_attr:meta])* enum $enum_name:ident {
+        $($(#[$attr:meta])* $variant:ident),* $(,)*
+    } $($rest:tt)*) => {
+        enum_set_type_internal_count_variants!(body (($(#[$enum_attr])*) () $enum_name {
+            $($(#[$attr])* $variant,)*
+        }) $($variant)*);
+        enum_set_type!($($rest)*);
+    };
+    () => { };
+}
+
+/// Creates a EnumSet literal, which can be used in const contexts.
+///
+/// The format is `enum_set_type!(Type, Type::A | Type::B | Type::C)`, where `Type` is the type
+/// the enum will contain, and the rest is the variants that the set will contain.
+///
+/// # Examples
+///
+/// ```rust
+/// # #[macro_use] extern crate enumset;
+/// # use enumset::*;
+/// # enum_set_type! {
+/// #     enum Enum { A, B, C }
+/// # }
+/// # fn main() {
+/// const CONST_SET: EnumSet<Enum> = enum_set!(Enum, Enum::A | Enum::B);
+/// assert_eq!(CONST_SET, Enum::A | Enum::B);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! enum_set {
+    ($enum_name:ty, $($value:path)|+) => {
+        $crate::EnumSet::<$enum_name>(
+            <$enum_name as $crate::EnumSetType>::ZERO
+            $(| (<$enum_name as $crate::EnumSetType>::ONE << ($value as u8)))*
+        )
+    }
+}
+
+#[cfg(test)]
+#[allow(dead_code)]
+mod test {
+    use super::*;
+
+    enum_set_type! {
+        enum Enum {
+            A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+        }
+        enum Enum2 {
+            A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+        }
+    }
+
+    #[cfg(feature = "i128")]
+    enum_set_type! {
+        enum LargeEnum {
+            _00,  _01,  _02,  _03,  _04,  _05,  _06,  _07,
+            _10,  _11,  _12,  _13,  _14,  _15,  _16,  _17,
+            _20,  _21,  _22,  _23,  _24,  _25,  _26,  _27,
+            _30,  _31,  _32,  _33,  _34,  _35,  _36,  _37,
+            _40,  _41,  _42,  _43,  _44,  _45,  _46,  _47,
+            _50,  _51,  _52,  _53,  _54,  _55,  _56,  _57,
+            _60,  _61,  _62,  _63,  _64,  _65,  _66,  _67,
+            _70,  _71,  _72,  _73,  _74,  _75,  _76,  _77,
+            A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+        }
+    }
+
+    macro_rules! test_variants {
+        ($enum_name:ident $test_name:ident $($variant:ident,)*) => {
+            #[test]
+            fn $test_name() {
+                let count = enum_set_type_internal!(@count u8 $($variant)*);
+                $(
+                    assert!(($enum_name::$variant as u8) < count);
+                )*
+            }
+        }
+    }
+
+    test_variants! { Enum enum_variant_range_test
+        A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+    }
+
+    #[cfg(feature = "i128")]
+    test_variants! { LargeEnum large_enum_variant_range_test
+        _00,  _01,  _02,  _03,  _04,  _05,  _06,  _07,
+        _10,  _11,  _12,  _13,  _14,  _15,  _16,  _17,
+        _20,  _21,  _22,  _23,  _24,  _25,  _26,  _27,
+        _30,  _31,  _32,  _33,  _34,  _35,  _36,  _37,
+        _40,  _41,  _42,  _43,  _44,  _45,  _46,  _47,
+        _50,  _51,  _52,  _53,  _54,  _55,  _56,  _57,
+        _60,  _61,  _62,  _63,  _64,  _65,  _66,  _67,
+        _70,  _71,  _72,  _73,  _74,  _75,  _76,  _77,
+        A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+    }
+
+    macro_rules! test_enum {
+        ($e:ident, $m:ident) => {
+            mod $m {
+                use super::*;
+
+                const CONST_SET: EnumSet<$e> = enum_set!($e, $e::A | $e::Y);
+                #[test]
+                fn const_set() {
+                    assert_eq!(CONST_SET.len(), 2);
+                    assert!(CONST_SET.contains($e::A));
+                    assert!(CONST_SET.contains($e::Y));
+                }
+
+                #[test]
+                fn basic_add_remove() {
+                    let mut set = EnumSet::new();
+                    set.insert($e::A);
+                    set.insert($e::B);
+                    set.insert($e::C);
+                    assert_eq!(set, $e::A | $e::B | $e::C);
+                    set.remove($e::B);
+                    assert_eq!(set, $e::A | $e::C);
+                    set.insert($e::D);
+                    assert_eq!(set, $e::A | $e::C | $e::D);
+                    set.insert_all($e::F | $e::E | $e::G);
+                    assert_eq!(set, $e::A | $e::C | $e::D | $e::F | $e::E | $e::G);
+                    set.remove_all($e::A | $e::D | $e::G);
+                    assert_eq!(set, $e::C | $e::F | $e::E);
+                    assert!(!set.is_empty());
+                    set.clear();
+                    assert!(set.is_empty());
+                }
+
+                #[test]
+                fn basic_iter_test() {
+                    let mut set = EnumSet::new();
+                    set.insert($e::A);
+                    set.insert($e::B);
+                    set.insert($e::C);
+                    set.insert($e::E);
+
+                    let mut set_2 = EnumSet::new();
+                    let vec: Vec<$e> = set.iter().collect();
+                    for val in vec {
+                        set_2.insert(val);
+                    }
+                    assert_eq!(set, set_2);
+
+                    let mut set_3 = EnumSet::new();
+                    for val in set {
+                        set_3.insert(val);
+                    }
+                    assert_eq!(set, set_3);
+                }
+
+                #[test]
+                fn basic_ops_test() {
+                    assert_eq!(($e::A | $e::B) | ($e::B | $e::C), $e::A | $e::B | $e::C);
+                    assert_eq!(($e::A | $e::B) & ($e::B | $e::C), EnumSet::new() | $e::B);
+                    assert_eq!(($e::A | $e::B) ^ ($e::B | $e::C), $e::A | $e::C);
+                    assert_eq!(($e::A | $e::B) - ($e::B | $e::C), EnumSet::new() | $e::A);
+                }
+
+                #[test]
+                fn basic_set_status() {
+                    assert!(($e::A | $e::B | $e::C).is_disjoint($e::D | $e::E | $e::F));
+                    assert!(!($e::A | $e::B | $e::C | $e::D).is_disjoint($e::D | $e::E | $e::F));
+                    assert!(($e::A | $e::B).is_subset($e::A | $e::B | $e::C));
+                    assert!(!($e::A | $e::D).is_subset($e::A | $e::B | $e::C));
+                }
+
+                #[test]
+                fn debug_impl() {
+                    assert_eq!(format!("{:?}", $e::A | $e::B | $e::W), "EnumSet(A | B | W)");
+                }
+            }
+        }
+    }
+
+    test_enum!(Enum, small_enum);
+    #[cfg(feature = "i128")]
+    test_enum!(LargeEnum, large_enum);
+}