Skip to content

Instantly share code, notes, and snippets.

@Robbepop
Created October 25, 2020 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robbepop/92397c085850b7a8decc55e080fbeffc to your computer and use it in GitHub Desktop.
Save Robbepop/92397c085850b7a8decc55e080fbeffc to your computer and use it in GitHub Desktop.
Constant evulator for next_power_of_two in proc. macros.
fn const_next_power_of_two(ty: TokenStream2, value: TokenStream2) -> TokenStream2 {
quote! {{
if (#value) <= 1 {
0
} else {
let p = (#value) - 1;
let z = p.leading_zeros();
<#ty>::MAX as usize >> z
}
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment