I'm currently having fun doing some with macros and type-level programming in Rust, so my question here is more of a "how far can I push Rust in this direction?" rather than "please help me or my program is never going to work".
What I'd like to do
I'm writing a pattern macro and I'd like to introduce type constraints within that macro.
For instance:
trait FooBar {
const TAG: &'static str;
}
match foo { // foo implements `FooBar`
my_macro!{x, y, z} => ...
| ...
}
I would like my_macro!
to cause a compile-time error if, for instance, foo::TAG
is not "ACCEPTABLE"
.
Why would you want to do that?
I'm writing a few macros that implement anonymous structs in Rust. It's a proof of concept, in an attempt to (hopefully) show that we do not need deep changes in the type system to implement anonymous structs and named arguments in the language.
In my current implementation, type-safety relies upon these TAG
fields. It works if we're willing to rewrite the expression side of pattern matches, to perform these type-level shenanigans, but that makes the macros much more complicated and brittle. I'm looking for a better way to perform the same checks.
I'm ok with restricting myself to Rust Nightly.