From 33c8fbdc7dbb9efce3937b0a35a90800c52899ce Mon Sep 17 00:00:00 2001 From: Mario Rugiero Date: Wed, 20 Dec 2023 10:12:38 -0300 Subject: [PATCH] feat: hex formatting helper (#26) --- crates/starknet-types-core/src/felt.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/starknet-types-core/src/felt.rs b/crates/starknet-types-core/src/felt.rs index a354a6f..dc5820b 100644 --- a/crates/starknet-types-core/src/felt.rs +++ b/crates/starknet-types-core/src/felt.rs @@ -210,6 +210,13 @@ impl Felt { BitArray::new(limbs) } + /// Helper to produce a hexadecimal formatted string. + /// Equivalent to calling `format!("{self:#x}")`. + #[cfg(feature = "alloc")] + pub fn to_hex_string(&self) -> alloc::string::String { + alloc::format!("{self:#x}") + } + /// Converts to little-endian bit representation. /// This is as performant as [to_bits_be](Felt::to_bits_be) pub fn to_bits_le(&self) -> BitArray { @@ -1060,6 +1067,12 @@ mod test { prop_assert_eq!(x, y); } + #[test] + #[cfg(feature = "alloc")] + fn to_hex_string_is_same_as_format(ref x in any::()) { + prop_assert_eq!(alloc::format!("{x:#x}"), x.to_hex_string()); + } + #[test] fn from_bytes_le_slice_works_for_all_lengths(x in 0..1000usize) { let bytes: [u8; 1000] = core::array::from_fn(|i| i as u8);