@@ -44,16 +44,16 @@ const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF";
44
44
/// ```
45
45
pub trait Hexify {
46
46
/// Hexify `Self`.
47
- fn hexify ( self ) -> String ;
47
+ fn hexify ( & self ) -> String ;
48
48
49
49
/// Hexify `Self` with uppercase.
50
- fn hexify_upper ( self ) -> String ;
50
+ fn hexify_upper ( & self ) -> String ;
51
51
52
52
/// Hexify `Self` with `0x` prefix.
53
- fn hexify_prefixed ( self ) -> String ;
53
+ fn hexify_prefixed ( & self ) -> String ;
54
54
55
55
/// Hexify `Self` with `0x` prefix and uppercase.
56
- fn hexify_prefixed_upper ( self ) -> String ;
56
+ fn hexify_prefixed_upper ( & self ) -> String ;
57
57
}
58
58
macro_rules! hexify_unsigned {
59
59
( $self: expr, $map: expr) => { {
@@ -103,40 +103,22 @@ macro_rules! impl_hexify_for_unsigned {
103
103
( $( $t: ty, ) +) => {
104
104
$(
105
105
impl Hexify for $t {
106
- fn hexify( self ) -> String {
106
+ fn hexify( & self ) -> String {
107
107
hexify_unsigned!( self , HEX_CHARS )
108
108
}
109
109
110
- fn hexify_upper( self ) -> String {
110
+ fn hexify_upper( & self ) -> String {
111
111
hexify_unsigned!( self , HEX_CHARS_UPPER )
112
112
}
113
113
114
- fn hexify_prefixed( self ) -> String {
114
+ fn hexify_prefixed( & self ) -> String {
115
115
hexify_unsigned_prefixed!( self , HEX_CHARS )
116
116
}
117
117
118
- fn hexify_prefixed_upper( self ) -> String {
118
+ fn hexify_prefixed_upper( & self ) -> String {
119
119
hexify_unsigned_prefixed!( self , HEX_CHARS_UPPER )
120
120
}
121
121
}
122
-
123
- impl Hexify for & $t {
124
- fn hexify( self ) -> String {
125
- ( * self ) . hexify( )
126
- }
127
-
128
- fn hexify_upper( self ) -> String {
129
- ( * self ) . hexify_upper( )
130
- }
131
-
132
- fn hexify_prefixed( self ) -> String {
133
- ( * self ) . hexify_prefixed( )
134
- }
135
-
136
- fn hexify_prefixed_upper( self ) -> String {
137
- ( * self ) . hexify_prefixed_upper( )
138
- }
139
- }
140
122
) +
141
123
} ;
142
124
}
@@ -202,90 +184,33 @@ macro_rules! hexify_prefixed {
202
184
unsafe { String :: from_utf8_unchecked( hex_bytes. into_vec( ) ) }
203
185
} } ;
204
186
}
205
- impl < const N : usize > Hexify for [ u8 ; N ] {
206
- fn hexify ( self ) -> String {
207
- hexify ! ( self , HEX_CHARS )
208
- }
187
+ macro_rules! hexify_bytes_fns {
188
+ ( ) => {
189
+ fn hexify( & self ) -> String {
190
+ hexify!( self , HEX_CHARS )
191
+ }
209
192
210
- fn hexify_upper ( self ) -> String {
211
- hexify ! ( self , HEX_CHARS_UPPER )
212
- }
193
+ fn hexify_upper( & self ) -> String {
194
+ hexify!( self , HEX_CHARS_UPPER )
195
+ }
213
196
214
- fn hexify_prefixed ( self ) -> String {
215
- hexify_prefixed ! ( self , HEX_CHARS )
216
- }
197
+ fn hexify_prefixed( & self ) -> String {
198
+ hexify_prefixed!( self , HEX_CHARS )
199
+ }
217
200
218
- fn hexify_prefixed_upper ( self ) -> String {
219
- hexify_prefixed ! ( self , HEX_CHARS_UPPER )
220
- }
201
+ fn hexify_prefixed_upper( & self ) -> String {
202
+ hexify_prefixed!( self , HEX_CHARS_UPPER )
203
+ }
204
+ } ;
221
205
}
222
- impl < const N : usize > Hexify for & [ u8 ; N ] {
223
- fn hexify ( self ) -> String {
224
- hexify ! ( self , HEX_CHARS )
225
- }
226
-
227
- fn hexify_upper ( self ) -> String {
228
- hexify ! ( self , HEX_CHARS_UPPER )
229
- }
230
-
231
- fn hexify_prefixed ( self ) -> String {
232
- hexify_prefixed ! ( self , HEX_CHARS )
233
- }
234
-
235
- fn hexify_prefixed_upper ( self ) -> String {
236
- hexify_prefixed ! ( self , HEX_CHARS_UPPER )
237
- }
206
+ impl < const N : usize > Hexify for [ u8 ; N ] {
207
+ hexify_bytes_fns ! { }
238
208
}
239
- impl Hexify for & [ u8 ] {
240
- fn hexify ( self ) -> String {
241
- hexify ! ( self , HEX_CHARS )
242
- }
243
-
244
- fn hexify_upper ( self ) -> String {
245
- hexify ! ( self , HEX_CHARS_UPPER )
246
- }
247
-
248
- fn hexify_prefixed ( self ) -> String {
249
- hexify_prefixed ! ( self , HEX_CHARS )
250
- }
251
-
252
- fn hexify_prefixed_upper ( self ) -> String {
253
- hexify_prefixed ! ( self , HEX_CHARS_UPPER )
254
- }
209
+ impl Hexify for [ u8 ] {
210
+ hexify_bytes_fns ! { }
255
211
}
256
212
impl Hexify for Vec < u8 > {
257
- fn hexify ( self ) -> String {
258
- hexify ! ( self , HEX_CHARS )
259
- }
260
-
261
- fn hexify_upper ( self ) -> String {
262
- hexify ! ( self , HEX_CHARS_UPPER )
263
- }
264
-
265
- fn hexify_prefixed ( self ) -> String {
266
- hexify_prefixed ! ( self , HEX_CHARS )
267
- }
268
-
269
- fn hexify_prefixed_upper ( self ) -> String {
270
- hexify_prefixed ! ( self , HEX_CHARS_UPPER )
271
- }
272
- }
273
- impl Hexify for & Vec < u8 > {
274
- fn hexify ( self ) -> String {
275
- hexify ! ( self , HEX_CHARS )
276
- }
277
-
278
- fn hexify_upper ( self ) -> String {
279
- hexify ! ( self , HEX_CHARS_UPPER )
280
- }
281
-
282
- fn hexify_prefixed ( self ) -> String {
283
- hexify_prefixed ! ( self , HEX_CHARS )
284
- }
285
-
286
- fn hexify_prefixed_upper ( self ) -> String {
287
- hexify_prefixed ! ( self , HEX_CHARS_UPPER )
288
- }
213
+ hexify_bytes_fns ! { }
289
214
}
290
215
#[ test]
291
216
fn hexify_should_work ( ) {
0 commit comments