@@ -398,7 +398,12 @@ impl<'a, I: AsRef<[u8]>> EncodeBuilder<'a, I> {
398
398
/// assert_eq!("he11owor1d\0ld", output);
399
399
/// # Ok::<(), bs58::encode::Error>(())
400
400
/// ```
401
- pub fn onto ( self , mut output : impl EncodeTarget ) -> Result < usize > {
401
+ pub fn onto ( self , output : impl EncodeTarget ) -> Result < usize > {
402
+ self . ref_onto ( output)
403
+ }
404
+
405
+ /// Same as [`Self::onto`] but does not consume `self`.
406
+ fn ref_onto ( & self , mut output : impl EncodeTarget ) -> Result < usize > {
402
407
let input = self . input . as_ref ( ) ;
403
408
match self . check {
404
409
Check :: Disabled => output. encode_with ( max_encoded_len ( input. len ( ) ) , |output| {
@@ -422,11 +427,42 @@ impl<'a, I: AsRef<[u8]>> EncodeBuilder<'a, I> {
422
427
}
423
428
}
424
429
430
+ #[ cfg( feature = "alloc" ) ]
431
+ impl < ' a , I : AsRef < [ u8 ] > > core:: fmt:: Display for EncodeBuilder < ' a , I > {
432
+ fn fmt ( & self , fmt : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
433
+ // If input is short enough, encode it into a buffer on stack to avoid
434
+ // allocation. 96 bytes length limit should be plenty and cover all
435
+ // sane cases; base58 is typically used for things like encryption keys
436
+ // and hashes which are often no more than 32-bytes long.
437
+ const LEN_LIMIT : usize = 96 ;
438
+
439
+ #[ cfg( any( feature = "check" , feature = "cb58" ) ) ]
440
+ const MAX_LEN : usize = LEN_LIMIT + CHECKSUM_LEN + 1 ;
441
+ #[ cfg( not( any( feature = "check" , feature = "cb58" ) ) ) ]
442
+ const MAX_LEN : usize = LEN_LIMIT ;
443
+ let mut buf = [ 0u8 ; max_encoded_len ( MAX_LEN ) ] ;
444
+ let mut vec = Vec :: new ( ) ;
445
+
446
+ let output = if self . input . as_ref ( ) . len ( ) <= LEN_LIMIT {
447
+ let len = self . ref_onto ( & mut buf[ ..] ) . unwrap ( ) ;
448
+ & buf[ ..len]
449
+ } else {
450
+ self . ref_onto ( & mut vec) . unwrap ( ) ;
451
+ vec. as_slice ( )
452
+ } ;
453
+
454
+ // SAFETY: we know that alphabet can only include ASCII characters
455
+ // thus our result is an ASCII string.
456
+ #[ allow( unsafe_code) ]
457
+ fmt. write_str ( unsafe { std:: str:: from_utf8_unchecked ( output) } )
458
+ }
459
+ }
460
+
425
461
/// Return maximum possible encoded length of a buffer with given length.
426
462
///
427
463
/// Assumes that the `len` already includes version and checksum bytes if those
428
- /// are
429
- fn max_encoded_len ( len : usize ) -> usize {
464
+ /// are part of the encoding.
465
+ const fn max_encoded_len ( len : usize ) -> usize {
430
466
// log_2(256) / log_2(58) ≈ 1.37. Assume 1.5 for easier calculation.
431
467
len + ( len + 1 ) / 2
432
468
}
0 commit comments