Skip to content

Commit e01a997

Browse files
mkjamboar
authored andcommitted
Allow ensure_no_std to run without alloc
1 parent e75087d commit e01a997

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
toolchain: nightly
6868
targets: thumbv7em-none-eabihf
6969
- run: cd ensure_no_std && cargo build --release --target thumbv7em-none-eabihf
70+
- run: cd ensure_no_std && cargo build --release --target thumbv7em-none-eabihf --no-default-features
7071

7172
ensure_wasm:
7273
name: Ensure wasm

ensure_no_std/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ name = "ensure_no_std"
2222
path = "src/main.rs"
2323

2424
[features]
25-
default = ["alloc"]
25+
default = ["alloc", "deku/alloc", "deku/bits" ]
2626
alloc = []
2727

2828
[dependencies]
2929
cortex-m-rt = "0.7.3"
30-
deku = { path = "../", default-features = false, features = ["alloc", "bits"] }
30+
deku = { path = "../", default-features = false }
3131
embedded-alloc = "0.6.0"
3232

ensure_no_std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![no_std]
22

3+
#[cfg(feature = "alloc")]
34
extern crate alloc;
45

56
pub mod no_alloc_imports;
7+
#[cfg(feature = "alloc")]
68
pub mod with_alloc_imports;

ensure_no_std/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
#![no_std]
33
#![no_main]
44

5-
extern crate alloc;
6-
75
use no_std_lib::*;
86

97
use core::panic::PanicInfo;
108

119
use cortex_m_rt::entry;
12-
use embedded_alloc::LlffHeap as Heap;
13-
14-
#[global_allocator]
15-
static HEAP: Heap = Heap::empty();
1610

1711
#[entry]
1812
fn main() -> ! {
1913
// Initialize the allocator BEFORE you use it
14+
#[cfg(feature = "alloc")]
2015
{
16+
use embedded_alloc::LlffHeap as Heap;
2117
use core::mem::MaybeUninit;
18+
19+
#[global_allocator]
20+
static HEAP: Heap = Heap::empty();
2221
const HEAP_SIZE: usize = 1024;
2322
static HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
2423
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
@@ -27,6 +26,7 @@ fn main() -> ! {
2726
// now the allocator is ready types like Box, Vec can be used.
2827

2928
no_alloc_imports::rw();
29+
#[cfg(feature = "alloc")]
3030
with_alloc_imports::rw();
3131

3232
loop { /* .. */ }

ensure_no_std/src/no_alloc_imports.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@ use deku::prelude::*;
22

33
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
44
struct DekuTest {
5-
#[deku(bits = 5)]
65
field_a: u8,
7-
#[deku(bits = 3)]
86
field_b: u8,
97
count: u8,
108
}
119

1210
pub fn rw() {
1311
#[allow(clippy::unusual_byte_groupings)]
14-
let test_data: &[u8] = &[0b10101_101, 0x02];
15-
let mut cursor = deku::no_std_io::Cursor::new(test_data);
12+
let test_data: &[u8] = &[0xaa, 0xb0, 0x02];
1613

1714
// Test reading
18-
let (_rest, val) = DekuTest::from_reader((&mut cursor, 0)).unwrap();
15+
let (_rest, val) = DekuTest::from_bytes((test_data, 0)).unwrap();
1916
assert_eq!(
2017
DekuTest {
21-
field_a: 0b10101,
22-
field_b: 0b101,
18+
field_a: 0xaa,
19+
field_b: 0xb0,
2320
count: 0x02,
2421
},
2522
val
2623
);
2724

2825
// Test writing
29-
let _val = val.to_bytes().unwrap();
26+
let mut buf = [0; 20];
27+
let _val = val.to_slice(&mut buf).unwrap();
3028
}

0 commit comments

Comments
 (0)