#enums #bitflags #union #variant #du #filter #bitmask

filterable-enum

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

3 releases

0.1.2 Oct 10, 2024
0.1.1 Oct 10, 2024
0.1.0 Apr 26, 2024

#1058 in Rust patterns

Download history 38/week @ 2024-08-05 22/week @ 2024-08-12 13/week @ 2024-08-19 45/week @ 2024-08-26 9/week @ 2024-09-02 109/week @ 2024-09-16 47/week @ 2024-09-23 5/week @ 2024-09-30 296/week @ 2024-10-07 137/week @ 2024-10-14 35/week @ 2024-10-21 39/week @ 2024-10-28 8/week @ 2024-11-04 13/week @ 2024-11-18

72 downloads per month
Used in tracexec

MIT license

5KB

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.7–2.3MB
~48K SLoC