Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hex formatting helper #26

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -200,6 +200,13 @@ impl Felt {
BitArray::new(limbs)
}

/// Helper to produce a hexadecimal formatted string.
/// Equivalent to calling `format!("{self:#x}")`.
#[cfg(feature = "alloc")]
0xLucqs marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1019,6 +1026,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