Skip to content

Commit 1ceb828

Browse files
committed
Add no-allocator crate
In order to test that `bech32` can be built in a `no_std` environment without an allocator add a crate `no-allocator` to the `embedded` directory. Add a CI job to build the crate.
1 parent 8f27962 commit 1ceb828

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

.github/workflows/rust.yml

+16
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,19 @@ jobs:
8585
RUSTFLAGS: "-C link-arg=-Tlink.x"
8686
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
8787
run: cd embedded/with-allocator && cargo run --target thumbv7m-none-eabi
88+
89+
EmbeddedNoAlloc:
90+
name: no_std no alloc
91+
runs-on: ubuntu-latest
92+
strategy:
93+
steps:
94+
- uses: actions/checkout@v2
95+
- uses: actions-rs/toolchain@v1
96+
with:
97+
profile: minimal
98+
toolchain: stable
99+
override: true
100+
- uses: actions-rs/cargo@v1
101+
with:
102+
command: rustc
103+
args: -- -C link-arg=-nostartfiles

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
*.o
33
/Cargo.lock
44

5+
/embedded/no-allocator/Cargo.lock
6+
/embedded/no-allocator/.cargo
57
/embedded/with-allocator/Cargo.lock
68
/embedded/with-allocator/.cargo

embedded/no-allocator/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
authors = ["Tobin C. Harding <[email protected]>"]
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

+10
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

+26
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+
}

0 commit comments

Comments
 (0)