use crate::error::{TaqError, TaqResult};
use crate::job::Job;
use crate::task::Task;
use tokio::sync::mpsc;
mod ext;
pub use ext::HandleExt;
type Tx<T> = mpsc::UnboundedSender<Job<T, ()>>;
pub struct Handle<A: Task>(Tx<A>);
impl<A> Handle<A>
where
A: Task + Send + 'static,
{
pub(crate) fn new(tx: Tx<A>) -> Self {
Self(tx)
}
pub fn run(&self, func: Job<A, ()>) -> TaqResult<()> {
self.0.send(func).map_err(|_| TaqError::SendToClosed)?;
Ok(())
}
}
impl<T: Task> Clone for Handle<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}