I am trying to build a sort of DSL that needs to access data from different variants of an enum. In continuation to my previous question, I would like to do something like this:
enum Sample {
Var1 {data: i32},
Var2 {data1: i32, data2: i32},
Var3 {data1: f32, data2:f32},
Var4,
}
macro_rules! test_member {
($val:ident, $var:path, $member_name:ident, $member_val:expr) => {
if let $var(s) = $val { // <== some magic happens here
s.$member_name == $member_val
} else {
false
}
}
}
// use
let s = Sample::Var2{data1: 1, data2: 2};
let is_val = test_member!(s, Sample::Var2, data2, 2);
Is this even possible? Is there a syntax that allows getting the "inner" struct from the enum variation?
The struct associated with the Var2 variant is anonymous. You can access and store all of its fields, but can't access/use the anonymous struct itself.
It is a bit limiting that you can't do something like having a function take a specific variant as an input when you know you have a specific variant.
Boilerplate-y workaround is to "extract" the enum variants into dedicated structs: