zero_trust_rps/common/client/channel/
sender.rs1use std::{fmt::Debug, future::Future};
2
3use tokio::sync::mpsc;
4use tokio::sync::mpsc::{Sender, UnboundedSender};
5
6#[derive(thiserror::Error, Debug)]
7pub enum SendError {
8 #[error("Channel closed")]
9 ChannelClosed,
10}
11
12impl<T: Debug> From<mpsc::error::SendError<T>> for SendError {
13 fn from(value: mpsc::error::SendError<T>) -> Self {
14 log::debug!("Tried to send {:?}, but {value}", value.0);
15 SendError::ChannelClosed
16 }
17}
18
19pub trait AsyncChannelSender<T: Send>: Send {
20 fn send(&self, value: T) -> impl Future<Output = Result<(), SendError>> + Send;
21}
22
23impl<TFrom: Send, TTo: From<TFrom> + Debug + Send> AsyncChannelSender<TFrom>
24 for UnboundedSender<TTo>
25{
26 async fn send(&self, value: TFrom) -> Result<(), SendError> {
27 Ok(UnboundedSender::<TTo>::send(self, value.into())?)
28 }
29}
30
31impl<TFrom: Send, TTo: From<TFrom> + Debug + Send> AsyncChannelSender<TFrom> for Sender<TTo> {
32 async fn send(&self, value: TFrom) -> Result<(), SendError> {
33 Ok(Sender::<TTo>::send(self, value.into()).await?)
34 }
35}
36
37impl<T: Send> AsyncChannelSender<T> for () {
38 async fn send(&self, _: T) -> Result<(), SendError> {
39 Ok(())
40 }
41}