Define constants
Closed this issue · 2 comments
Wondering if there'd be any interest in defining constants for all values for each of the types. I have been playing with this for packet parsing, and there's often a field that corresponds to a fixed set of packet types that it would be handy to be able to use constants for.
struct MyPacket {
...
}
impl MyPacket {
pub const TYPE: u4 = u4::C10; // The value '10' in a u4
}
fn parse(...) {
let type: u4 = ...
match type {
MyPacket::TYPE => { ... },
OtherPacket::TYPE => { ... },
// etc
}
}
I like the idea. But this is >10^40 constants, I imagine this would be a problem for both the rust compiler in terms of the amount of symbols and the recursion depths of the macros required to write out all these constants. Am I wrong here? Are there any smart ways to only having to define the constants actually in use?
I see that panic is also supported in const
context now. There is no reason for not making new
a const
function. Would that solve the problem?
But this is >10^40 constants, I imagine this would be a problem for both the rust compiler in terms of the amount of symbols and the recursion depths of the macros required to write out all these constants.
Good point...when I played around with this in my own lib I only supported up to u7, so this would probably be a bit much.
I see that panic is also supported in const context now. There is no reason for not making new a const function. Would that solve the problem?
Yeah I hadn't thought about that but that would work fine!