about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuuk Wester <luuk.wester@gmail.com>2025-01-21 14:45:30 +0100
committerLuuk Wester <luuk.wester@gmail.com>2025-01-21 14:45:30 +0100
commite08f6d45305617b180856ae75969aac6c0db3a80 (patch)
treee58efb05e35a1617d539de863e0bd102bc398e6f
parent398cd2dbf63d67a0f58e1fa730883a7420f8ef70 (diff)
downloadrust-e08f6d45305617b180856ae75969aac6c0db3a80.tar.gz
rust-e08f6d45305617b180856ae75969aac6c0db3a80.zip
switch from using leading zeros to trailing zeros
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/render.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/render.rs b/src/tools/rust-analyzer/crates/ide/src/hover/render.rs
index 95be7c5618e..fb7c2904afc 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/render.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover/render.rs
@@ -1235,9 +1235,12 @@ fn is_pwr2plus1(val: u128) -> bool {
     val != 0 && is_pwr2(val - 1)
 }
 
+/// Formats a power of two as an exponent of two, i.e. 16 => ⁴. Note that `num` MUST be a power
+/// of 2, or this function will panic.
 fn pwr2_to_exponent(num: u128) -> String {
     const DIGITS: [char; 10] = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'];
-    (127 - num.leading_zeros())
+    assert_eq!(num.count_ones(), 1);
+    num.trailing_zeros()
         .to_string()
         .chars()
         .map(|c| c.to_digit(10).unwrap() as usize)