@@ -54,21 +54,24 @@ 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
- use core:: { convert:: Infallible , fmt, mem} ;
72
+ use core:: { fmt, mem} ;
73
+ #[ cfg( feature = "alloc" ) ]
74
+ use core:: convert:: Infallible ;
72
75
73
76
/// Integer in the range `0..32`
74
77
#[ derive( PartialEq , Eq , Debug , Copy , Clone , Default , PartialOrd , Ord , Hash ) ]
@@ -228,6 +231,7 @@ pub trait FromBase32: Sized {
228
231
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
229
232
}
230
233
234
+ #[ cfg( feature = "alloc" ) ]
231
235
impl WriteBase32 for Vec < u5 > {
232
236
type Err = Infallible ;
233
237
@@ -242,6 +246,7 @@ impl WriteBase32 for Vec<u5> {
242
246
}
243
247
}
244
248
249
+ #[ cfg( feature = "alloc" ) ]
245
250
impl FromBase32 for Vec < u8 > {
246
251
type Err = Error ;
247
252
@@ -253,6 +258,7 @@ impl FromBase32 for Vec<u8> {
253
258
}
254
259
255
260
/// A trait for converting a value to a type `T` that represents a `u5` slice.
261
+ #[ cfg( feature = "alloc" ) ]
256
262
pub trait ToBase32 {
257
263
/// Convert `Self` to base32 vector
258
264
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -267,11 +273,13 @@ pub trait ToBase32 {
267
273
}
268
274
269
275
/// Interface to calculate the length of the base32 representation before actually serializing
276
+ #[ cfg( feature = "alloc" ) ]
270
277
pub trait Base32Len : ToBase32 {
271
278
/// Calculate the base32 serialized length
272
279
fn base32_len ( & self ) -> usize ;
273
280
}
274
281
282
+ #[ cfg( feature = "alloc" ) ]
275
283
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
276
284
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
277
285
// Amount of bits left over from last round, stored in buffer.
@@ -316,6 +324,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
316
324
}
317
325
}
318
326
327
+ #[ cfg( feature = "alloc" ) ]
319
328
impl < T : AsRef < [ u8 ] > > Base32Len for T {
320
329
fn base32_len ( & self ) -> usize {
321
330
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -337,6 +346,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
337
346
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
338
347
}
339
348
349
+ #[ cfg( feature = "alloc" ) ]
340
350
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
341
351
type Err = Error ;
342
352
@@ -349,6 +359,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
349
359
}
350
360
351
361
#[ derive( Clone , Copy , PartialEq , Eq ) ]
362
+ #[ cfg( feature = "alloc" ) ]
352
363
enum Case {
353
364
Upper ,
354
365
Lower ,
@@ -361,6 +372,7 @@ enum Case {
361
372
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
362
373
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
363
374
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
375
+ #[ cfg( feature = "alloc" ) ]
364
376
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
365
377
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
366
378
return Err ( Error :: InvalidLength ) ;
@@ -400,6 +412,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
400
412
/// * If [check_hrp] returns an error for the given HRP.
401
413
/// # Deviations from standard
402
414
/// * No length limits are enforced for the data part
415
+ #[ cfg( feature = "alloc" ) ]
403
416
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
404
417
fmt : & mut fmt:: Write ,
405
418
hrp : & str ,
@@ -429,6 +442,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
429
442
/// * If [check_hrp] returns an error for the given HRP.
430
443
/// # Deviations from standard
431
444
/// * No length limits are enforced for the data part
445
+ #[ cfg( feature = "alloc" ) ]
432
446
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
433
447
fmt : & mut fmt:: Write ,
434
448
hrp : & str ,
@@ -467,6 +481,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
467
481
468
482
impl Variant {
469
483
// Produce the variant based on the remainder of the polymod operation
484
+ #[ cfg( feature = "alloc" ) ]
470
485
fn from_remainder ( c : u32 ) -> Option < Self > {
471
486
match c {
472
487
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -489,6 +504,7 @@ impl Variant {
489
504
/// * If [check_hrp] returns an error for the given HRP.
490
505
/// # Deviations from standard
491
506
/// * No length limits are enforced for the data part
507
+ #[ cfg( feature = "alloc" ) ]
492
508
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
493
509
let mut buf = String :: new ( ) ;
494
510
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -501,6 +517,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
501
517
/// * If [check_hrp] returns an error for the given HRP.
502
518
/// # Deviations from standard
503
519
/// * No length limits are enforced for the data part
520
+ #[ cfg( feature = "alloc" ) ]
504
521
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
505
522
let mut buf = String :: new ( ) ;
506
523
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?. unwrap ( ) ;
@@ -510,6 +527,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
510
527
/// Decode a bech32 string into the raw HRP and the data bytes.
511
528
///
512
529
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
530
+ #[ cfg( feature = "alloc" ) ]
513
531
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
514
532
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
515
533
if data. len ( ) < CHECKSUM_LENGTH {
@@ -531,11 +549,13 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
531
549
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
532
550
///
533
551
/// Returns the HRP in lowercase and the data.
552
+ #[ cfg( feature = "alloc" ) ]
534
553
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
535
554
split_and_decode ( s)
536
555
}
537
556
538
557
/// Decode a bech32 string into the raw HRP and the `u5` data.
558
+ #[ cfg( feature = "alloc" ) ]
539
559
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
540
560
// Split at separator and check for two pieces
541
561
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -592,12 +612,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
592
612
Ok ( ( hrp_lower, data) )
593
613
}
594
614
615
+ #[ cfg( feature = "alloc" ) ]
595
616
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
596
617
let mut exp = hrp_expand ( hrp) ;
597
618
exp. extend_from_slice ( data) ;
598
619
Variant :: from_remainder ( polymod ( & exp) )
599
620
}
600
621
622
+ #[ cfg( feature = "alloc" ) ]
601
623
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
602
624
let mut v: Vec < u5 > = Vec :: new ( ) ;
603
625
for b in hrp {
@@ -610,6 +632,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
610
632
v
611
633
}
612
634
635
+ #[ cfg( feature = "alloc" ) ]
613
636
fn polymod ( values : & [ u5 ] ) -> u32 {
614
637
let mut chk: u32 = 1 ;
615
638
let mut b: u8 ;
@@ -638,6 +661,7 @@ const CHARSET: [char; 32] = [
638
661
] ;
639
662
640
663
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
664
+ #[ cfg( feature = "alloc" ) ]
641
665
const CHARSET_REV : [ i8 ; 128 ] = [
642
666
-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
667
-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 +742,7 @@ impl std::error::Error for Error {
718
742
/// let base5 = convert_bits(&[0xff], 8, 5, true);
719
743
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
720
744
/// ```
745
+ #[ cfg( feature = "alloc" ) ]
721
746
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
722
747
where
723
748
T : Into < u8 > + Copy ,
@@ -753,6 +778,7 @@ where
753
778
}
754
779
755
780
#[ cfg( test) ]
781
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
756
782
mod tests {
757
783
use super :: * ;
758
784
0 commit comments