I’m aware of several crates that use macros for similar purposes. I left them aside specifically because I wanted to focus on constants/literals in pattern syntax, match just being a prominent example. I now realise I somewhat buried that intent by the time the post was written:
(More on this below)
However, you example does demonstrate an interesting point, which is that the could be a place for some annotation or cast at the match level for all arms at once:
But really, the point remains that the match is already looking for u8, so telling it again that the arms should be taken as u8 seems superfluous, at either position.
So to focus on something a little more specific, let me highlight two particular questions relevant to current work-in-development.
at the pattern / match site
I was trying to figure out, in the totally vanilla case of matching variants of a plain enum against a value of the same type, which item of The Reference chapter on patterns describes the use of enum variants. I initially thought it might be literals and just not called out expicitly. I thought it might be a somewhat-hidden special-case of “Tuple Struct Patterns” even where there was no tuple as part of the variant. Turns out, it’s “Path Patterns” at the very end, in the same place where constants are.
So one possible route here is to augment the ‘path pattern’ syntax with some kind of cast, or a static equivalent, which I guess overlaps with current work on “type ascription”. If I’ve guessed correctly, other than the as syntax already wished-for, what might this look like?
at the enum definition
Another possible route would be to have something declared on the enum itself (if #[repr(u8)] can’t somehow already be enough) that allows enum variants to be taken as constants of the desired type. This was what the const B_U8 did, and what I understand several macro crates do. In this case, I’m guessing that the current work on const functions and expressions could lead to some more inherent way to get at a const of the suitable type, maybe something akin to a const version of into(). If I’ve guessed correctly, what might this look like?