rsiot/components/shared_tasks/
mpsc_to_msgbus.rs1use tokio::sync::mpsc;
4
5use crate::{
6    executor::{CmpInOut, ComponentError},
7    message::{Message, MsgDataBound},
8};
9
10pub struct MpscToMsgBus<TMsg>
12where
13    TMsg: MsgDataBound,
14{
15    pub input: mpsc::Receiver<Message<TMsg>>,
17
18    pub msg_bus: CmpInOut<TMsg>,
20}
21
22impl<TMsg> MpscToMsgBus<TMsg>
23where
24    TMsg: MsgDataBound,
25{
26    pub async fn spawn(mut self) -> Result<(), Error> {
28        while let Some(msg) = self.input.recv().await {
29            self.msg_bus.send_output(msg).await?;
30        }
31        Ok(())
32    }
33}
34
35#[allow(missing_docs)]
36#[derive(thiserror::Error, Debug)]
37pub enum Error {
38    #[error(transparent)]
39    Component(#[from] ComponentError),
40}