rsiot/components/shared_tasks/
change_mpsc_msg.rs1use tokio::sync::mpsc::{Receiver, Sender};
4
5pub struct ChangeMpscMsg<TInput, TOutput> {
7 pub input: Receiver<TInput>,
9 pub output: Sender<TOutput>,
11 pub fn_change: fn(TInput) -> TOutput,
13}
14
15impl<TInput, TOutput> ChangeMpscMsg<TInput, TOutput> {
16 pub async fn spawn(mut self) -> Result<(), Error> {
18 while let Some(input_msg) = self.input.recv().await {
19 let output_msg = (self.fn_change)(input_msg);
20 self.output
21 .send(output_msg)
22 .await
23 .map_err(|e| Error::TokioSyncMpsc(e.to_string()))?;
24 }
25 Ok(())
26 }
27}
28
29#[allow(missing_docs)]
30#[derive(thiserror::Error, Debug)]
31pub enum Error {
32 #[error("{0}")]
33 TokioSyncMpsc(String),
34}