Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

implement display and debug trait for Address #1080

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Changes from all 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
21 changes: 20 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use cairo_vm::{
felt::Felt252, serde::deserialize_program::BuiltinName, vm::runners::builtin_runner,
};
use cairo_vm::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine};
use core::fmt;
use num_integer::Integer;
use num_traits::{Num, ToPrimitive};
use serde::{Deserialize, Serialize};
Expand All @@ -36,9 +37,21 @@ pub type CompiledClassHash = [u8; 32];
//* Address
//* -------------------

#[derive(Debug, Clone, PartialEq, Hash, Eq, Default, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Hash, Eq, Default, Serialize, Deserialize)]
pub struct Address(pub Felt252);

impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "0x{}", self.0.to_str_radix(16))
}
}

impl fmt::Debug for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}

//* -------------------
//* Helper Functions
//* -------------------
Expand Down Expand Up @@ -916,4 +929,10 @@ mod test {
],
);
}

#[test]
fn test_address_display() {
let address = Address(Felt252::from(123456789));
assert_eq!(format!("{}", address), "0x75bcd15".to_string());
}
}