+macro_rules! conversion_impls {
+ (
+ $(for_num!(
+ $underlying:ty, $underlying_str:expr, $from_fn:ident, $to_fn:ident,
+ $from:ident $try_from:ident $from_truncated:ident
+ $to:ident $try_to:ident $to_truncated:ident
+ );)*
+ ) => {
+ impl <T : EnumSetType> EnumSet<T> {$(
+ #[doc = "Returns a `"]
+ #[doc = $underlying_str]
+ #[doc = "` representing the elements of this set. \n\nIf the underlying bitset will \
+ not fit in a `"]
+ #[doc = $underlying_str]
+ #[doc = "`, this method will panic."]
+ pub fn $to(&self) -> $underlying {
+ self.$try_to().expect("Bitset will not fit into this type.")
+ }
+
+ #[doc = "Tries to return a `"]
+ #[doc = $underlying_str]
+ #[doc = "` representing the elements of this set. \n\nIf the underlying bitset will \
+ not fit in a `"]
+ #[doc = $underlying_str]
+ #[doc = "`, this method will instead return `None`."]
+ pub fn $try_to(&self) -> Option<$underlying> {
+ self.__enumset_underlying.$to_fn()
+ }
+
+ #[doc = "Returns a truncated `"]
+ #[doc = $underlying_str]
+ #[doc = "` representing the elements of this set. \n\nIf the underlying bitset will \
+ not fit in a `"]
+ #[doc = $underlying_str]
+ #[doc = "`, this method will truncate any bits that don't fit."]
+ pub fn $to_truncated(&self) -> $underlying {
+ AsPrimitive::<$underlying>::as_(self.__enumset_underlying)
+ }
+
+ #[doc = "Constructs a bitset from a `"]
+ #[doc = $underlying_str]
+ #[doc = "`. \n\nIf a bit that doesn't correspond to an enum variant is set, this \
+ method will panic."]
+ pub fn $from(bits: $underlying) -> Self {
+ Self::$try_from(bits).expect("Bitset contains invalid variants.")
+ }
+
+ #[doc = "Attempts to constructs a bitset from a `"]
+ #[doc = $underlying_str]
+ #[doc = "`. \n\nIf a bit that doesn't correspond to an enum variant is set, this \
+ method will return `None`."]
+ pub fn $try_from(bits: $underlying) -> Option<Self> {
+ let bits = <T::Repr as FromPrimitive>::$from_fn(bits);
+ let mask = Self::all().__enumset_underlying;
+ bits.and_then(|bits| if (bits & !mask) == T::Repr::zero() {
+ Some(EnumSet { __enumset_underlying: bits })
+ } else {
+ None
+ })
+ }
+
+ #[doc = "Constructs a bitset from a `"]
+ #[doc = $underlying_str]
+ #[doc = "`, ignoring invalid variants."]
+ pub fn $from_truncated(bits: $underlying) -> Self {
+ let mask = Self::all().$to_truncated();
+ let bits = <T::Repr as EnumSetTypeRepr>::$from_fn(bits & mask);
+ EnumSet { __enumset_underlying: bits }
+ }
+ )*}
+ }
+}
+
+conversion_impls! {
+ for_num!(u8, "u8", from_u8, to_u8,
+ from_u8 try_from_u8 from_u8_truncated as_u8 try_as_u8 as_u8_truncated);
+ for_num!(u16, "u16", from_u16, to_u16,
+ from_u16 try_from_u16 from_u16_truncated as_u16 try_as_u16 as_u16_truncated);
+ for_num!(u32, "u32", from_u32, to_u32,
+ from_u32 try_from_u32 from_u32_truncated as_u32 try_as_u32 as_u32_truncated);
+ for_num!(u64, "u64", from_u64, to_u64,
+ from_u64 try_from_u64 from_u64_truncated as_u64 try_as_u64 as_u64_truncated);
+ for_num!(u128, "u128", from_u128, to_u128,
+ from_u128 try_from_u128 from_u128_truncated as_u128 try_as_u128 as_u128_truncated);
+ for_num!(usize, "usize", from_usize, to_usize,
+ from_usize try_from_usize from_usize_truncated
+ as_usize try_as_usize as_usize_truncated);
+}
+