@@ -57,19 +57,20 @@ assert_eq!(variant, Variant::Bech32);
57
57
#![ cfg_attr( feature = "strict" , deny( warnings) ) ]
58
58
#![ cfg_attr( all( not( feature = "std" ) , not( test) ) , no_std) ]
59
59
60
- #[ cfg( all ( not ( feature = "std" ) , not ( test ) ) ) ]
60
+ #[ cfg( feature = "alloc" ) ]
61
61
extern crate alloc;
62
62
63
63
#[ cfg( any( test, feature = "std" ) ) ]
64
64
extern crate core;
65
65
66
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
66
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
67
67
use alloc:: borrow:: Cow ;
68
- #[ cfg( all( not ( feature = "std" ) , not( test ) ) ) ]
68
+ #[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
69
69
use alloc:: { string:: String , vec:: Vec } ;
70
+ #[ cfg( feature = "alloc" ) ]
70
71
use core:: convert:: Infallible ;
71
72
use core:: { fmt, mem} ;
72
- #[ cfg( any ( feature = "std" , test ) ) ]
73
+ #[ cfg( feature = "std" ) ]
73
74
use std:: borrow:: Cow ;
74
75
75
76
/// Integer in the range `0..32`
@@ -216,6 +217,7 @@ pub trait FromBase32: Sized {
216
217
fn from_base32 ( b32 : & [ u5 ] ) -> Result < Self , Self :: Err > ;
217
218
}
218
219
220
+ #[ cfg( feature = "alloc" ) ]
219
221
impl WriteBase32 for Vec < u5 > {
220
222
type Err = Infallible ;
221
223
@@ -230,6 +232,7 @@ impl WriteBase32 for Vec<u5> {
230
232
}
231
233
}
232
234
235
+ #[ cfg( feature = "alloc" ) ]
233
236
impl FromBase32 for Vec < u8 > {
234
237
type Err = Error ;
235
238
@@ -239,6 +242,7 @@ impl FromBase32 for Vec<u8> {
239
242
}
240
243
241
244
/// A trait for converting a value to a type `T` that represents a `u5` slice.
245
+ #[ cfg( feature = "alloc" ) ]
242
246
pub trait ToBase32 {
243
247
/// Convert `Self` to base32 vector
244
248
fn to_base32 ( & self ) -> Vec < u5 > {
@@ -253,11 +257,13 @@ pub trait ToBase32 {
253
257
}
254
258
255
259
/// Interface to calculate the length of the base32 representation before actually serializing
260
+ #[ cfg( feature = "alloc" ) ]
256
261
pub trait Base32Len : ToBase32 {
257
262
/// Calculate the base32 serialized length
258
263
fn base32_len ( & self ) -> usize ;
259
264
}
260
265
266
+ #[ cfg( feature = "alloc" ) ]
261
267
impl < T : AsRef < [ u8 ] > > ToBase32 for T {
262
268
fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > {
263
269
// Amount of bits left over from last round, stored in buffer.
@@ -302,6 +308,7 @@ impl<T: AsRef<[u8]>> ToBase32 for T {
302
308
}
303
309
}
304
310
311
+ #[ cfg( feature = "alloc" ) ]
305
312
impl < T : AsRef < [ u8 ] > > Base32Len for T {
306
313
fn base32_len ( & self ) -> usize {
307
314
let bits = self . as_ref ( ) . len ( ) * 8 ;
@@ -323,6 +330,7 @@ pub trait CheckBase32<T: AsRef<[u5]>> {
323
330
fn check_base32 ( self ) -> Result < T , Self :: Err > ;
324
331
}
325
332
333
+ #[ cfg( feature = "alloc" ) ]
326
334
impl < T : AsRef < [ u8 ] > > CheckBase32 < Vec < u5 > > for T {
327
335
type Err = Error ;
328
336
@@ -332,6 +340,7 @@ impl<T: AsRef<[u8]>> CheckBase32<Vec<u5>> for T {
332
340
}
333
341
334
342
#[ derive( Clone , Copy , PartialEq , Eq ) ]
343
+ #[ cfg( feature = "alloc" ) ]
335
344
enum Case {
336
345
Upper ,
337
346
Lower ,
@@ -344,6 +353,7 @@ enum Case {
344
353
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
345
354
/// * **InvalidChar**: If the HRP contains any non-ASCII characters (outside 33..=126).
346
355
/// * **InvalidLength**: If the HRP is outside 1..83 characters long.
356
+ #[ cfg( feature = "alloc" ) ]
347
357
fn check_hrp ( hrp : & str ) -> Result < Case , Error > {
348
358
if hrp. is_empty ( ) || hrp. len ( ) > 83 {
349
359
return Err ( Error :: InvalidLength ) ;
@@ -383,6 +393,7 @@ fn check_hrp(hrp: &str) -> Result<Case, Error> {
383
393
/// * If [check_hrp] returns an error for the given HRP.
384
394
/// # Deviations from standard
385
395
/// * No length limits are enforced for the data part
396
+ #[ cfg( feature = "alloc" ) ]
386
397
pub fn encode_to_fmt < T : AsRef < [ u5 ] > > (
387
398
fmt : & mut fmt:: Write ,
388
399
hrp : & str ,
@@ -412,6 +423,7 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
412
423
/// * If [check_hrp] returns an error for the given HRP.
413
424
/// # Deviations from standard
414
425
/// * No length limits are enforced for the data part
426
+ #[ cfg( feature = "alloc" ) ]
415
427
pub fn encode_without_checksum_to_fmt < T : AsRef < [ u5 ] > > (
416
428
fmt : & mut fmt:: Write ,
417
429
hrp : & str ,
@@ -450,6 +462,7 @@ const BECH32M_CONST: u32 = 0x2bc8_30a3;
450
462
451
463
impl Variant {
452
464
// Produce the variant based on the remainder of the polymod operation
465
+ #[ cfg( feature = "alloc" ) ]
453
466
fn from_remainder ( c : u32 ) -> Option < Self > {
454
467
match c {
455
468
BECH32_CONST => Some ( Variant :: Bech32 ) ,
@@ -472,6 +485,7 @@ impl Variant {
472
485
/// * If [check_hrp] returns an error for the given HRP.
473
486
/// # Deviations from standard
474
487
/// * No length limits are enforced for the data part
488
+ #[ cfg( feature = "alloc" ) ]
475
489
pub fn encode < T : AsRef < [ u5 ] > > ( hrp : & str , data : T , variant : Variant ) -> Result < String , Error > {
476
490
let mut buf = String :: new ( ) ;
477
491
encode_to_fmt ( & mut buf, hrp, data, variant) ?. unwrap ( ) ;
@@ -484,6 +498,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
484
498
/// * If [check_hrp] returns an error for the given HRP.
485
499
/// # Deviations from standard
486
500
/// * No length limits are enforced for the data part
501
+ #[ cfg( feature = "alloc" ) ]
487
502
pub fn encode_without_checksum < T : AsRef < [ u5 ] > > ( hrp : & str , data : T ) -> Result < String , Error > {
488
503
let mut buf = String :: new ( ) ;
489
504
encode_without_checksum_to_fmt ( & mut buf, hrp, data) ?. unwrap ( ) ;
@@ -493,6 +508,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
493
508
/// Decode a bech32 string into the raw HRP and the data bytes.
494
509
///
495
510
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
511
+ #[ cfg( feature = "alloc" ) ]
496
512
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
497
513
let ( hrp_lower, mut data) = split_and_decode ( s) ?;
498
514
if data. len ( ) < CHECKSUM_LENGTH {
@@ -514,9 +530,11 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
514
530
/// Decode a bech32 string into the raw HRP and the data bytes, assuming no checksum.
515
531
///
516
532
/// Returns the HRP in lowercase and the data.
533
+ #[ cfg( feature = "alloc" ) ]
517
534
pub fn decode_without_checksum ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > { split_and_decode ( s) }
518
535
519
536
/// Decode a bech32 string into the raw HRP and the `u5` data.
537
+ #[ cfg( feature = "alloc" ) ]
520
538
fn split_and_decode ( s : & str ) -> Result < ( String , Vec < u5 > ) , Error > {
521
539
// Split at separator and check for two pieces
522
540
let ( raw_hrp, raw_data) = match s. rfind ( SEP ) {
@@ -573,12 +591,14 @@ fn split_and_decode(s: &str) -> Result<(String, Vec<u5>), Error> {
573
591
Ok ( ( hrp_lower, data) )
574
592
}
575
593
594
+ #[ cfg( feature = "alloc" ) ]
576
595
fn verify_checksum ( hrp : & [ u8 ] , data : & [ u5 ] ) -> Option < Variant > {
577
596
let mut exp = hrp_expand ( hrp) ;
578
597
exp. extend_from_slice ( data) ;
579
598
Variant :: from_remainder ( polymod ( & exp) )
580
599
}
581
600
601
+ #[ cfg( feature = "alloc" ) ]
582
602
fn hrp_expand ( hrp : & [ u8 ] ) -> Vec < u5 > {
583
603
let mut v: Vec < u5 > = Vec :: new ( ) ;
584
604
for b in hrp {
@@ -591,6 +611,7 @@ fn hrp_expand(hrp: &[u8]) -> Vec<u5> {
591
611
v
592
612
}
593
613
614
+ #[ cfg( feature = "alloc" ) ]
594
615
fn polymod ( values : & [ u5 ] ) -> u32 {
595
616
let mut chk: u32 = 1 ;
596
617
let mut b: u8 ;
@@ -619,6 +640,7 @@ const CHARSET: [char; 32] = [
619
640
] ;
620
641
621
642
/// Reverse character set. Maps ASCII byte -> CHARSET index on [0,31]
643
+ #[ cfg( feature = "alloc" ) ]
622
644
const CHARSET_REV : [ i8 ; 128 ] = [
623
645
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
624
646
-1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
@@ -693,6 +715,7 @@ impl std::error::Error for Error {
693
715
/// let base5 = convert_bits(&[0xff], 8, 5, true);
694
716
/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]);
695
717
/// ```
718
+ #[ cfg( feature = "alloc" ) ]
696
719
pub fn convert_bits < T > ( data : & [ T ] , from : u32 , to : u32 , pad : bool ) -> Result < Vec < u8 > , Error >
697
720
where
698
721
T : Into < u8 > + Copy ,
@@ -728,6 +751,7 @@ where
728
751
}
729
752
730
753
#[ cfg( test) ]
754
+ #[ cfg( feature = "alloc" ) ] // Note, all the unit tests currently require an allocator.
731
755
mod tests {
732
756
use super :: * ;
733
757
0 commit comments