Compiletime processing with const generics functions
casualhacks.net/blog/2020-05-31/compiletime-p...Posted May 31, 2020, 7:12 PM UTC
Posted May 31, 2020, 7:12 PM UTC
Would love to see
pub const fn obfuscate**<const** LEN: usize**>(s: &str) -> ObfString<[u8; LEN]> { ... }**
being replaced with
pub const fn obfuscate(s: &str) -> ObfString<{s.len()}> { ... }
Yup me too! Unfortunatly I don't think this is possible as const fn means that the function is optionally const, but can still be called with runtime values.
So there would need to be some syntax to indicate the argument must be a const value, eg:
pub const fn obfuscate(const s: &str) -> ObfString<{s.len()}> { ... }
That's basically just a const generic parameter.
This almost works: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=6b565f45808c7b2e6c22b6cef65a9b8e
Oh of course, also very nice that it almost works :)
I've made a correction:
In the previous versions of the blog post I simply returned the value of the const evaluated expr like so:
macro_rules! entropy {
() => { entropy(file!(), line!(), column!()) };
}
However it turns out that in this case the value is not evaluated at compiletime and instead gets calculated at runtime (specifically the hash of the filename is calculated at runtime).
In order to force the const eval to do its thing it is necessary to assign it to a const variable first:
macro_rules! entropy {
() => {{
const ENTROPY: u64 = entropy(file!(), line!(), column!());
ENTROPY
}};
}
This triggers const eval correctly and the final value is embedded directly in the binary.
It's not entirely clear to me where this decision comes from (should a const value be evaluated as eagerly as possible, or only if necessarly required?).
[deleted]
I do hope to see more const fn.
But with the stabilization of proc-macros in expr context (whereas before you had to use the proc-macro-hack workaround) I'm afraid that for now we'll see an increase in proc-macros for compiletime function evaluation.
Perhaps in the future when const eval gets stabilized and more expressive we'll see this trend go back.