Skip to main content

Crate filterable_enum

Crate filterable_enum 

Source
Expand description

§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. 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.

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 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:

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.

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.

Re-exports§

pub use enumflags2;

Traits§

EnumFilter
A filter that tests whether it includes a variant’s bitflag.
FilterableEnum
An enum that can be filtered by its variant’s bitflag.

Attribute Macros§

filterable_enum
Makes an enum filterable using its own integer tag as a bitflag.