diff --git a/CHANGELOG.md b/CHANGELOG.md index 975a323db42..0356e2c1d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed the unused `Id::hex`. - Deprecated `Id::hex_path` and added `Id::legacy_hex_path` as a replacement. - Added `Id::clean_hex_path` as an alternative to `Id::legacy_hex_path`. + - Changed `Id::hex_clean` to format zero as `"00"`. ### Fixed diff --git a/core/src/types.rs b/core/src/types.rs index 3c7b87350a3..302178673dc 100644 --- a/core/src/types.rs +++ b/core/src/types.rs @@ -145,7 +145,8 @@ impl Id { /// Hex representation of this ID without leading zeroes. /// /// This implementation skips all leading bytes that are zero so that the resulting hex string - /// always has an even number of characters and does not start with more than one zero. + /// always has an even number of characters and does not start with more than one zero. 0 is + /// formatted as `"00"`. pub fn hex_clean(&self) -> HexClean { HexClean(self.0) } @@ -198,7 +199,8 @@ impl<'de> Deserialize<'de> for Id { /// Hex representation of an `u128` without leading zeroes. /// /// This implementation skips all leading bytes that are zero so that the resulting hex string -/// always has an even number of characters and does not start with more than one zero. +/// always has an even number of characters and does not start with more than one zero. 0 is +/// formatted as `"00"`. pub struct HexClean(pub u128); impl core::fmt::Display for HexClean { @@ -224,8 +226,9 @@ impl<'a> HexCleanBytes<'a> { upper: true, } } else { + // Format 0 as "00" Self { - array: &[], + array: &[0], upper: true, } } @@ -529,7 +532,7 @@ mod tests { #[test] fn test_id_clean_hex_path() { - assert_eq!(Id(0).clean_hex_path().as_str(), ""); + assert_eq!(Id(0).clean_hex_path().as_str(), "00"); assert_eq!(Id(1).clean_hex_path().as_str(), "01"); assert_eq!(Id(10).clean_hex_path().as_str(), "0a"); assert_eq!(Id(16).clean_hex_path().as_str(), "10"); @@ -544,7 +547,7 @@ mod tests { #[test] fn test_id_hex_clean() { - assert_eq!(Id(0).hex_clean().to_string(), ""); + assert_eq!(Id(0).hex_clean().to_string(), "00"); assert_eq!(Id(1).hex_clean().to_string(), "01"); assert_eq!(Id(10).hex_clean().to_string(), "0a"); assert_eq!(Id(16).hex_clean().to_string(), "10");