|
| 1 | +#![feature(alloc_error_handler)] |
| 2 | +#![no_main] |
| 3 | +#![no_std] |
| 4 | + |
| 5 | +extern crate alloc; |
| 6 | +use panic_halt as _; |
| 7 | + |
| 8 | +use self::alloc::string::ToString; |
| 9 | +use self::alloc::vec; |
| 10 | +use self::alloc::vec::Vec; |
| 11 | +use core::alloc::Layout; |
| 12 | + |
| 13 | +use alloc_cortex_m::CortexMHeap; |
| 14 | +use bech32::{self, FromBase32, ToBase32, Variant}; |
| 15 | +use cortex_m::asm; |
| 16 | +use cortex_m_rt::entry; |
| 17 | +use cortex_m_semihosting::{debug, hprintln}; |
| 18 | + |
| 19 | +#[global_allocator] |
| 20 | +static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); |
| 21 | + |
| 22 | +const HEAP_SIZE: usize = 1024; // in bytes |
| 23 | + |
| 24 | +#[entry] |
| 25 | +fn main() -> ! { |
| 26 | + // Initialize the allocator BEFORE you use it |
| 27 | + unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } |
| 28 | + |
| 29 | + let encoded = bech32::encode( |
| 30 | + "bech32", |
| 31 | + vec![0x00, 0x01, 0x02].to_base32(), |
| 32 | + Variant::Bech32, |
| 33 | + ) |
| 34 | + .unwrap(); |
| 35 | + test(encoded == "bech321qqqsyrhqy2a".to_string()); |
| 36 | + |
| 37 | + hprintln!("{}", encoded).unwrap(); |
| 38 | + |
| 39 | + let (hrp, data, variant) = bech32::decode(&encoded).unwrap(); |
| 40 | + test(hrp == "bech32"); |
| 41 | + test(Vec::<u8>::from_base32(&data).unwrap() == vec![0x00, 0x01, 0x02]); |
| 42 | + test(variant == Variant::Bech32); |
| 43 | + |
| 44 | + debug::exit(debug::EXIT_SUCCESS); |
| 45 | + |
| 46 | + loop {} |
| 47 | +} |
| 48 | + |
| 49 | +fn test(result: bool) { |
| 50 | + if !result { |
| 51 | + debug::exit(debug::EXIT_FAILURE); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// define what happens in an Out Of Memory (OOM) condition |
| 56 | +#[alloc_error_handler] |
| 57 | +fn alloc_error(layout: Layout) -> ! { |
| 58 | + hprintln!("{:?}", layout); |
| 59 | + asm::bkpt(); |
| 60 | + |
| 61 | + loop {} |
| 62 | +} |
0 commit comments