Skip to content

Commit

Permalink
Allow doing more things without alloc
Browse files Browse the repository at this point in the history
Co-Authored-By: Sebastian <geisler.sebastian@googlemail.com>
  • Loading branch information
Ericson2314 and sgeisler committed Mar 22, 2023
1 parent 1645683 commit e3d4ef4
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ jobs:
- name: Checkout Crate
uses: actions/checkout@v3
- name: Checkout Toolchain
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@nightly
- name: Run
run: cd embedded/no-allocator && cargo rustc -- -C link-arg=-nostartfiles
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ alloc = []
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
strict = []

[dependencies]
[dependencies.arrayvec]
version = "0.7.1"
default-features = false
optional = true
18 changes: 15 additions & 3 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,24 @@ fi
cargo build --no-default-features --features="strict"
cargo test --no-default-features --features="strict"

# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
cargo build --no-default-features --features="strict std"
cargo test --no-default-features --features="strict std"
# Check "arrayvec" feature alone.
cargo build --no-default-features --features="strict arrayvec"
cargo test --no-default-features --features="strict arrayvec"

# Check "alloc" feature alone.
cargo build --no-default-features --features="strict alloc"
cargo test --no-default-features --features="strict alloc"

# Check "alloc" & "arrayvec" features together.
cargo build --no-default-features --features="strict alloc arrayvec"
cargo test --no-default-features --features="strict alloc arrayvec"

# Check "std" feature (implies "alloc", so this is equivalent to --all-features).
cargo build --no-default-features --features="strict std"
cargo test --no-default-features --features="strict std"

# Check "std" & "arrayvec" features together.
cargo build --no-default-features --features="strict std arrayvec"
cargo test --no-default-features --features="strict std arrayvec"

exit 0
19 changes: 16 additions & 3 deletions embedded/no-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ readme = "README.md"
name = "no-allocator"
version = "0.1.0"

[dependencies]
cortex-m = "0.6.0"
cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
arrayvec = { version = "0.7.1", default-features = false }
bech32 = { path = "../../", default-features = false, features = ["arrayvec"] }

[[bin]]
name = "no-allocator"
test = false
bench = false

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
bech32 = { path = "../../", default_features = false }
codegen-units = 1 # better optimizations
debug = true # symbols are nice and they don't increase the size on Flash
lto = true # better optimizations
50 changes: 39 additions & 11 deletions embedded/no-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,52 @@
//! Build with: `cargo rustc -- -C link-arg=-nostartfiles`.
//!

#![no_std]
#![feature(alloc_error_handler)]
#![no_main]
#![no_std]

use panic_halt as _;

use core::panic::PanicInfo;
use arrayvec::{ArrayString, ArrayVec};
use bech32::{self, u5, ComboError, FromBase32, ToBase32, Variant};
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};

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

#[allow(unused_imports)]
use bech32;
#[entry]
fn main() -> ! {
let mut encoded = ArrayString::<30>::new();

let mut base32 = ArrayVec::<u5, 30>::new();

[0x00u8, 0x01, 0x02].write_base32(&mut base32).unwrap();

bech32::encode_to_fmt_anycase(&mut encoded, "bech32", &base32, Variant::Bech32)
.unwrap()
.unwrap();
test(&*encoded == "bech321qqqsyrhqy2a");

hprintln!("{}", encoded).unwrap();

let mut decoded = ArrayVec::<u5, 30>::new();

let mut scratch = ArrayVec::<u5, 30>::new();

let (hrp, data, variant) =
bech32::decode_lowercase::<ComboError, _, _>(&encoded, &mut decoded, &mut scratch).unwrap();
test(hrp == "bech32");
let res = ArrayVec::<u8, 30>::from_base32(&data).unwrap();
test(&res == [0x00, 0x01, 0x02].as_ref());
test(variant == Variant::Bech32);

debug::exit(debug::EXIT_SUCCESS);

/// This function is called on panic, defining this ensures build will fail if `std` is enabled
/// because `panic` will be defined twice.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {}
fn test(result: bool) {
if !result {
debug::exit(debug::EXIT_FAILURE);
}
}
2 changes: 1 addition & 1 deletion embedded/with-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0"
alloc-cortex-m = "0.4.1"
bech32 = { path="../../", default-features = false, features = ["alloc"] }
bech32 = { path = "../../", default-features = false, features = ["alloc"] }

[[bin]]
name = "with-allocator"
Expand Down
Loading

0 comments on commit e3d4ef4

Please sign in to comment.