@@ -44,9 +44,9 @@ pub struct HexDisplay<'a> {
44
44
impl std:: fmt:: Display for HexDisplay < ' _ > {
45
45
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
46
46
let mut hex = Kind :: hex_buf ( ) ;
47
- let max_len = self . inner . hex_to_buf ( hex. as_mut ( ) ) ;
48
- let hex = std :: str :: from_utf8 ( & hex[ .. self . hex_len . min ( max_len ) ] ) . expect ( "ascii only in hex" ) ;
49
- f. write_str ( hex)
47
+ let hex = self . inner . hex_to_buf ( hex. as_mut ( ) ) ;
48
+ let max_len = hex. len ( ) ;
49
+ f. write_str ( & hex[ .. self . hex_len . min ( max_len ) ] )
50
50
}
51
51
}
52
52
@@ -152,22 +152,21 @@ impl oid {
152
152
153
153
/// Sha1 specific methods
154
154
impl oid {
155
- /// Write ourselves to the `out` in hexadecimal notation, returning the amount of written bytes .
155
+ /// Write ourselves to the `out` in hexadecimal notation, returning the hex-string ready for display .
156
156
///
157
157
/// **Panics** if the buffer isn't big enough to hold twice as many bytes as the current binary size.
158
158
#[ inline]
159
159
#[ must_use]
160
- pub fn hex_to_buf ( & self , buf : & mut [ u8 ] ) -> usize {
160
+ pub fn hex_to_buf < ' a > ( & self , buf : & ' a mut [ u8 ] ) -> & ' a mut str {
161
161
let num_hex_bytes = self . bytes . len ( ) * 2 ;
162
- faster_hex:: hex_encode ( & self . bytes , & mut buf[ ..num_hex_bytes] ) . expect ( "to count correctly" ) ;
163
- num_hex_bytes
162
+ faster_hex:: hex_encode ( & self . bytes , & mut buf[ ..num_hex_bytes] ) . expect ( "to count correctly" )
164
163
}
165
164
166
165
/// Write ourselves to `out` in hexadecimal notation.
167
166
#[ inline]
168
167
pub fn write_hex_to ( & self , out : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
169
168
let mut hex = Kind :: hex_buf ( ) ;
170
- let hex_len = self . hex_to_buf ( & mut hex) ;
169
+ let hex_len = self . hex_to_buf ( & mut hex) . len ( ) ;
171
170
out. write_all ( & hex[ ..hex_len] )
172
171
}
173
172
@@ -210,10 +209,8 @@ impl<'a> From<&'a [u8; SIZE_OF_SHA1_DIGEST]> for &'a oid {
210
209
211
210
impl std:: fmt:: Display for & oid {
212
211
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
213
- for b in self . as_bytes ( ) {
214
- write ! ( f, "{b:02x}" ) ?;
215
- }
216
- Ok ( ( ) )
212
+ let mut buf = Kind :: hex_buf ( ) ;
213
+ f. write_str ( self . hex_to_buf ( & mut buf) )
217
214
}
218
215
}
219
216
0 commit comments