@@ -29,7 +29,7 @@ pub(crate) trait Base32Len: Base32Iterable {
29
29
30
30
impl < const N : usize > Base32Iterable for [ u8 ; N ] {
31
31
fn fe_iter < ' s > ( & ' s self ) -> Box < dyn Iterator < Item = Fe32 > + ' s > {
32
- Box :: new ( ( * self ) . into_iter ( ) . bytes_to_fes ( ) )
32
+ self [ .. ] . fe_iter ( )
33
33
}
34
34
}
35
35
@@ -218,19 +218,20 @@ impl Display for SiPrefix {
218
218
}
219
219
220
220
/// Encode an integer to base32, big endian, without leading zeros
221
- fn encode_int_be_base32 ( int : u64 ) -> Vec < Fe32 > {
221
+ fn encode_int_be_base32 ( int : u64 ) -> impl ExactSizeIterator < Item = Fe32 > {
222
222
let base = 32u64 ;
223
223
224
224
// (64 + 4) / 5 == 13
225
- let mut out_vec = Vec :: < Fe32 > :: with_capacity ( 13 ) ;
225
+ let mut out = [ Fe32 :: Q ; 13 ] ;
226
+ let mut out_pos = 0 ;
226
227
let mut rem_int = int;
227
228
while rem_int != 0 {
228
- out_vec. push ( Fe32 :: try_from ( ( rem_int % base) as u8 ) . expect ( "always <32" ) ) ;
229
+ out[ out_pos] = Fe32 :: try_from ( ( rem_int % base) as u8 ) . expect ( "always <32" ) ;
230
+ out_pos += 1 ;
229
231
rem_int /= base;
230
232
}
231
233
232
- out_vec. reverse ( ) ;
233
- out_vec
234
+ out. into_iter ( ) . take ( out_pos) . rev ( )
234
235
}
235
236
236
237
/// The length of the output of `encode_int_be_base32`.
@@ -252,7 +253,7 @@ impl Base32Iterable for PositiveTimestamp {
252
253
let fes = encode_int_be_base32 ( self . as_unix_timestamp ( ) ) ;
253
254
debug_assert ! ( fes. len( ) <= 7 , "Invalid timestamp length" ) ;
254
255
let to_pad = 7 - fes. len ( ) ;
255
- Box :: new ( core:: iter:: repeat ( Fe32 :: Q ) . take ( to_pad) . chain ( fes. into_iter ( ) ) )
256
+ Box :: new ( core:: iter:: repeat ( Fe32 :: Q ) . take ( to_pad) . chain ( fes) )
256
257
}
257
258
}
258
259
@@ -305,7 +306,7 @@ impl Base32Len for PayeePubKey {
305
306
306
307
impl Base32Iterable for ExpiryTime {
307
308
fn fe_iter < ' s > ( & ' s self ) -> Box < dyn Iterator < Item = Fe32 > + ' s > {
308
- Box :: new ( encode_int_be_base32 ( self . as_seconds ( ) ) . into_iter ( ) )
309
+ Box :: new ( encode_int_be_base32 ( self . as_seconds ( ) ) )
309
310
}
310
311
}
311
312
@@ -317,7 +318,7 @@ impl Base32Len for ExpiryTime {
317
318
318
319
impl Base32Iterable for MinFinalCltvExpiryDelta {
319
320
fn fe_iter < ' s > ( & ' s self ) -> Box < dyn Iterator < Item = Fe32 > + ' s > {
320
- Box :: new ( encode_int_be_base32 ( self . 0 ) . into_iter ( ) )
321
+ Box :: new ( encode_int_be_base32 ( self . 0 ) )
321
322
}
322
323
}
323
324
@@ -504,6 +505,6 @@ mod test {
504
505
. map ( |v| Fe32 :: try_from ( v) . expect ( "<= 31" ) )
505
506
. collect :: < Vec < Fe32 > > ( ) ;
506
507
507
- assert_eq ! ( expected_out, encode_int_be_base32( input) ) ;
508
+ assert_eq ! ( expected_out, encode_int_be_base32( input) . collect :: < Vec < Fe32 >> ( ) ) ;
508
509
}
509
510
}
0 commit comments