use std::future::Future;
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
use tokio::task::{JoinError, JoinHandle};
use tokio::runtime::{Handle};
use crate::dropper::Dropper;
#[derive(Clone, Debug)]
pub struct Worker {
pub(crate) handle: Handle,
pub(crate) _dropper: Arc<Dropper>,
}
impl Worker {
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.handle.spawn(future)
}
pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
self.handle.spawn_blocking(func)
}
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
self.handle.block_on(future)
}
}
#[cfg(test)]
mod tests {
use crate::builder::Builder;
#[test]
fn test_spawn(){
let worker = Builder::default()
.worker_threads(3)
.thread_name("1")
.build();
assert!(worker.is_ok())
}
}