Lymia / enumset

A library for compact bit sets containing enums.
Apache License 2.0
91 stars 35 forks source link

Feature request: A public `BIT_WIDTH` and `enum_into_u32` #45

Closed nicopap closed 3 months ago

nicopap commented 1 year ago

I have two data structures indexed by T: EnumSetType.

/// A bitset similar to [`BitMatrix`][super::BitMatrix],
/// but with a fixed column and row count, one row per `T` variant.
pub struct EnumBitMatrix<T: EnumSetType>(Box<[u32]>, PhantomData<T>);

/// A MultiMap stored in a [`JaggedArray`].
///
/// The key set need to be bound and exhaustively known at compile time,
/// ie: it must be an enum derived with `#[derive(EnumSetType)]`.
///
/// Use it as follow:
/// `EnumMultiMap<MyEnumSet, String, { (MyEnumSet::BIT_WIDTH - 1) as usize }>`
pub struct EnumMultiMap<K: EnumSetType, V, const CLM: usize>

I use the bit position of T as index in my collection. I also use BIT_WIDTH to size the arrays used in those data structures. It's very pleasant.

However, there is no way to get a bit position outside of EnumSeTypePrivate::enum_into_u32, and no const way of getting the bit width outside of EnumSeTypePrivate::BIT_WIDTH. I don't want to rely on private/unstable API, so I'm opening an issue to request a "stable" way of getting those.

An explicit representation doesn't solve this. The BIT_WIDTH and enum_into_u32 is independent from the representation of T, Ideally I can have as many variants as I want.

I imagine the "bit position" is problematic for enums defined with explicit variant values, so I guess this needs to be part of a new trait only valid for enums without explicit variant values. For example, EnumBitSetType.

What do you think of this? Would you be open to a PR for such a new trait?

Lymia commented 1 year ago

The bit position is actually pretty stable, since it's already exposed via e.g. EnumSet::to_u64. I'll think about whether this is viable at this point; it should be.