tkaitchuck / constrandom

Macro to generate random constants in Rust https://xkcd.com/221/
Apache License 2.0
73 stars 14 forks source link

[u8; N] parsing panics due to missing space #34

Closed geo-ant closed 7 months ago

geo-ant commented 7 months ago

Hi,

I tried using your crate like in the example in the documentation

const MY_RANDOM_BYTES: [u8; 32] = const_random!([u8; 32]);

That will panic with a message "invalid type". The underlying problem is in the parsing code in this line

byte_array: &str if byte_array.starts_with("[u8 ; ") && byte_array.ends_with(']')=> ...

Here you are sensitive to spaces and the formatting is problematic if someone has rustfmt enabled like I do. I could submit a PR to fix this, but rather than just patching that particular line I would want to use the syn crate to parse the tokens correctly. I don't know if you would want this dependency though, so I thought I'd ask. Your code works when I use

#[rustfmt::skip]
const MY_RANDOM_BYTES: [u8; 32] = const_random!([u8 ; 32]);

which seems a bit silly :)

dtolnay commented 7 months ago

This will be fixed by #33.