Skip to content

Commit 24d76fc

Browse files
Ericson2314sgeisler
andcommitted
Create std feature for core-only users.
Co-Authored-By: Sebastian <[email protected]>
1 parent cc50e7d commit 24d76fc

File tree

9 files changed

+413
-51
lines changed

9 files changed

+413
-51
lines changed

.github/workflows/rust.yml

+23-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
command: clippy
6565
args: -- -D warnings
6666

67-
Embedded:
67+
Embedded-No-Alloc:
6868
runs-on: ubuntu-latest
6969
steps:
7070
- name: Checkout
@@ -83,4 +83,25 @@ jobs:
8383
env:
8484
RUSTFLAGS: "-C link-arg=-Tlink.x"
8585
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
86-
run: cd embedded && cargo run --target thumbv7m-none-eabi
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 mps2-an385 -nographic -semihosting-config enable=on,target=native -kernel"
107+
run: cd embedded-alloc && cargo run --target thumbv7m-none-eabi

Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ edition = "2018"
1212

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

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

embedded/Cargo.toml renamed to embedded-alloc/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Riccardo Casatta <[email protected]>"]
33
edition = "2018"
44
readme = "README.md"
5-
name = "embedded"
5+
name = "embedded-alloc"
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, features = ["alloc"] }
1515

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

File renamed without changes.
File renamed without changes.

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

0 commit comments

Comments
 (0)