@@ -19,7 +19,7 @@ use crate::traits::AsBytes;
1919use crate :: traits:: ByteConversion ;
2020use crate :: unsigned_integer:: traits:: IsUnsignedInteger ;
2121
22- use core:: fmt:: { self , Debug , Display } ;
22+ use core:: fmt:: { self , Debug , Display , LowerHex , UpperHex } ;
2323
2424pub type U384 = UnsignedInteger < 6 > ;
2525pub type U256 = UnsignedInteger < 4 > ;
@@ -108,6 +108,28 @@ impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {
108108}
109109
110110impl < const NUM_LIMBS : usize > Display for UnsignedInteger < NUM_LIMBS > {
111+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112+ write ! ( f, "0x" ) ?;
113+
114+ let mut limbs_iterator = self . limbs . iter ( ) . skip_while ( |limb| * * limb == 0 ) . peekable ( ) ;
115+
116+ if limbs_iterator. peek ( ) . is_none ( ) {
117+ write ! ( f, "0" ) ?;
118+ } else {
119+ if let Some ( most_significant_limb) = limbs_iterator. next ( ) {
120+ write ! ( f, "{most_significant_limb:x}" ) ?;
121+ }
122+
123+ for limb in limbs_iterator {
124+ write ! ( f, "{limb:016x}" ) ?;
125+ }
126+ }
127+
128+ Ok ( ( ) )
129+ }
130+ }
131+
132+ impl < const NUM_LIMBS : usize > LowerHex for UnsignedInteger < NUM_LIMBS > {
111133 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112134 if f. alternate ( ) {
113135 write ! ( f, "0x" ) ?;
@@ -131,6 +153,30 @@ impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
131153 }
132154}
133155
156+ impl < const NUM_LIMBS : usize > UpperHex for UnsignedInteger < NUM_LIMBS > {
157+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
158+ if f. alternate ( ) {
159+ write ! ( f, "0X" ) ?;
160+ }
161+
162+ let mut limbs_iterator = self . limbs . iter ( ) . skip_while ( |limb| * * limb == 0 ) . peekable ( ) ;
163+
164+ if limbs_iterator. peek ( ) . is_none ( ) {
165+ write ! ( f, "0" ) ?;
166+ } else {
167+ if let Some ( most_significant_limb) = limbs_iterator. next ( ) {
168+ write ! ( f, "{most_significant_limb:X}" ) ?;
169+ }
170+
171+ for limb in limbs_iterator {
172+ write ! ( f, "{limb:016X}" ) ?;
173+ }
174+ }
175+
176+ Ok ( ( ) )
177+ }
178+ }
179+
134180// impl Add for both references and variables
135181
136182impl < const NUM_LIMBS : usize > Add < & UnsignedInteger < NUM_LIMBS > > for & UnsignedInteger < NUM_LIMBS > {
0 commit comments