@@ -54,18 +54,19 @@ assert_eq!(variant, Variant::Bech32);
54
54
#![ deny( non_camel_case_types) ]
55
55
#![ deny( non_snake_case) ]
56
56
#![ deny( unused_mut) ]
57
+
57
58
#![ cfg_attr( feature = "strict" , deny( warnings) ) ]
58
59
#![ cfg_attr( all( not( feature = "std" ) , not( test) ) , no_std) ]
59
60
60
- #[ cfg( all ( not ( feature = "std" ) , not ( test ) ) ) ]
61
+ #[ cfg( feature = "alloc" ) ]
61
62
extern crate alloc;
62
63
63
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
64
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
64
65
use alloc:: { string:: String , vec:: Vec } ;
65
66
66
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
67
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
67
68
use alloc:: borrow:: Cow ;
68
- #[ cfg( any ( feature = "std" , test ) ) ]
69
+ #[ cfg( feature = "std" ) ]
69
70
use std:: borrow:: Cow ;
70
71
71
72
use core:: { fmt, mem} ;
@@ -228,6 +229,7 @@ pub trait FromBase32: Sized {
228
229
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
229
230
}
230
231
232
+ #[ cfg( feature = "alloc" ) ]
231
233
impl WriteBase32 for Vec < u5 > {
232
234
type Err = ( ) ;
233
235
@@ -242,6 +244,7 @@ impl WriteBase32 for Vec<u5> {
242
244
}
243
245
}
244
246
247
+ #[ cfg( feature = "alloc" ) ]
245
248
impl FromBase32 for Vec < u8 > {
246
249
type Err = Error ;
247
250
@@ -253,6 +256,7 @@ impl FromBase32 for Vec<u8> {
253
256
}
254
257
255
258
/// A trait for converting a value to a type `T` that represents a `u5` slice.
259
+ #[ cfg( feature = "alloc" ) ]
256
260
pub trait ToBase32 {
257
261
/// Convert `Self` to base32 vector
258
262
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -267,11 +271,13 @@ pub trait ToBase32 {
267
271
}
268
272
269
273
/// Interface to calculate the length of the base32 representation before actually serializing
274
+ #[ cfg( feature = "alloc" ) ]
270
275
pub trait Base32Len : ToBase32 {
271
276
/// Calculate the base32 serialized length
272
277
fn base32_len ( & self ) -> usize ;
273
278
}
274
279
280
+ #[ cfg( feature = "alloc" ) ]
275
281
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
276
282
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
277
283
// Amount of bits left over from last round, stored in buffer.
@@ -316,6 +322,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
316
322
}
317
323
}
318
324
325
+ #[ cfg( feature = "alloc" ) ]
319
326
impl < T : AsRef < [ u8 ] > > Base32Len for T {
320
327
fn base32_len ( & self ) -> usize {
321
328
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -337,6 +344,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
337
344
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
338
345
}
339
346
347
+ #[ cfg( feature = "alloc" ) ]
340
348
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
341
349
type Err = Error ;
342
350
@@ -349,6 +357,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
349
357
}
350
358
351
359
#[ derive( Clone , Copy , PartialEq , Eq ) ]
360
+ #[ cfg( feature = "alloc" ) ]
352
361
enum Case {
353
362
Upper ,
354
363
Lower ,
@@ -361,6 +370,7 @@ enum Case {
361
370
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
362
371
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
363
372
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
373
+ #[ cfg( feature = "alloc" ) ]
364
374
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
365
375
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
366
376
return Err ( Error :: InvalidLength ) ;
@@ -401,6 +411,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
401
411
/// * If `fmt` fails on write
402
412
/// # Deviations from standard
403
413
/// * No length limits are enforced for the data part
414
+ #[ cfg( feature = "alloc" ) ]
404
415
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
405
416
fmt : & mut fmt:: Write ,
406
417
hrp : & str ,
@@ -427,6 +438,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
427
438
/// * If `fmt` fails on write
428
439
/// # Deviations from standard
429
440
/// * No length limits are enforced for the data part
441
+ #[ cfg( feature = "alloc" ) ]
430
442
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
431
443
fmt : & mut fmt:: Write ,
432
444
hrp : & str ,
@@ -459,6 +471,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
459
471
460
472
impl Variant {
461
473
// Produce the variant based on the remainder of the polymod operation
474
+ #[ cfg( feature = "alloc" ) ]
462
475
fn from_remainder ( c : u32 ) -> Option < Self > {
463
476
match c {
464
477
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -481,6 +494,7 @@ impl Variant {
481
494
/// * If [check_hrp] returns an error for the given HRP.
482
495
/// # Deviations from standard
483
496
/// * No length limits are enforced for the data part
497
+ #[ cfg( feature = "alloc" ) ]
484
498
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
485
499
let mut buf = String :: new ( ) ;
486
500
encode_to_fmt ( & mut buf, hrp, data, variant) ?;
@@ -493,6 +507,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
493
507
/// * If [check_hrp] returns an error for the given HRP.
494
508
/// # Deviations from standard
495
509
/// * No length limits are enforced for the data part
510
+ #[ cfg( feature = "alloc" ) ]
496
511
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
497
512
let mut buf = String :: new ( ) ;
498
513
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?;
@@ -502,6 +517,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
502
517
/// Decode a bech32 string into the raw HRP and the data bytes.
503
518
///
504
519
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
520
+ #[ cfg( feature = "alloc" ) ]
505
521
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
506
522
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
507
523
if data. len ( ) < CHECKSUM_LENGTH {
@@ -523,11 +539,13 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
523
539
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
524
540
///
525
541
/// Returns the HRP in lowercase and the data.
542
+ #[ cfg( feature = "alloc" ) ]
526
543
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
527
544
split_and_decode ( s)
528
545
}
529
546
530
547
/// Decode a bech32 string into the raw HRP and the `u5` data.
548
+ #[ cfg( feature = "alloc" ) ]
531
549
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
532
550
// Split at separator and check for two pieces
533
551
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -584,12 +602,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
584
602
Ok ( ( hrp_lower, data) )
585
603
}
586
604
605
+ #[ cfg( feature = "alloc" ) ]
587
606
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
588
607
let mut exp = hrp_expand ( hrp) ;
589
608
exp. extend_from_slice ( data) ;
590
609
Variant :: from_remainder ( polymod ( & exp) )
591
610
}
592
611
612
+ #[ cfg( feature = "alloc" ) ]
593
613
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
594
614
let mut v: Vec < u5 > = Vec :: new ( ) ;
595
615
for b in hrp {
@@ -602,6 +622,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
602
622
v
603
623
}
604
624
625
+ #[ cfg( feature = "alloc" ) ]
605
626
fn polymod ( values : & [ u5 ] ) -> u32 {
606
627
let mut chk: u32 = 1 ;
607
628
let mut b: u8 ;
@@ -630,6 +651,7 @@ const CHARSET: [char; 32] = [
630
651
] ;
631
652
632
653
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
654
+ #[ cfg( feature = "alloc" ) ]
633
655
const CHARSET_REV : [ i8 ; 128 ] = [
634
656
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
635
657
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -690,7 +712,7 @@ impl fmt::Display for Error {
690
712
}
691
713
}
692
714
693
- #[ cfg( any ( feature = "std" , test ) ) ]
715
+ #[ cfg( feature = "std" ) ]
694
716
impl std:: error:: Error for Error {
695
717
fn description ( & self ) -> & str {
696
718
match * self {
@@ -723,6 +745,7 @@ impl std::error::Error for Error {
723
745
/// let base5 = convert_bits(&[0xff], 8, 5, true);
724
746
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
725
747
/// ```
748
+ #[ cfg( feature = "alloc" ) ]
726
749
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
727
750
where
728
751
T : Into < u8 > + Copy ,
@@ -758,6 +781,7 @@ where
758
781
}
759
782
760
783
#[ cfg( test) ]
784
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
761
785
mod tests {
762
786
use super :: * ;
763
787
0 commit comments