filterable-enum 0.2.0

A library for generating filterable enums (Combining bitflags and discriminated unions)
Documentation
# filterable-enum

Filter Rust enums by a bitmask of their variants, using the enum's own tag.

## What is this?

The `#[filterable_enum]` attribute assigns power-of-two discriminants to an enum
and generates a matching `*Kind` enum for use with `enumflags2::BitFlags`.
Filtering reads the original enum's tag directly, without a wrapper or a second
stored tag. Unit variants, tuple variants, named fields, and generics are supported.

The macro sets an explicit integer representation on both enums. This makes
reading the tag sound under Rust's [enum layout guarantees](https://doc.rust-lang.org/reference/items/enumerations.html#pointer-casting).
It changes the original enum's layout and discriminants, and can prevent Rust's
usual enum layout optimizations.

## How to use

Add `filterable-enum` and `enumflags2` as dependencies. The generated bitflags
implementation requires `enumflags2` to be a direct dependency.

```rust
use filterable_enum::{filterable_enum, FilterableEnum};

#[filterable_enum(repr = "u8")]
#[derive(Debug, PartialEq)]
enum Event {
    Started,
    Message(String),
    Progress { current: usize, total: usize },
}

let event = Event::Message("hello".into());
let filter = EventKind::Started | EventKind::Message;

assert_eq!(event.filterable_id(), EventKind::Message);
assert_eq!(event.filter_ref(filter), Some(&event));
assert_eq!(event.filter_ref(EventKind::Progress), None);
assert_eq!(event.filter_and_take(filter), Some(Event::Message("hello".into())));
```

The default representation is `u32`. Use `repr = "u8"`, `"u16"`, `"u32"`,
`"u64"`, or `"u128"` to select a different width. The number of variants must
fit in that many bits (for example, at most eight variants for `u8`). Each
variant receives `1 << index` in declaration order. The macro owns the
representation and discriminants; do not add a separate `#[repr(...)]` or
explicit variant discriminants.

To add derives or attributes to the generated `Kind` enum, pass
`kind_extra_derive = Hash` or `kind_extra_attrs = "allow(dead_code)"` inside
`#[filterable_enum(...)]`. These options can be repeated. For example,
`kind_extra_attrs = "strum(serialize_all = \"kebab-case\")"` passes that attribute
to the generated enum. Its visibility matches the original enum's visibility.

See the [tests](https://github.com/kxxt/filterable-enum/blob/main/tests/attribute_impl.rs)
for more examples.

## Migrating from 0.1

Replace `#[derive(FilterableEnum)]` with `#[filterable_enum]`, importing the
attribute alongside the `FilterableEnum` trait. Move any existing
`#[filterable_enum(...)]` options into this attribute, placed before other
attributes. Use `MyEnum` values directly in place of `FilterableMyEnum` and
remove conversions into the wrapper. The filtering methods and `MyEnumKind`
flags keep their names.

Manual implementations now use `impl FilterableEnum for MyEnum` rather than
`impl FilterableEnum<MyEnum> for FilterableMyEnum`. Only `filterable_id` and the
associated types are required; the filtering methods have default implementations.
`EnumFilter` is already implemented for `BitFlags<Kind>`.

## Testing with Miri

Install Miri and run the full suite with strict provenance and alignment checks:

```sh
rustup toolchain install nightly --profile minimal --component miri --component rust-src
MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check" cargo +nightly miri test --workspace --locked
```

The Miri CI workflow also runs on `s390x-unknown-linux-gnu` to check big-endian
tag reads. To run that target locally, append `--target s390x-unknown-linux-gnu`
to the test command. Miri builds its own sysroot, so a cross linker is unnecessary.
See the [Miri documentation](https://github.com/rust-lang/miri#using-miri).

The tests cover tag reads for every supported representation, inactive payloads
and padding, borrowed and highly aligned payloads, moves, variant changes,
ownership and drops, and the highest bit of a `u128` tag. These tests also run
under ordinary `cargo test`.