From 121b435a9b0ad6223a2f98c82b708cab92b84400 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Aug 2024 06:22:41 +1000 Subject: [PATCH 1/3] base58: Use from to cast u8 `d58` is the iterator value from `Bytes` (iter returned by `String::bytes`). As such we can infallibly convert it using `from`. Internal change only, no external changes. --- base58/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base58/src/lib.rs b/base58/src/lib.rs index e9f3b8ec0..7e882f4fc 100644 --- a/base58/src/lib.rs +++ b/base58/src/lib.rs @@ -73,11 +73,11 @@ pub fn decode(data: &str) -> Result, InvalidCharacterError> { // Build in base 256 for d58 in data.bytes() { // Compute "X = X * 58 + next_digit" in base 256 - if d58 as usize >= BASE58_DIGITS.len() { + if usize::from(d58) >= BASE58_DIGITS.len() { return Err(InvalidCharacterError { invalid: d58 }); } - let mut carry = match BASE58_DIGITS[d58 as usize] { - Some(d58) => d58 as u32, + let mut carry = match BASE58_DIGITS[usize::from(d58)] { + Some(d58) => u32::from(d58), None => { return Err(InvalidCharacterError { invalid: d58 }); } From 3f8cf1b335d5f204d0c9039accec3c1121d8fd2b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Aug 2024 07:08:46 +1000 Subject: [PATCH 2/3] base58: Use from and document cast Done in an effort to reduce the cognitive load of reading the loop. The base68 decode and encode algorithm uses a `u32` intentionally for multiplication and a cast to `u8` intentionally when carrying. Use `From` where possible and document the cast. --- base58/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/base58/src/lib.rs b/base58/src/lib.rs index 7e882f4fc..5705a2d7f 100644 --- a/base58/src/lib.rs +++ b/base58/src/lib.rs @@ -83,8 +83,8 @@ pub fn decode(data: &str) -> Result, InvalidCharacterError> { } }; for d256 in scratch.iter_mut().rev() { - carry += *d256 as u32 * 58; - *d256 = carry as u8; + carry += u32::from(*d256) * 58; + *d256 = carry as u8; // cast loses data intentionally carry /= 256; } assert_eq!(carry, 0); @@ -208,7 +208,7 @@ where let mut leading_zeroes = true; // Build string in little endian with 0-58 in place of characters... for d256 in data { - let mut carry = d256 as usize; + let mut carry = usize::from(d256); if leading_zeroes && carry == 0 { leading_zero_count += 1; } else { @@ -216,13 +216,13 @@ where } for ch in buf.slice_mut() { - let new_ch = *ch as usize * 256 + carry; - *ch = (new_ch % 58) as u8; + let new_ch = usize::from(*ch) * 256 + carry; + *ch = (new_ch % 58) as u8; // cast loses data intentionally carry = new_ch / 58; } while carry > 0 { - buf.push((carry % 58) as u8); + buf.push((carry % 58) as u8); // cast loses data intentionally carry /= 58; } } @@ -233,7 +233,7 @@ where } for ch in buf.slice().iter().rev() { - writer.write_char(BASE58_CHARS[*ch as usize] as char)?; + writer.write_char(char::from(BASE58_CHARS[usize::from(*ch)]))?; } Ok(()) From 4b66a479b0ddf71d2bcde61e2d8f34a197b479ad Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 7 Aug 2024 05:33:31 +1000 Subject: [PATCH 3/3] base58: Use u32 instead of usize The `carry` variable is used as a value not as an array index so we should use a `u32` - this is inline with other usage in the crate. --- base58/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base58/src/lib.rs b/base58/src/lib.rs index 5705a2d7f..7e0f15239 100644 --- a/base58/src/lib.rs +++ b/base58/src/lib.rs @@ -208,7 +208,7 @@ where let mut leading_zeroes = true; // Build string in little endian with 0-58 in place of characters... for d256 in data { - let mut carry = usize::from(d256); + let mut carry = u32::from(d256); if leading_zeroes && carry == 0 { leading_zero_count += 1; } else { @@ -216,7 +216,7 @@ where } for ch in buf.slice_mut() { - let new_ch = usize::from(*ch) * 256 + carry; + let new_ch = u32::from(*ch) * 256 + carry; *ch = (new_ch % 58) as u8; // cast loses data intentionally carry = new_ch / 58; }