pub trait SyncSend<T> {
fn send_sync(&self, item: T) -> Result<(), anyhow::Error>;
}
#[cfg(feature = "tokio")]
pub use tokio::sync::mpsc::{
UnboundedReceiver as Receiver, UnboundedSender as Sender, unbounded_channel as channel,
};
#[cfg(feature = "tokio")]
impl<T> SyncSend<T> for tokio::sync::mpsc::UnboundedSender<T> {
fn send_sync(&self, item: T) -> Result<(), anyhow::Error> {
self.send(item)
.map_err(|_| anyhow::anyhow!("Channel closed"))
}
}
#[cfg(all(feature = "async-std", not(feature = "tokio")))]
pub use async_std::channel::{Receiver, Sender, unbounded as channel};
#[cfg(all(feature = "async-std", not(feature = "tokio")))]
impl<T> SyncSend<T> for async_std::channel::Sender<T> {
fn send_sync(&self, item: T) -> Result<(), anyhow::Error> {
self.try_send(item).map_err(|e| match e {
async_std::channel::TrySendError::Full(_) => anyhow::anyhow!("Channel full"),
async_std::channel::TrySendError::Closed(_) => anyhow::anyhow!("Channel closed"),
})
}
}
#[cfg(not(any(feature = "tokio", feature = "async-std")))]
pub use flume::{Receiver, Sender, unbounded as channel};
#[cfg(not(any(feature = "tokio", feature = "async-std")))]
impl<T> SyncSend<T> for flume::Sender<T> {
fn send_sync(&self, item: T) -> Result<(), anyhow::Error> {
self.try_send(item).map_err(|e| match e {
flume::TrySendError::Full(_) => anyhow::anyhow!("Channel full"),
flume::TrySendError::Disconnected(_) => anyhow::anyhow!("Channel disconnected"),
})
}
}