use crate::{error::TaqResult, job, Handle, Job, Task};
use std::sync::mpsc;
use tokio::sync::oneshot;
pub trait HandleExt<A, R> {
fn recv(&self, func: Job<A, R>) -> TaqResult<oneshot::Receiver<R>>;
fn blocking_recv(&self, func: Job<A, R>) -> TaqResult<mpsc::Receiver<R>>;
}
#[doc(hidden)]
impl<A: Task + Send + 'static, R: Send + 'static> HandleExt<A, R> for Handle<A> {
fn recv(&self, func: Job<A, R>) -> TaqResult<oneshot::Receiver<R>> {
let (tx, rx) = oneshot::channel();
self.run(job!(|t| let _ = tx.send(func.with(t).await)))?;
Ok(rx)
}
fn blocking_recv(&self, func: Job<A, R>) -> TaqResult<mpsc::Receiver<R>> {
let (tx, rx) = mpsc::sync_channel(1);
self.run(job!(|t| let _ = tx.send(func.with(t).await)))?;
Ok(rx)
}
}