Skip to content

Commit 42b3446

Browse files
Ericson2314sgeisler
andcommitted
Allow doing more things without alloc
Co-Authored-By: Sebastian <[email protected]>
1 parent 1645683 commit 42b3446

File tree

10 files changed

+447
-111
lines changed

10 files changed

+447
-111
lines changed

.github/workflows/rust.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ jobs:
8888
steps:
8989
- name: Checkout Crate
9090
uses: actions/checkout@v3
91+
- name: Set up QEMU
92+
run: sudo apt update && sudo apt install -y qemu-system-arm gcc-arm-none-eabi
9193
- name: Checkout Toolchain
92-
uses: dtolnay/rust-toolchain@stable
94+
uses: dtolnay/rust-toolchain@nightly
95+
with:
96+
targets: thumbv7m-none-eabi
9397
- name: Run
94-
run: cd embedded/no-allocator && cargo rustc -- -C link-arg=-nostartfiles
98+
env:
99+
RUSTFLAGS: "-C link-arg=-Tlink.x"
100+
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
101+
run: cd embedded/no-allocator && cargo run --target thumbv7m-none-eabi

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ alloc = []
1919
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
2020
strict = []
2121

22-
[dependencies]
22+
[dependencies.arrayvec]
23+
version = "0.7.1"
24+
default-features = false
25+
optional = true

contrib/test.sh

+33-12
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,31 @@
44
#
55
# The "strict" feature is used to configure cargo to deny all warnings, always use it in test runs.
66

7-
set -ex
8-
9-
FEATURES="std alloc"
7+
set -eu
8+
set -x
109

1110
# Some tests require certain toolchain types.
1211
NIGHTLY=false
12+
MIN_VERSION=false
1313
if cargo --version | grep nightly; then
1414
NIGHTLY=true
1515
fi
16+
if cargo --version | grep 1.41; then
17+
MIN_VERSION=true
18+
fi
1619

1720
# Sanity, check tools exist.
1821
cargo --version
1922
rustc --version
2023

2124
# Run the linter if told to.
22-
if [ "$DO_LINT" = true ]
25+
if [ "${DO_LINT-false}" = true ]
2326
then
2427
cargo clippy --all-features --all-targets -- -D warnings
2528
fi
2629

2730
# Run formatter if told to.
28-
if [ "$DO_FMT" = true ]; then
31+
if [ "${DO_FMT-false}" = true ]; then
2932
if [ "$NIGHTLY" = false ]; then
3033
echo "DO_FMT requires a nightly toolchain (consider using RUSTUP_TOOLCHAIN)"
3134
exit 1
@@ -34,16 +37,34 @@ if [ "$DO_FMT" = true ]; then
3437
cargo fmt --check
3538
fi
3639

40+
check () {
41+
cargo build --no-default-features --features="strict $1"
42+
cargo test --no-default-features --features="strict $1"
43+
}
44+
3745
# Check without features ("strict" is a CI feature only, see above).
38-
cargo build --no-default-features --features="strict"
39-
cargo test --no-default-features --features="strict"
46+
check ""
4047

41-
# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
42-
cargo build --no-default-features --features="strict std"
43-
cargo test --no-default-features --features="strict std"
48+
# Check "arrayvec" feature alone.
49+
if [ "$MIN_VERSION" != true ]; then
50+
check "arrayvec"
51+
fi
4452

4553
# Check "alloc" feature alone.
46-
cargo build --no-default-features --features="strict alloc"
47-
cargo test --no-default-features --features="strict alloc"
54+
check "alloc"
55+
56+
# Check "alloc" & "arrayvec" features together.
57+
if [ "$MIN_VERSION" != true ]; then
58+
check "alloc arrayvec"
59+
fi
60+
61+
# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
62+
cargo build --no-default-features --features="std"
63+
cargo test --no-default-features --features="std"
64+
65+
# Check "std" & "arrayvec" features together.
66+
if [ "$MIN_VERSION" != true ]; then
67+
check "std arrayvec"
68+
fi
4869

4970
exit 0

embedded/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Embedded tests
2+
3+
These no-std tests are a bit peculiar.
4+
They are cross compiled to ARM and run in an emulator.
5+
Here's why:
6+
7+
- We want to build for an exotic platform to help make sure `std` doesn't sneak in by accident.
8+
9+
- We use an emulator and build something runnable,
10+
rather than merely testing whether a library builds,
11+
because we want to actually run our integration test.

embedded/no-allocator/Cargo.toml

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ readme = "README.md"
55
name = "no-allocator"
66
version = "0.1.0"
77

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+
arrayvec = { version = "0.7.1", default-features = false }
14+
bech32 = { path = "../../", default-features = false, features = ["arrayvec"] }
15+
16+
[[bin]]
17+
name = "no-allocator"
18+
test = false
19+
bench = false
20+
821
[profile.dev]
922
panic = "abort"
1023

1124
[profile.release]
1225
panic = "abort"
13-
14-
[dependencies]
15-
bech32 = { path = "../../", default_features = false }

embedded/no-allocator/README.md

-10
This file was deleted.

embedded/no-allocator/memory.x

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../with-allocator/memory.x

embedded/no-allocator/src/main.rs

+39-11
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,52 @@
33
//! Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
44
//!
55
6-
#![no_std]
6+
#![feature(alloc_error_handler)]
77
#![no_main]
8+
#![no_std]
9+
10+
use panic_halt as _;
811

9-
use core::panic::PanicInfo;
12+
use arrayvec::{ArrayString, ArrayVec};
13+
use bech32::{self, u5, ComboError, FromBase32, ToBase32, Variant};
14+
use cortex_m_rt::entry;
15+
use cortex_m_semihosting::{debug, hprintln};
1016

1117
// Note: `#[global_allocator]` is NOT set.
1218

13-
#[allow(unused_imports)]
14-
use bech32;
19+
#[entry]
20+
fn main() -> ! {
21+
let mut encoded = ArrayString::<30>::new();
22+
23+
let mut base32 = ArrayVec::<u5, 30>::new();
24+
25+
[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();
26+
27+
bech32::encode_to_fmt_anycase(&mut encoded, "bech32", &base32, Variant::Bech32)
28+
.unwrap()
29+
.unwrap();
30+
test(&*encoded == "bech321qqqsyrhqy2a");
31+
32+
hprintln!("{}", encoded).unwrap();
33+
34+
let mut decoded = ArrayVec::<u5, 30>::new();
35+
36+
let mut scratch = ArrayVec::<u5, 30>::new();
37+
38+
let (hrp, data, variant) =
39+
bech32::decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
40+
test(hrp == "bech32");
41+
let res = ArrayVec::<u8, 30>::from_base32(&data).unwrap();
42+
test(&res == [0x00, 0x01, 0x02].as_ref());
43+
test(variant == Variant::Bech32);
44+
45+
debug::exit(debug::EXIT_SUCCESS);
1546

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) -> ! {
2047
loop {}
2148
}
2249

23-
#[no_mangle]
24-
pub extern "C" fn _start() -> ! {
25-
loop {}
50+
fn test(result: bool) {
51+
if !result {
52+
debug::exit(debug::EXIT_FAILURE);
53+
}
2654
}

embedded/with-allocator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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, features = ["alloc"] }
14+
bech32 = { path = "../../", default-features = false, features = ["alloc"] }
1515

1616
[[bin]]
1717
name = "with-allocator"

0 commit comments

Comments
 (0)