Skip to content

Commit

Permalink
feat: hex formatting helper (starknet-io#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen authored Dec 20, 2023
1 parent 6686436 commit 33c8fbd
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/starknet-types-core/src/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitArrayStore> {
Expand Down Expand Up @@ -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::<Felt>()) {
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);
Expand Down

0 comments on commit 33c8fbd

Please sign in to comment.