Skip to content

Commit f2d8845

Browse files
committed
Merge branch 'use-void-for-infalliblity' into std-feature
2 parents 2fa121a + d85414d commit f2d8845

File tree

15 files changed

+558
-166
lines changed

15 files changed

+558
-166
lines changed

.github/workflows/rust.yml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
on: [pull_request]
2+
3+
name: Continuous Integration
4+
5+
jobs:
6+
test:
7+
name: Test Suite
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
rust:
12+
- 1.29.0
13+
- stable
14+
- nightly
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions-rs/toolchain@v1
18+
with:
19+
profile: minimal
20+
toolchain: ${{ matrix.rust }}
21+
override: true
22+
- uses: actions-rs/cargo@v1
23+
with:
24+
command: test
25+
args: --verbose --features strict
26+
27+
fmt:
28+
name: Rustfmt
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
rust:
33+
- stable
34+
steps:
35+
- uses: actions/checkout@v2
36+
- uses: actions-rs/toolchain@v1
37+
with:
38+
profile: minimal
39+
toolchain: ${{ matrix.rust }}
40+
override: true
41+
- run: rustup component add rustfmt
42+
- uses: actions-rs/cargo@v1
43+
with:
44+
command: fmt
45+
args: --all -- --check
46+
47+
clippy:
48+
name: Clippy
49+
runs-on: ubuntu-latest
50+
strategy:
51+
matrix:
52+
rust:
53+
- stable
54+
steps:
55+
- uses: actions/checkout@v2
56+
- uses: actions-rs/toolchain@v1
57+
with:
58+
profile: minimal
59+
toolchain: ${{ matrix.rust }}
60+
override: true
61+
- run: rustup component add clippy
62+
- uses: actions-rs/cargo@v1
63+
with:
64+
command: clippy
65+
args: -- -D warnings
66+
67+
Embedded-No-Alloc:
68+
runs-on: ubuntu-latest
69+
steps:
70+
- name: Checkout
71+
uses: actions/checkout@v2
72+
- name: Set up QEMU
73+
run: sudo apt install qemu-system-arm
74+
- name: Checkout Toolchain
75+
uses: actions-rs/toolchain@v1
76+
with:
77+
profile: minimal
78+
toolchain: nightly
79+
override: true
80+
components: rust-src
81+
target: thumbv7m-none-eabi
82+
- name: Run
83+
env:
84+
RUSTFLAGS: "-C link-arg=-Tlink.x"
85+
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
86+
run: cd embedded-no-alloc && cargo run --target thumbv7m-none-eabi
87+
88+
Embedded-Alloc:
89+
runs-on: ubuntu-latest
90+
steps:
91+
- name: Checkout
92+
uses: actions/checkout@v2
93+
- name: Set up QEMU
94+
run: sudo apt install qemu-system-arm
95+
- name: Checkout Toolchain
96+
uses: actions-rs/toolchain@v1
97+
with:
98+
profile: minimal
99+
toolchain: nightly
100+
override: true
101+
components: rust-src
102+
target: thumbv7m-none-eabi
103+
- name: Run
104+
env:
105+
RUSTFLAGS: "-C link-arg=-Tlink.x"
106+
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
107+
run: cd embedded-alloc && cargo run --target thumbv7m-none-eabi

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
**/target
22
*.o
33
/Cargo.lock
4+
5+
/embedded/Cargo.lock
6+
/embedded/.cargo

.travis.yml

-29
This file was deleted.

Cargo.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bech32"
3-
version = "0.7.2"
3+
version = "0.8.1"
44
authors = ["Clark Moody"]
55
repository = "https://github.com/rust-bitcoin/rust-bech32"
66
description = "Encodes and decodes the Bech32 format"
@@ -11,9 +11,17 @@ license = "MIT"
1111

1212
[features]
1313
default = ["std"]
14-
std = []
14+
arrayvec = ["arrayvec_dep"]
15+
alloc = []
16+
std = ["alloc"]
1517
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
1618
strict = []
1719

1820
[dependencies]
1921
void = { default-features = false, version = "1" }
22+
23+
[dependencies.arrayvec_dep]
24+
package = "arrayvec"
25+
version = "0.7.1"
26+
default-features = false
27+
optional = true

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Bech32 Rust
22
[![Docs.rs badge](https://docs.rs/bech32/badge.svg)](https://docs.rs/bech32/)
3-
[![Build Status](https://travis-ci.org/rust-bitcoin/rust-bech32.svg?branch=master)](https://travis-ci.org/rust-bitcoin/rust-bech32)
3+
[![Continuous Integration](https://github.com/rust-bitcoin/rust-bech32/workflows/Continuous%20Integration/badge.svg)](https://github.com/rust-bitcoin/rust-bech32/actions?query=workflow%3A%22Continuous+Integration%22)
44

55
Rust implementation of the Bech32 encoding format described in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki).
66
You can find some usage examples in the [documentation](https://docs.rs/bech32/).
77

88
Bitcoin-specific address encoding is handled by the `bitcoin-bech32` crate.
9+
10+
# MSRV
11+
The minimum supported Rust version with the standard library is **1.29**.
12+
13+
With nostd, we use the `alloc` dependency, so the MSRV is instead **1.36**.
14+

clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msrv = "1.29.0"

embedded-alloc/Cargo.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
authors = ["Riccardo Casatta <[email protected]>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "embedded-alloc"
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
cortex-m = "0.6.0"
10+
cortex-m-rt = "0.6.10"
11+
cortex-m-semihosting = "0.3.3"
12+
panic-halt = "0.2.0"
13+
alloc-cortex-m = "0.4.1"
14+
bech32 = { path="../", default-features = false, features = ["alloc"] }
15+
16+
[[bin]]
17+
name = "embedded-alloc"
18+
test = false
19+
bench = false
20+
21+
[profile.release]
22+
codegen-units = 1 # better optimizations
23+
debug = true # symbols are nice and they don't increase the size on Flash
24+
lto = true # better optimizations

embedded-alloc/memory.x

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MEMORY
2+
{
3+
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
5+
RAM : ORIGIN = 0x20000000, LENGTH = 64K
6+
}

embedded-alloc/src/main.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

embedded-no-alloc/Cargo.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
authors = ["Riccardo Casatta <[email protected]>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "embedded-no-alloc"
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
arrayvec = { version = "0.7.1", default-features = false }
10+
cortex-m = "0.6.0"
11+
cortex-m-rt = "0.6.10"
12+
cortex-m-semihosting = "0.3.3"
13+
panic-halt = "0.2.0"
14+
bech32 = { path="../", default-features = false, features = ["arrayvec"] }
15+
16+
[[bin]]
17+
name = "embedded-no-alloc"
18+
test = false
19+
bench = false
20+
21+
[profile.release]
22+
codegen-units = 1 # better optimizations
23+
debug = true # symbols are nice and they don't increase the size on Flash
24+
lto = true # better optimizations

embedded-no-alloc/memory.x

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MEMORY
2+
{
3+
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
5+
RAM : ORIGIN = 0x20000000, LENGTH = 64K
6+
}

embedded-no-alloc/src/main.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![feature(alloc_error_handler)]
2+
#![no_main]
3+
#![no_std]
4+
5+
use panic_halt as _;
6+
7+
use arrayvec::{ArrayString, ArrayVec};
8+
use bech32::{self, u5, ComboError, FromBase32, ToBase32, Variant};
9+
use cortex_m_rt::entry;
10+
use cortex_m_semihosting::{debug, hprintln};
11+
12+
#[entry]
13+
fn main() -> ! {
14+
let mut encoded = ArrayString::<30>::new();
15+
16+
let mut base32 = ArrayVec::<u5, 30>::new();
17+
18+
[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();
19+
20+
bech32::encode_to_fmt_anycase(&mut encoded, "bech32", &base32, Variant::Bech32)
21+
.unwrap()
22+
.unwrap();
23+
test(&*encoded == "bech321qqqsyrhqy2a");
24+
25+
hprintln!("{}", encoded).unwrap();
26+
27+
let mut decoded = ArrayVec::<u5, 30>::new();
28+
29+
let mut scratch = ArrayVec::<u5, 30>::new();
30+
31+
let (hrp, data, variant) =
32+
bech32::decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
33+
test(hrp == "bech32");
34+
let res = ArrayVec::<u8, 30>::from_base32(&data).unwrap();
35+
test(&res == [0x00, 0x01, 0x02].as_ref());
36+
test(variant == Variant::Bech32);
37+
38+
debug::exit(debug::EXIT_SUCCESS);
39+
40+
loop {}
41+
}
42+
43+
fn test(result: bool) {
44+
if !result {
45+
debug::exit(debug::EXIT_FAILURE);
46+
}
47+
}

fuzz/fuzz_targets/decode_rnd.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn do_test(data: &[u8]) {
1010
Err(_) => return,
1111
};
1212

13-
assert_eq!(bech32::encode(&b32.0, b32.1).unwrap(), data_str);
13+
assert_eq!(bech32::encode(&b32.0, b32.1, b32.2).unwrap(), data_str);
1414
}
1515

1616
#[cfg(feature = "afl")]
@@ -23,7 +23,8 @@ fn main() {
2323
}
2424

2525
#[cfg(feature = "honggfuzz")]
26-
#[macro_use] extern crate honggfuzz;
26+
#[macro_use]
27+
extern crate honggfuzz;
2728
#[cfg(feature = "honggfuzz")]
2829
fn main() {
2930
loop {

0 commit comments

Comments
 (0)