Introduce alloc feature for optional allocation
Wrap heap-allocated types and service generation in the `alloc` feature flag to support environments without a memory allocator.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![cfg_attr(not(test), no_main)]
|
||||
|
||||
mod helloworld;
|
||||
|
||||
@@ -8,10 +8,11 @@ extern crate alloc;
|
||||
|
||||
use roto_runtime::{ProtoAccessor, RotoMessage, RotoOwned};
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
#[cfg(all(feature = "alloc", not(test)))]
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: embedded_alloc::Heap = embedded_alloc::Heap::empty();
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
@@ -27,6 +28,7 @@ pub extern "C" fn _critical_section_1_0_release() {}
|
||||
|
||||
static HELLO_DATA: &[u8] = &[0x0A, 0x05, 0x57, 0x6f, 0x72, 0x6c, 0x64];
|
||||
|
||||
#[cfg(all(not(test), not(feature = "alloc")))]
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
#[cfg(not(feature = "alloc"))]
|
||||
@@ -58,3 +60,15 @@ pub extern "C" fn _start() -> ! {
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_helloworld_decoding() {
|
||||
let hello = helloworld::Hello::new(HELLO_DATA).expect("failed to decode hello");
|
||||
let name = hello.name().expect("failed to get name");
|
||||
assert!(!name.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user