Hello everyone! I have a Rust enum with a significant number of variants, and I need to implement a trait that has many methods. Right now, this leads to a lot of repetitive match
statements, especially for methods like merge(&mut self, rhs: Self)
, process_record(...)
, etc. Each method requires matching on every variant, which is cumbersome.
I’ve tried using a procedural macro that generates the trait implementation by expanding a big match
for each method, but it’s still quite verbose. I’m wondering:
- Are there any crates or existing solutions that help reduce this kind of boilerplate for large enums implementing large traits?
- What are the general best practices in Rust for handling this situation? I’ve heard about the Visitor pattern, macros, or even using trait objects, but I’m not sure which approach is considered idiomatic or if there’s a community-recommended library.
Thanks in advance for any insights or suggestions!