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

Commit cb7b277

Browse files
author
Antonio Calvín García
authored
implement display and debug trait for Address (#1080)
* implement display and debug trait for Address * hexa fmt
1 parent 2bee0c5 commit cb7b277

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/utils.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cairo_vm::{
1616
felt::Felt252, serde::deserialize_program::BuiltinName, vm::runners::builtin_runner,
1717
};
1818
use cairo_vm::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine};
19+
use core::fmt;
1920
use num_integer::Integer;
2021
use num_traits::{Num, ToPrimitive};
2122
use serde::{Deserialize, Serialize};
@@ -36,9 +37,21 @@ pub type CompiledClassHash = [u8; 32];
3637
//* Address
3738
//* -------------------
3839

39-
#[derive(Debug, Clone, PartialEq, Hash, Eq, Default, Serialize, Deserialize)]
40+
#[derive(Clone, PartialEq, Hash, Eq, Default, Serialize, Deserialize)]
4041
pub struct Address(pub Felt252);
4142

43+
impl fmt::Display for Address {
44+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45+
write!(f, "0x{}", self.0.to_str_radix(16))
46+
}
47+
}
48+
49+
impl fmt::Debug for Address {
50+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51+
write!(f, "{}", self)
52+
}
53+
}
54+
4255
//* -------------------
4356
//* Helper Functions
4457
//* -------------------
@@ -916,4 +929,10 @@ mod test {
916929
],
917930
);
918931
}
932+
933+
#[test]
934+
fn test_address_display() {
935+
let address = Address(Felt252::from(123456789));
936+
assert_eq!(format!("{}", address), "0x75bcd15".to_string());
937+
}
919938
}

0 commit comments

Comments
 (0)