Skip to content

Commit e26863a

Browse files
committed
Merge #71: Add "alloc" feature
85caa28 Add CI test.sh script (Tobin C. Harding) 1ceb828 Add no-allocator crate (Tobin C. Harding) 8f27962 Add "alloc" feature (Tobin C. Harding) 33bad4c Move embedded to sub-dir with-allocator (Tobin C. Harding) Pull request description: We would like users to be able to use parts of this library in a `no_std` environment without an allocator. To achieve this add an "alloc" feature and feature gate any code that requires allocation behind. To test the new feature gating works, and catch regressions, add a crate that uses `bech32` as a dependency with no default features and forces a `no_std` build. (Does not test any code just imports the lib to verify it builds.) Patch 3 can be moved before patch 2 to verify that the crate does not currently build without an allocator. Patch 4 adds a minimal `contrib/test.sh` script. ACKs for top commit: apoelstra: ACK 85caa28 Tree-SHA512: ee1507d08dca79ddee07e6b7188c8f707d0c0dd1aaf778f3d5b376fa5ba99358260ed034efce852366e794843737757589564cbf65fa3e2560ff73559baa06d2
2 parents 8f83401 + 85caa28 commit e26863a

File tree

11 files changed

+141
-18
lines changed

11 files changed

+141
-18
lines changed

.github/workflows/rust.yml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on: [pull_request]
33
name: Continuous Integration
44

55
jobs:
6-
test:
6+
Test:
77
name: Test Suite
88
runs-on: ubuntu-latest
99
strategy:
@@ -13,16 +13,17 @@ jobs:
1313
- stable
1414
- nightly
1515
steps:
16+
- name: Checkout Crate
1617
- uses: actions/checkout@v2
18+
- name: Checkout Toolchain
1719
- uses: actions-rs/toolchain@v1
1820
with:
1921
profile: minimal
2022
toolchain: ${{ matrix.rust }}
2123
override: true
22-
- uses: actions-rs/cargo@v1
23-
with:
24-
command: test
25-
args: --verbose --features strict
24+
- name: Run Test Script
25+
env: ${{ matrix.rust }}
26+
run: ./contrib/test.sh
2627

2728
fmt:
2829
name: Rustfmt
@@ -60,7 +61,8 @@ jobs:
6061
command: clippy
6162
args: -- -D warnings
6263

63-
Embedded:
64+
EmbeddedWithAlloc:
65+
name: no_std with alloc
6466
runs-on: ubuntu-latest
6567
steps:
6668
- name: Checkout
@@ -79,4 +81,20 @@ jobs:
7981
env:
8082
RUSTFLAGS: "-C link-arg=-Tlink.x"
8183
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
82-
run: cd embedded && cargo run --target thumbv7m-none-eabi
84+
run: cd embedded/with-allocator && cargo run --target thumbv7m-none-eabi
85+
86+
EmbeddedNoAlloc:
87+
name: no_std no alloc
88+
runs-on: ubuntu-latest
89+
strategy:
90+
steps:
91+
- uses: actions/checkout@v2
92+
- uses: actions-rs/toolchain@v1
93+
with:
94+
profile: minimal
95+
toolchain: stable
96+
override: true
97+
- uses: actions-rs/cargo@v1
98+
with:
99+
command: rustc
100+
args: -- -C link-arg=-nostartfiles

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
*.o
33
/Cargo.lock
44

5-
/embedded/Cargo.lock
6-
/embedded/.cargo
5+
/embedded/no-allocator/Cargo.lock
6+
/embedded/no-allocator/.cargo
7+
/embedded/with-allocator/Cargo.lock
8+
/embedded/with-allocator/.cargo

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ edition = "2018"
1313

1414
[features]
1515
default = ["std"]
16-
std = []
16+
std = ["alloc"]
17+
alloc = []
18+
1719
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
1820
strict = []
1921

contrib/test.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
#
3+
# CI test script for rust-bech32.
4+
#
5+
# The "strict" feature is used to configure cargo to deny all warnings, always use it in test runs.
6+
7+
set -ex
8+
9+
# Sanity, check tools exist.
10+
cargo --version
11+
rustc --version
12+
13+
# Check without features ("strict" is a CI feature only, see above).
14+
cargo build --no-default-features --features="strict"
15+
cargo test --no-default-features --features="strict"
16+
17+
# Check "std" feature (implies "alloc").
18+
cargo build --no-default-features --features="strict std"
19+
cargo test --no-default-features --features="strict std"
20+
21+
# Check "alloc" feature alone.
22+
cargo build --no-default-features --features="strict alloc"
23+
cargo test --no-default-features --features="strict alloc"
24+
25+
exit 0

embedded/no-allocator/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
authors = ["Tobin C. Harding <me@tobin.cc>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "no-allocator"
6+
version = "0.1.0"
7+
8+
[profile.dev]
9+
panic = "abort"
10+
11+
[profile.release]
12+
panic = "abort"
13+
14+
[dependencies]
15+
bech32 = { path = "../../", default_features = false }

embedded/no-allocator/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# no_std test crate without an allocator
2+
3+
This crate is based on the blog post found at:
4+
5+
https://blog.dbrgn.ch/2019/12/24/testing-for-no-std-compatibility/
6+
7+
Its purpose is to test that the `rust-bech32` library can be built in a `no_std` environment without
8+
a global allocator.
9+
10+
Build with: `cargo rustc -- -C link-arg=-nostartfiles`.

embedded/no-allocator/src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Test `no_std` build of `bech32`.
2+
//!
3+
//! Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
4+
//!
5+
6+
#![no_std]
7+
#![no_main]
8+
9+
use core::panic::PanicInfo;
10+
11+
// Note: `#[global_allocator]` is NOT set.
12+
13+
#[allow(unused_imports)]
14+
use bech32;
15+
16+
/// This function is called on panic, defining this ensures build will fail if `std` is enabled
17+
/// because `panic` will be defined twice.
18+
#[panic_handler]
19+
fn panic(_info: &PanicInfo) -> ! {
20+
loop {}
21+
}
22+
23+
#[no_mangle]
24+
pub extern "C" fn _start() -> ! {
25+
loop {}
26+
}

embedded/Cargo.toml renamed to embedded/with-allocator/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Riccardo Casatta <riccardo@casatta.it>"]
33
edition = "2018"
44
readme = "README.md"
5-
name = "embedded"
5+
name = "with-allocator"
66
version = "0.1.0"
77

88
[dependencies]
@@ -11,10 +11,10 @@ cortex-m-rt = "0.6.10"
1111
cortex-m-semihosting = "0.3.3"
1212
panic-halt = "0.2.0"
1313
alloc-cortex-m = "0.4.1"
14-
bech32 = { path="../", default-features = false }
14+
bech32 = { path="../../", default-features = false }
1515

1616
[[bin]]
17-
name = "embedded"
17+
name = "with-allocator"
1818
test = false
1919
bench = false
2020

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)