I am working with Rust on a Teensy 4.0 ( --target thumbv7em-none-eabihf
) which means I have to use #![no_std]
.
I have some situations where I want to do different things depending on the position of a rotary switch.
The following is a toy example that illustrates a problem where I want to return one of a series of objects that implement a trait.
fn text_for(selector: i32) -> impl Fn()->Box<dyn Iterator<Item=char>> {
match selector {
1 => || {
let rval : Box<dyn Iterator<Item=char>> = Box::new("author".chars());
rval
},
_ => || {
let rval : Box::<dyn Iterator<Item=char>> = Box::new(b"baseline".iter().map(|&b| (b) as char));
rval
}
}
}
Unfortunately Box
is not available in the no_std environment. I have seen references to an alloc
crate (Is it possible to use Box with no_std?), but when I use extern crate alloc; use alloc::boxed::Box;
the compiler complains
error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait.
error: `#[alloc_error_handler]` function required, but not found.
note: Use `#![feature(default_alloc_error_handler)]` for a default error handler.
Attempting to use the alloc-cortex-m
crate to use a CortexMHeap
as the #[global_allocator]
as suggested by lkolbly results in the following error
error[E0554]: `#![feature]` may not be used on the stable release channel
--> /home/thoth/.cargo/registry/src/github.com-1ecc6299db9ec823/linked_list_allocator-0.8.11/src/lib.rs:1:41
|
1 | #![cfg_attr(feature = "const_mut_refs", feature(const_mut_refs))]
| ^^^^^^^^^^^^^^^^^^^^^^^
How can I work with dyn
instances of a trait in a no_std environment using stable rust?