@@ -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 ) ;
@@ -400,6 +410,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
400
410
/// * If [check_hrp] returns an error for the given HRP.
401
411
/// # Deviations from standard
402
412
/// * No length limits are enforced for the data part
413
+ #[ cfg( feature = "alloc" ) ]
403
414
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
404
415
fmt : & mut fmt:: Write ,
405
416
hrp : & str ,
@@ -429,6 +440,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
429
440
/// * If [check_hrp] returns an error for the given HRP.
430
441
/// # Deviations from standard
431
442
/// * No length limits are enforced for the data part
443
+ #[ cfg( feature = "alloc" ) ]
432
444
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
433
445
fmt : & mut fmt:: Write ,
434
446
hrp : & str ,
@@ -467,6 +479,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
467
479
468
480
impl Variant {
469
481
// Produce the variant based on the remainder of the polymod operation
482
+ #[ cfg( feature = "alloc" ) ]
470
483
fn from_remainder ( c : u32 ) -> Option < Self > {
471
484
match c {
472
485
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -489,6 +502,7 @@ impl Variant {
489
502
/// * If [check_hrp] returns an error for the given HRP.
490
503
/// # Deviations from standard
491
504
/// * No length limits are enforced for the data part
505
+ #[ cfg( feature = "alloc" ) ]
492
506
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
493
507
let mut buf = String :: new ( ) ;
494
508
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -501,6 +515,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
501
515
/// * If [check_hrp] returns an error for the given HRP.
502
516
/// # Deviations from standard
503
517
/// * No length limits are enforced for the data part
518
+ #[ cfg( feature = "alloc" ) ]
504
519
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
505
520
let mut buf = String :: new ( ) ;
506
521
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?. unwrap ( ) ;
@@ -510,6 +525,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
510
525
/// Decode a bech32 string into the raw HRP and the data bytes.
511
526
///
512
527
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
528
+ #[ cfg( feature = "alloc" ) ]
513
529
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
514
530
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
515
531
if data. len ( ) < CHECKSUM_LENGTH {
@@ -531,11 +547,13 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
531
547
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
532
548
///
533
549
/// Returns the HRP in lowercase and the data.
550
+ #[ cfg( feature = "alloc" ) ]
534
551
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
535
552
split_and_decode ( s)
536
553
}
537
554
538
555
/// Decode a bech32 string into the raw HRP and the `u5` data.
556
+ #[ cfg( feature = "alloc" ) ]
539
557
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
540
558
// Split at separator and check for two pieces
541
559
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -592,12 +610,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
592
610
Ok ( ( hrp_lower, data) )
593
611
}
594
612
613
+ #[ cfg( feature = "alloc" ) ]
595
614
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
596
615
let mut exp = hrp_expand ( hrp) ;
597
616
exp. extend_from_slice ( data) ;
598
617
Variant :: from_remainder ( polymod ( & exp) )
599
618
}
600
619
620
+ #[ cfg( feature = "alloc" ) ]
601
621
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
602
622
let mut v: Vec < u5 > = Vec :: new ( ) ;
603
623
for b in hrp {
@@ -610,6 +630,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
610
630
v
611
631
}
612
632
633
+ #[ cfg( feature = "alloc" ) ]
613
634
fn polymod ( values : & [ u5 ] ) -> u32 {
614
635
let mut chk: u32 = 1 ;
615
636
let mut b: u8 ;
@@ -638,6 +659,7 @@ const CHARSET: [char; 32] = [
638
659
] ;
639
660
640
661
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
662
+ #[ cfg( feature = "alloc" ) ]
641
663
const CHARSET_REV : [ i8 ; 128 ] = [
642
664
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
643
665
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -718,6 +740,7 @@ impl std::error::Error for Error {
718
740
/// let base5 = convert_bits(&[0xff], 8, 5, true);
719
741
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
720
742
/// ```
743
+ #[ cfg( feature = "alloc" ) ]
721
744
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
722
745
where
723
746
T : Into < u8 > + Copy ,
@@ -753,6 +776,7 @@ where
753
776
}
754
777
755
778
#[ cfg( test) ]
779
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
756
780
mod tests {
757
781
use super :: * ;
758
782
0 commit comments