eset 0.1.0

A simple library for flags like enums.
Documentation

/// Create an [`ESet`](crate::ESet) with the `|` syntax.
///
/// This does not require the [`derive_eset`] macro being invoked.
///
/// Example
///
///```
/// # use eset::*;
/// # #[repr(u8)]
/// # #[derive(Debug)]
/// enum A {
///    Foo,
///    Bar,
/// }
/// # impl Into<u8> for A { fn into(self) -> u8 { self as u8 }}
/// #
/// # impl From<u8> for A {
/// #     fn from(value: u8) -> Self {
/// #         match value {
/// #             0 => Self::Foo,
/// #             1 => Self::Bar,
/// #             _ => panic!()}}}
/// #
/// # impl ReprEnum for A {
/// #     type Repr = u8;
/// #     type FlagsRepr = u64;
/// # }
///
/// assert_eq!(eset!(A::Foo | A::Bar), ESet::new_flags([A::Foo, A::Bar]));
///```
#[macro_export]
macro_rules! eset {
    ($expr: path) => {
        ::eset::ESet::<_, '|'>::new($expr)
    };
    ($expr: path | $($exprs: path)|*) => {
        eset!($expr) | eset!($($exprs)|*)
    };

    ($sep: literal, $expr: path) => {
        ::eset::ESet::<_, $sep>::new($expr)
    };
    ($sep: literal $expr: path | $($exprs: path)|*) => {
        eset!($sep, $expr) | eset!($sep, $($exprs)|*)
    };
}

/// An opt-in derive that adds [`Into<ESet<T>>`] for T and operator [`BitOr<T, Output=ESet<T>>`](std::ops::BitOr).
///
/// This supports T as the right hand side for [`ESet`](crate::ESet) operations like `|` or `&=`.
///
/// You can use methods like [`insert`](crate::ESet::insert) or [`with`](crate::ESet::with) instead of this macro.
///
/// # Examples
///
///```
/// # use eset::*;
/// # #[repr(u8)]
/// # #[derive(Debug)]
/// enum A {
///    Foo,
///    Bar,
/// }
/// # impl Into<u8> for A { fn into(self) -> u8 { self as u8 }}
/// #
/// # impl From<u8> for A {
/// #     fn from(value: u8) -> Self {
/// #         match value {
/// #             0 => Self::Foo,
/// #             1 => Self::Bar,
/// #             _ => panic!()}}}
/// #
/// # impl ReprEnum for A {
/// #     type Repr = u8;
/// #     type FlagsRepr = u64;
/// # }
///
/// derive_eset!(A);
/// assert_eq!(A::Foo | A::Bar, ESet::new_flags([A::Foo, A::Bar]));
///```
#[macro_export]
macro_rules! derive_eset {
    ($expr: ty) => {
        impl<const SEP: char> ::std::convert::Into<::eset::ESet<$expr, SEP>> for $expr {
            fn into(self) -> ::eset::ESet<$expr, SEP> {
                ::eset::ESet::new(self)
            }
        }

        impl ::core::ops::BitOr for $expr {
            type Output = ::eset::ESet<$expr>;
            fn bitor (self, rhs: Self) -> Self::Output {
                ::eset::ESet::<$expr>::new(self) | ::eset::ESet::<$expr>::new(rhs)
            }
        }
    };
}