feat: implement asynchronous channel support with tokio integration

This commit is contained in:
Callum Leslie 2026-03-04 20:51:20 +00:00
parent 56d0889b37
commit 863ca61215
13 changed files with 385 additions and 33 deletions

7
src/asynchronous/mod.rs Normal file
View file

@ -0,0 +1,7 @@
pub mod traits;
#[cfg(feature = "async-tokio")]
pub mod tokio;
#[cfg(feature = "async-tokio")]
pub use tokio::TokioSuck;

113
src/asynchronous/tokio.rs Normal file
View file

@ -0,0 +1,113 @@
use std::sync::Arc;
use arc_swap::ArcSwap;
use async_trait::async_trait;
use tokio::sync::{Mutex, mpsc};
use crate::asynchronous::traits::{
AsyncChannelReceiver, AsyncChannelSender, AsyncChannelType, ChannelError,
};
use crate::types;
type TokioSucker<T> =
crate::AsyncSucker<T, TokioSender<types::Request>, TokioReceiver<types::Response<T>>>;
type TokioSourcer<T> =
crate::AsyncSourcer<T, TokioReceiver<types::Request>, TokioSender<types::Response<T>>>;
pub struct TokioSender<T>(mpsc::UnboundedSender<T>);
pub struct TokioReceiver<T>(Mutex<mpsc::UnboundedReceiver<T>>);
#[async_trait]
impl<T: Send + 'static> AsyncChannelSender<T> for TokioSender<T> {
async fn send(&self, msg: T) -> Result<(), ChannelError> {
self.0
.send(msg)
.map_err(|_| ChannelError::ProducerDisconnected)
}
}
#[async_trait]
impl<T: Send + 'static> AsyncChannelReceiver<T> for TokioReceiver<T> {
async fn recv(&self) -> Result<T, ChannelError> {
let mut receiver = self.0.lock().await;
receiver
.recv()
.await
.ok_or(ChannelError::ProducerDisconnected)
}
}
pub struct TokioChannel;
impl AsyncChannelType for TokioChannel {
type Sender<T: Send + 'static> = TokioSender<T>;
type Receiver<T: Send + 'static> = TokioReceiver<T>;
fn create_request_channel() -> (Self::Sender<types::Request>, Self::Receiver<types::Request>) {
let (tx, rx) = mpsc::unbounded_channel();
(TokioSender(tx), TokioReceiver(Mutex::new(rx)))
}
fn create_response_channel<T: Send + 'static>() -> (
Self::Sender<types::Response<T>>,
Self::Receiver<types::Response<T>>,
) {
let (tx, rx) = mpsc::unbounded_channel();
(TokioSender(tx), TokioReceiver(Mutex::new(rx)))
}
}
pub struct TokioSuck<T> {
_phantom: std::marker::PhantomData<T>,
}
impl<T> TokioSuck<T> {
pub fn pair() -> (TokioSucker<T>, TokioSourcer<T>)
where
T: Clone + Send + 'static,
{
let (request_tx, request_rx) = TokioChannel::create_request_channel();
let (response_tx, response_rx) = TokioChannel::create_response_channel::<T>();
let state = ArcSwap::new(Arc::new(crate::types::ValueSource::None));
let sucker = crate::AsyncSucker::new(request_tx, response_rx);
let sourcer = crate::AsyncSourcer::new(request_rx, response_tx, state);
(sucker, sourcer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Error;
#[tokio::test]
async fn test_pre_computed_value() {
let (sucker, sourcer) = TokioSuck::<i32>::pair();
let producer = tokio::spawn(async move {
sourcer.set_static(42).unwrap();
sourcer.run().await.unwrap();
});
let value = sucker.get().await.unwrap();
assert_eq!(value, 42);
sucker.close().await.unwrap();
producer.await.unwrap();
}
#[tokio::test]
async fn test_no_source_error() {
let (sucker, sourcer) = TokioSuck::<i32>::pair();
let producer = tokio::spawn(async move {
sourcer.run().await.unwrap();
});
let result = sucker.get().await;
assert!(matches!(result, Err(Error::NoSource)));
sucker.close().await.unwrap();
producer.await.unwrap();
}
}

View file

@ -0,0 +1,27 @@
use async_trait::async_trait;
pub use crate::error::Error as ChannelError;
#[async_trait]
pub trait AsyncChannelSender<T>: Send + Sync {
async fn send(&self, msg: T) -> Result<(), ChannelError>;
}
#[async_trait]
pub trait AsyncChannelReceiver<T>: Send + Sync {
async fn recv(&self) -> Result<T, ChannelError>;
}
pub trait AsyncChannelType {
type Sender<T: Send + 'static>: AsyncChannelSender<T>;
type Receiver<T: Send + 'static>: AsyncChannelReceiver<T>;
fn create_request_channel() -> (
Self::Sender<crate::types::Request>,
Self::Receiver<crate::types::Request>,
);
fn create_response_channel<T: Send + 'static>() -> (
Self::Sender<crate::types::Response<T>>,
Self::Receiver<crate::types::Response<T>>,
);
}