Hello,
thanks to several tips & help from people in NOM gitter, i am slowly building a toy parser for ABNF according to a RFC... In the process, i stumbled upon a specific problem/pattern i assume is not that infrequent, but am not sure on elegant way to address it...
I have an enum of different items with various inner data:
enum ModuleHeaderStatements<'a> {
YangVersion(&'a str),
Namespace(&'a NamespaceData),
Prefix(&'a SomeOtherPrefixStuff),
}
I collect an array/vec of an enum instances in code...
let sub_stmts: Vec<ModuleHeaderStatements> = parse_some_stuff(...);
At this point, i need to run a check on the gathered collection (sub_stmts
), and verify that there are specific numbers of various enum instances in the array...
E.g., i expect to have 2 pieces of ModuleHeaderStatements::Namespace
, one piece of ModuleHeaderStatements::Prefix
, and no pieces of ModuleHeaderStatements::YangVersion
.
These counts i need to pass around as arguments/parameters of some "general" function, due to pattern being repeated many times for different enums/node types etc.
I have found std::mem::discriminant
that i can run on already existing enum instance - to allow for e.g. building a count map/vec, or any counter-like struct to identify each of variants counts.
Yet, i find it a bit puzzling that there seems to be no way to create constant discriminant without constructing some "dummy" enum instance.
Building up artificial enum instance to be able to pass enum variant discriminant + count around as parameter feels a bit cumbersome, or is it not?
How would you address this "problem"? Defining some trait that adds/implements indexing constant for enum variants? Or do i need to get some macro based enum crate?