@@ -54,21 +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( any( test, feature = "std" ) ) ]
64
- extern crate core;
65
-
66
- #[ cfg( all( not( feature = "std" ) , not( test) ) ) ]
64
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
67
65
use alloc:: { string:: String , vec:: Vec } ;
68
66
69
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
67
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
70
68
use alloc:: borrow:: Cow ;
71
- #[ cfg( any ( feature = "std" , test ) ) ]
69
+ #[ cfg( feature = "std" ) ]
72
70
use std:: borrow:: Cow ;
73
71
74
72
use core:: { fmt, mem} ;
@@ -228,6 +226,7 @@ pub trait FromBase32: Sized {
228
226
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
229
227
}
230
228
229
+ #[ cfg( feature = "alloc" ) ]
231
230
impl WriteBase32 for Vec < u5 > {
232
231
type Err = ( ) ;
233
232
@@ -242,6 +241,7 @@ impl WriteBase32 for Vec<u5> {
242
241
}
243
242
}
244
243
244
+ #[ cfg( feature = "alloc" ) ]
245
245
impl FromBase32 for Vec < u8 > {
246
246
type Err = Error ;
247
247
@@ -253,6 +253,7 @@ impl FromBase32 for Vec<u8> {
253
253
}
254
254
255
255
/// A trait for converting a value to a type `T` that represents a `u5` slice.
256
+ #[ cfg( feature = "alloc" ) ]
256
257
pub trait ToBase32 {
257
258
/// Convert `Self` to base32 vector
258
259
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -267,11 +268,13 @@ pub trait ToBase32 {
267
268
}
268
269
269
270
/// Interface to calculate the length of the base32 representation before actually serializing
271
+ #[ cfg( feature = "alloc" ) ]
270
272
pub trait Base32Len : ToBase32 {
271
273
/// Calculate the base32 serialized length
272
274
fn base32_len ( & self ) -> usize ;
273
275
}
274
276
277
+ #[ cfg( feature = "alloc" ) ]
275
278
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
276
279
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
277
280
// Amount of bits left over from last round, stored in buffer.
@@ -316,6 +319,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
316
319
}
317
320
}
318
321
322
+ #[ cfg( feature = "alloc" ) ]
319
323
impl < T : AsRef < [ u8 ] > > Base32Len for T {
320
324
fn base32_len ( & self ) -> usize {
321
325
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -337,6 +341,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
337
341
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
338
342
}
339
343
344
+ #[ cfg( feature = "alloc" ) ]
340
345
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
341
346
type Err = Error ;
342
347
@@ -349,6 +354,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
349
354
}
350
355
351
356
#[ derive( Clone , Copy , PartialEq , Eq ) ]
357
+ #[ cfg( feature = "alloc" ) ]
352
358
enum Case {
353
359
Upper ,
354
360
Lower ,
@@ -361,6 +367,7 @@ enum Case {
361
367
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
362
368
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
363
369
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
370
+ #[ cfg( feature = "alloc" ) ]
364
371
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
365
372
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
366
373
return Err ( Error :: InvalidLength ) ;
@@ -400,6 +407,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
400
407
/// * If [check_hrp] returns an error for the given HRP.
401
408
/// # Deviations from standard
402
409
/// * No length limits are enforced for the data part
410
+ #[ cfg( feature = "alloc" ) ]
403
411
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
404
412
fmt : & mut fmt:: Write ,
405
413
hrp : & str ,
@@ -436,6 +444,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
436
444
437
445
impl Variant {
438
446
// Produce the variant based on the remainder of the polymod operation
447
+ #[ cfg( feature = "alloc" ) ]
439
448
fn from_remainder ( c : u32 ) -> Option < Self > {
440
449
match c {
441
450
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -458,6 +467,7 @@ impl Variant {
458
467
/// * If [check_hrp] returns an error for the given HRP.
459
468
/// # Deviations from standard
460
469
/// * No length limits are enforced for the data part
470
+ #[ cfg( feature = "alloc" ) ]
461
471
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
462
472
let mut buf = String :: new ( ) ;
463
473
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -467,6 +477,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
467
477
/// Decode a bech32 string into the raw HRP and the data bytes.
468
478
///
469
479
/// Returns the HRP in lowercase..
480
+ #[ cfg( feature = "alloc" ) ]
470
481
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
471
482
// Ensure overall length is within bounds
472
483
if s. len ( ) < 8 {
@@ -541,12 +552,14 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
541
552
}
542
553
}
543
554
555
+ #[ cfg( feature = "alloc" ) ]
544
556
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
545
557
let mut exp = hrp_expand ( hrp) ;
546
558
exp. extend_from_slice ( data) ;
547
559
Variant :: from_remainder ( polymod ( & exp) )
548
560
}
549
561
562
+ #[ cfg( feature = "alloc" ) ]
550
563
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
551
564
let mut v: Vec < u5 > = Vec :: new ( ) ;
552
565
for b in hrp {
@@ -559,6 +572,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
559
572
v
560
573
}
561
574
575
+ #[ cfg( feature = "alloc" ) ]
562
576
fn polymod ( values : & [ u5 ] ) -> u32 {
563
577
let mut chk: u32 = 1 ;
564
578
let mut b: u8 ;
@@ -587,6 +601,7 @@ const CHARSET: [char; 32] = [
587
601
] ;
588
602
589
603
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
604
+ #[ cfg( feature = "alloc" ) ]
590
605
const CHARSET_REV : [ i8 ; 128 ] = [
591
606
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
592
607
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -638,7 +653,7 @@ impl fmt::Display for Error {
638
653
}
639
654
}
640
655
641
- #[ cfg( any ( feature = "std" , test ) ) ]
656
+ #[ cfg( feature = "std" ) ]
642
657
impl std:: error:: Error for Error {
643
658
fn description ( & self ) -> & str {
644
659
match * self {
@@ -670,6 +685,7 @@ impl std::error::Error for Error {
670
685
/// let base5 = convert_bits(&[0xff], 8, 5, true);
671
686
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
672
687
/// ```
688
+ #[ cfg( feature = "alloc" ) ]
673
689
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
674
690
where
675
691
T : Into < u8 > + Copy ,
@@ -705,6 +721,7 @@ where
705
721
}
706
722
707
723
#[ cfg( test) ]
724
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
708
725
mod tests {
709
726
use super :: * ;
710
727
0 commit comments