@@ -54,24 +54,27 @@ 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
64
#[ cfg( any( test, feature = "std" ) ) ]
64
65
extern crate core;
65
66
66
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
67
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
67
68
use alloc:: { string:: String , vec:: Vec } ;
68
69
69
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
70
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
70
71
use alloc:: borrow:: Cow ;
71
- #[ cfg( any ( feature = "std" , test ) ) ]
72
+ #[ cfg( feature = "std" ) ]
72
73
use std:: borrow:: Cow ;
73
74
74
- use core:: { convert:: Infallible , fmt, mem} ;
75
+ use core:: { fmt, mem} ;
76
+ #[ cfg( feature = "alloc" ) ]
77
+ use core:: convert:: Infallible ;
75
78
76
79
/// Integer in the range `0..32`
77
80
#[ derive( PartialEq , Eq , Debug , Copy , Clone , Default , PartialOrd , Ord , Hash ) ]
@@ -231,6 +234,7 @@ pub trait FromBase32: Sized {
231
234
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
232
235
}
233
236
237
+ #[ cfg( feature = "alloc" ) ]
234
238
impl WriteBase32 for Vec < u5 > {
235
239
type Err = Infallible ;
236
240
@@ -245,6 +249,7 @@ impl WriteBase32 for Vec<u5> {
245
249
}
246
250
}
247
251
252
+ #[ cfg( feature = "alloc" ) ]
248
253
impl FromBase32 for Vec < u8 > {
249
254
type Err = Error ;
250
255
@@ -256,6 +261,7 @@ impl FromBase32 for Vec<u8> {
256
261
}
257
262
258
263
/// A trait for converting a value to a type `T` that represents a `u5` slice.
264
+ #[ cfg( feature = "alloc" ) ]
259
265
pub trait ToBase32 {
260
266
/// Convert `Self` to base32 vector
261
267
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -270,11 +276,13 @@ pub trait ToBase32 {
270
276
}
271
277
272
278
/// Interface to calculate the length of the base32 representation before actually serializing
279
+ #[ cfg( feature = "alloc" ) ]
273
280
pub trait Base32Len : ToBase32 {
274
281
/// Calculate the base32 serialized length
275
282
fn base32_len ( & self ) -> usize ;
276
283
}
277
284
285
+ #[ cfg( feature = "alloc" ) ]
278
286
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
279
287
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
280
288
// Amount of bits left over from last round, stored in buffer.
@@ -319,6 +327,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
319
327
}
320
328
}
321
329
330
+ #[ cfg( feature = "alloc" ) ]
322
331
impl < T : AsRef < [ u8 ] > > Base32Len for T {
323
332
fn base32_len ( & self ) -> usize {
324
333
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -340,6 +349,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
340
349
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
341
350
}
342
351
352
+ #[ cfg( feature = "alloc" ) ]
343
353
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
344
354
type Err = Error ;
345
355
@@ -352,6 +362,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
352
362
}
353
363
354
364
#[ derive( Clone , Copy , PartialEq , Eq ) ]
365
+ #[ cfg( feature = "alloc" ) ]
355
366
enum Case {
356
367
Upper ,
357
368
Lower ,
@@ -364,6 +375,7 @@ enum Case {
364
375
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
365
376
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
366
377
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
378
+ #[ cfg( feature = "alloc" ) ]
367
379
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
368
380
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
369
381
return Err ( Error :: InvalidLength ) ;
@@ -403,6 +415,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
403
415
/// * If [check_hrp] returns an error for the given HRP.
404
416
/// # Deviations from standard
405
417
/// * No length limits are enforced for the data part
418
+ #[ cfg( feature = "alloc" ) ]
406
419
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
407
420
fmt : & mut fmt:: Write ,
408
421
hrp : & str ,
@@ -432,6 +445,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
432
445
/// * If [check_hrp] returns an error for the given HRP.
433
446
/// # Deviations from standard
434
447
/// * No length limits are enforced for the data part
448
+ #[ cfg( feature = "alloc" ) ]
435
449
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
436
450
fmt : & mut fmt:: Write ,
437
451
hrp : & str ,
@@ -470,6 +484,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
470
484
471
485
impl Variant {
472
486
// Produce the variant based on the remainder of the polymod operation
487
+ #[ cfg( feature = "alloc" ) ]
473
488
fn from_remainder ( c : u32 ) -> Option < Self > {
474
489
match c {
475
490
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -492,6 +507,7 @@ impl Variant {
492
507
/// * If [check_hrp] returns an error for the given HRP.
493
508
/// # Deviations from standard
494
509
/// * No length limits are enforced for the data part
510
+ #[ cfg( feature = "alloc" ) ]
495
511
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
496
512
let mut buf = String :: new ( ) ;
497
513
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -504,6 +520,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
504
520
/// * If [check_hrp] returns an error for the given HRP.
505
521
/// # Deviations from standard
506
522
/// * No length limits are enforced for the data part
523
+ #[ cfg( feature = "alloc" ) ]
507
524
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
508
525
let mut buf = String :: new ( ) ;
509
526
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?. unwrap ( ) ;
@@ -513,6 +530,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
513
530
/// Decode a bech32 string into the raw HRP and the data bytes.
514
531
///
515
532
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
533
+ #[ cfg( feature = "alloc" ) ]
516
534
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
517
535
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
518
536
if data. len ( ) < CHECKSUM_LENGTH {
@@ -534,11 +552,13 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
534
552
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
535
553
///
536
554
/// Returns the HRP in lowercase and the data.
555
+ #[ cfg( feature = "alloc" ) ]
537
556
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
538
557
split_and_decode ( s)
539
558
}
540
559
541
560
/// Decode a bech32 string into the raw HRP and the `u5` data.
561
+ #[ cfg( feature = "alloc" ) ]
542
562
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
543
563
// Split at separator and check for two pieces
544
564
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -595,12 +615,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
595
615
Ok ( ( hrp_lower, data) )
596
616
}
597
617
618
+ #[ cfg( feature = "alloc" ) ]
598
619
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
599
620
let mut exp = hrp_expand ( hrp) ;
600
621
exp. extend_from_slice ( data) ;
601
622
Variant :: from_remainder ( polymod ( & exp) )
602
623
}
603
624
625
+ #[ cfg( feature = "alloc" ) ]
604
626
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
605
627
let mut v: Vec < u5 > = Vec :: new ( ) ;
606
628
for b in hrp {
@@ -613,6 +635,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
613
635
v
614
636
}
615
637
638
+ #[ cfg( feature = "alloc" ) ]
616
639
fn polymod ( values : & [ u5 ] ) -> u32 {
617
640
let mut chk: u32 = 1 ;
618
641
let mut b: u8 ;
@@ -641,6 +664,7 @@ const CHARSET: [char; 32] = [
641
664
] ;
642
665
643
666
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
667
+ #[ cfg( feature = "alloc" ) ]
644
668
const CHARSET_REV : [ i8 ; 128 ] = [
645
669
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
646
670
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -721,6 +745,7 @@ impl std::error::Error for Error {
721
745
/// let base5 = convert_bits(&[0xff], 8, 5, true);
722
746
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
723
747
/// ```
748
+ #[ cfg( feature = "alloc" ) ]
724
749
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
725
750
where
726
751
T : Into < u8 > + Copy ,
@@ -756,6 +781,7 @@ where
756
781
}
757
782
758
783
#[ cfg( test) ]
784
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
759
785
mod tests {
760
786
use super :: * ;
761
787
0 commit comments