Skip to content

Commit 0096985

Browse files
committed
Eliminate alloc::boxed::Box deps from Runtime
1 parent 9d6e613 commit 0096985

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

examples/xbd-net/src/embassy/executor.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
use mcu_if::{println, alloc::boxed::Box};
21
use embassy_executor::{Spawner, raw::Executor as RawExecutor};
2+
use super::{to_raw, static_from_raw};
33

44
// https://github.com/embassy-rs/embassy/blob/b6fc682117a41e8e63a9632e06da5a17f46d9ab0/embassy-executor/src/raw/mod.rs#L465
55
#[export_name = "__pender"]
66
fn pender(context: *mut ()) {
7-
let signaler: &'static Signaler = unsafe { core::mem::transmute(context) };
8-
if 0 == 1 { println!("@@ pender(): signaler: {:?}", signaler); }
7+
//@@ let signaler: &'static Signaler = unsafe { core::mem::transmute(context) };
8+
let signaler: &'static Signaler = static_from_raw(context);
9+
if 0 == 1 { crate::println!("@@ pender(): signaler: {:?}", signaler); }
910
}
1011

1112
pub struct Executor {
1213
executor: RawExecutor,
13-
_signaler: &'static Signaler, // c.f. embassy/embassy-executor/src/arch/std.rs
14+
//@@ _signaler: &'static Signaler, // c.f. embassy/embassy-executor/src/arch/std.rs
15+
_signaler: Signaler,
1416
}
1517

1618
#[derive(Debug)]
1719
struct Signaler(()); // TODO
1820

1921
impl Executor {
2022
pub fn new() -> Self {
21-
let signaler = Box::leak(Box::new(Signaler(())));
23+
//@@ let signaler = Box::leak(Box::new(Signaler(())));
24+
let mut signaler = Signaler(());
2225

2326
Self {
24-
executor: RawExecutor::new(signaler as *mut _ as *mut ()),
27+
executor: RawExecutor::new(to_raw(&mut signaler)),
2528
_signaler: signaler,
2629
}
2730
}

examples/xbd-net/src/embassy/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use mcu_if::alloc::boxed::Box;
21
use super::xbd::{self, Xbd};
32

43
mod executor;
@@ -30,19 +29,21 @@ async fn task_shell_stream() {
3029
xbd::process_shell_stream().await.unwrap();
3130
}
3231

33-
pub struct Runtime(&'static mut Executor);
34-
35-
impl Runtime {
36-
pub fn new_static() -> Result<&'static mut Self, ()> {
37-
Ok(Self::get_static(Self::new()))
38-
}
32+
fn to_raw<T>(x: &mut T) -> *mut () { x as *mut _ as _ }
33+
fn static_from_raw<T>(p: *mut ()) -> &'static mut T { unsafe { core::mem::transmute(p) } }
34+
pub fn get_static<T>(x: &mut T) -> &'static mut T { static_from_raw(to_raw(x)) }
35+
/* deprecated
36+
pub fn get_static<T>(x: &mut T) -> &'static mut T {
37+
use mcu_if::alloc::boxed::Box;
38+
Box::leak(Box::new(x))
39+
}
40+
*/
3941

40-
fn new() -> Self {
41-
Self(Self::get_static(Executor::new()))
42-
}
42+
pub struct Runtime(Executor);
4343

44-
fn get_static<T>(x: T) -> &'static mut T {
45-
Box::leak(Box::new(x))
44+
impl Runtime {
45+
pub fn new() -> Self {
46+
Self(Executor::new())
4647
}
4748

4849
pub fn run(&'static mut self) -> ! {

examples/xbd-net/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ pub extern fn rustmod_start(
4747
if 0 == 1 { // debug
4848
Xbd::usleep(1_000_000);
4949
blogos12::test_misc();
50-
return;
50+
panic!("ok");
5151
}
5252

5353
if 1 == 1 {
5454
println!("@@ [debug] `xbd_main()` with `embassy::Runtime` ...");
55-
embassy::Runtime::new_static().unwrap().run();
55+
embassy::get_static(&mut embassy::Runtime::new())
56+
.run(); // -> !
57+
58+
// should be never reached
5659
} else {
5760
println!("@@ [debug] `xbd_main()` with `blogos12::Runtime` ...");
5861
let _ = blogos12::Runtime::new()

0 commit comments

Comments
 (0)