From 7a2a5279164ee7fae1711635802207569ae0a74f Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Fri, 18 Oct 2024 18:07:07 +0800 Subject: [PATCH] cleaning up RTIO_DEVICE_MAP --- src/libksupport/src/lib.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/libksupport/src/lib.rs b/src/libksupport/src/lib.rs index 7f978d4..a3e972f 100644 --- a/src/libksupport/src/lib.rs +++ b/src/libksupport/src/lib.rs @@ -35,6 +35,7 @@ pub mod rtio; #[rustfmt::skip] #[path = "../../../build/pl.rs"] pub mod pl; +use core::ptr::addr_of; #[derive(Debug, Clone)] @@ -115,6 +116,7 @@ pub async fn report_async_rtio_errors() { } } +// use interior mutability -> no need for this to be a static mut (no volatile r/ws or FFI) static mut RTIO_DEVICE_MAP: BTreeMap = BTreeMap::new(); fn read_device_map(cfg: &Config) -> BTreeMap { @@ -146,15 +148,14 @@ fn read_device_map(cfg: &Config) -> BTreeMap { device_map } -fn _resolve_channel_name(channel: u32, device_map: &BTreeMap) -> String { - match device_map.get(&channel) { - Some(val) => val.clone(), - None => String::from("unknown"), - } -} - pub fn resolve_channel_name(channel: u32) -> String { - _resolve_channel_name(channel, unsafe { &RTIO_DEVICE_MAP }) + unsafe { + let ptr: *const BTreeMap = addr_of!(RTIO_DEVICE_MAP); + match ptr.as_ref().unwrap().get(&channel) { + Some(val) => val.clone(), + None => String::from("unknown"), + } + } } pub fn setup_device_map(cfg: &Config) {