Make `{encode,decode}_len` const fns?
When operating on fixed length hashes like SRI, it's great to use arrays on stack as buffers. But only constants and const-fns can be used in array length expressions, results in manual magic numbers like let buf = [0u8; 44] in code (base64 encoded length of SHA256 hash). With const-fns, we could possibly write let buf = [0u8; BASE64.encode_len(32)]; to make it clear.
Thanks for the suggestion! I think it makes sense to do something like that however:
- I won't have time to work on it before mid June.
- I'm not sure it will be possible to have those functions const. If not I'll expose them in data-encoding-macro. Or if possible in nightly I'll add a feature.
I'll come back to this later.
As expected, making {encode,decode}_len const fns does not work for a few different reasons:
- I use traits and it's not possible to have
const fnin traits. - I use
Cow(when theallocfeature is enabled) and rely onDerefwhich is not const.
So I'll go with the solution of making those functions macros in the data-encoding-macro crate. I'll only have time to look at this at the earliest this week-end though. I'll ping here when done.
You can test if #70 is fixing your issue. Instead of writing let buf = [0u8; BASE64.encode_len(32)]; as in your original comment, you would need to write let buf = [0u8; base64_encode_len!(32)]; instead. If that's useful, I'll merge and make a release.
Due to lack of activity I'm going to close this. This will eventually get fixed once const support in Rust is improved.