We have:
pub struct Animal { Dog, Cat(Cat), Horse(usize, String) }
We want to auto generate:
impl Animal { pub fn as_dog(&self) -> Option<()>, pub fn as_cat(&self) -> Option<&Cat>, pub fn as_horse(&self) -> Option<(&usize, &String)>, }
Is there an existing crate that provides this as a derive macro ?
I can't be sure, but I've wanted something like this¹ before and never found it. And I never wrote it myself because cost/benefit didn't lean that way at the time.
¹ technically, this and enum variant predicates eg is_dog() etc.
is_dog()
Here are a couple of options:
https://crates.io/crates/variantly
https://crates.io/crates/enum-methods
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.