#[cfg(all(feature = "bounded-channel", feature = "unbounded-channel"))]
compile_error!(
"xan-actor: enable exactly one of `bounded-channel` or `unbounded-channel`, not both"
);
#[cfg(not(any(feature = "bounded-channel", feature = "unbounded-channel")))]
compile_error!(
"xan-actor: enable one of `bounded-channel` (default) or `unbounded-channel`"
);
pub use tokio::sync::mpsc::error::SendError;
#[cfg(feature = "bounded-channel")]
pub type Sender<T> = tokio::sync::mpsc::Sender<T>;
#[cfg(feature = "bounded-channel")]
pub type Receiver<T> = tokio::sync::mpsc::Receiver<T>;
#[cfg(feature = "unbounded-channel")]
pub type Sender<T> = tokio::sync::mpsc::UnboundedSender<T>;
#[cfg(feature = "unbounded-channel")]
pub type Receiver<T> = tokio::sync::mpsc::UnboundedReceiver<T>;
#[cfg(feature = "bounded-channel")]
pub fn channel<T>(size: usize) -> (Sender<T>, Receiver<T>) {
tokio::sync::mpsc::channel(size)
}
#[cfg(feature = "unbounded-channel")]
pub fn channel<T>(_size: usize) -> (Sender<T>, Receiver<T>) {
tokio::sync::mpsc::unbounded_channel()
}
#[cfg(feature = "bounded-channel")]
pub async fn send<T>(tx: &Sender<T>, value: T) -> Result<(), SendError<T>> {
tx.send(value).await
}
#[cfg(feature = "unbounded-channel")]
pub async fn send<T>(tx: &Sender<T>, value: T) -> Result<(), SendError<T>> {
tx.send(value)
}
#[async_trait::async_trait]
pub trait SendAsync<T: Send> {
async fn send_async(&self, value: T) -> Result<(), SendError<T>>;
}
#[cfg(feature = "bounded-channel")]
#[async_trait::async_trait]
impl<T: Send> SendAsync<T> for tokio::sync::mpsc::Sender<T> {
async fn send_async(&self, value: T) -> Result<(), SendError<T>> {
self.send(value).await
}
}
#[cfg(feature = "unbounded-channel")]
#[async_trait::async_trait]
impl<T: Send> SendAsync<T> for tokio::sync::mpsc::UnboundedSender<T> {
async fn send_async(&self, value: T) -> Result<(), SendError<T>> {
self.send(value)
}
}
#[cfg(feature = "bounded-channel")]
pub fn try_send<T>(tx: &Sender<T>, value: T) -> Result<(), String> {
tx.try_send(value).map_err(|e| format!("{:?}", e))
}
#[cfg(feature = "unbounded-channel")]
pub fn try_send<T>(tx: &Sender<T>, value: T) -> Result<(), String> {
tx.send(value).map_err(|e| format!("{:?}", e))
}