Anonymous Const Static Declarations


#1

So I ran into this situation that is probably not that uncommon (I couldn’t find if there was an existing feature request open for such a thing, however…I do seem to have a perpetual problem of searching for the wrong thing however). I have a class who should only instantiated with static members, i.e.:

struct BytePattern {
    pattern: &'static [u8],
    mask: &'static [u8]
};

When I want to initialize it, I want to initialize it quickly an concisely inline, so:

return BytePattern {
     pattern: &'static [0x00u8, 0x00u8, 0x01u8, 0x00u8],
     mask:  &'static  [0xFFu8, 0xFFu8, 0xFFu8, 0xFFu8]
 }

Right now this doesn’t seem possible. It does seem that there is a hack to get around this:

return BytePattern {
    pattern: { static P: &'static [u8] = &[0x00u8, 0x00u8, 0x01u8, 0x00u8]; P },
    mask:    { static P: &'static [u8] = &[0xFFu8, 0xFFu8, 0xFFu8, 0xFFu8]; P },
}

But is extra verbose.

More concise would be to allow anonymous static const declarations.