#enums #bitflags #du #filter #union #bitmask #wrapper

filterable-enum

A library for generating filterable enums (Combining bitflags and discriminated unions)

1 unstable release

0.1.0 Apr 26, 2024

#807 in Rust patterns

Download history 121/week @ 2024-04-21 60/week @ 2024-04-28 116/week @ 2024-05-05 119/week @ 2024-05-12 130/week @ 2024-05-19 50/week @ 2024-05-26

422 downloads per month
Used in tracexec

MIT license

4KB

filterable-enum

Filterable wrapper for discriminated unions(DU) in rust.

What is this?

In some cases, you may want to filter a DU by a bitmask to check if it matches a subset of all variants. This crate provides a derive macro to generate a filterable wrapper for a DU.

Ideally, I think this whole crate should be unnecessary. Rust already stores a tag for representing the variant of a DU. However, Rust does not expose this tag to the user, so I cannot really use that tag as a bitflag.

How to use

See the tests for examples.

To add more derives to the Kind enum, use #[filterable_enum(kind_extra_derive=DeriveMacroYouWant)].

To add more attrs to the Kind enum, use #[filterable_enum(kind_extra_attrs="ATTR")] like #[filterable_enum(kind_extra_attrs="strum(serialize_all = \"kebab-case\")")].

Note

This is the first procedural macro I've ever written, so I'm not sure if I'm doing it idiomatic or right. Feel free to open an issue or PR if you have any suggestions or improvements.

Dependencies

~1.8–2.4MB
~49K SLoC