Warning
This post was published 49 days ago. The information described in this article may have changed.
Hello, wise people of Rust!
I have a convoluted use case of async Rust co-operating with a C library with callbacks.
C
setup_device()
).read_register()
, write_register()
events).send()
/recv()
(yep, I know, read register via network. Nobody claims people at Microchip are sane).cc
crate)unsafe extern "C" { ... }
) block.#[unsafe(no_mangle)] pub extern "C" fn XXX(){ ... }
Now it is time to stitch it together.
loop { select! { ... }}
.async fn lib_requestor_loop(){}
connected with main reactor via async mpsc queue lib_queue
of enum Cmd{}
,
each Cmd
has a one-shot channel lib_reply
for reply from the library.
Dispatched Cmd
calls blocking the library
lib callbacks issue enum NetReq{}
via async mpsc queue net_rquests
to the main reactor (try_send
is sync, no problem), each NetReq
has a field with a sync channel net_reply
for enum NetResult{}
-s (i don't see a sync one-shot implementation anywhere, Tokio's sync one_shot::try_recv()
doesn't block). Callback blocks on recv on net_reply
.
net_reply
.Recv on net_reply
unblocks, callback passes the result back to library
Blocking call to the library returns
The result is sent back via lib_reply
channel to the main reactor
Do you have any improvements or suggestions for my flow? My main concern is callback-main reactor communication, especially the slippery situation of async reactor and sync callbacks.
Thanks in advance.
1 post - 1 participant
🏷️ rust_feed