1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use futures::{ executor::{block_on as block_on_executor, ThreadPool}, future::{Future, RemoteHandle}, task::{FutureObj, Spawn, SpawnError, SpawnExt}, }; lazy_static! { static ref THREAD_POOL: ThreadPool = ThreadPool::new().unwrap(); } #[derive(Default)] pub struct ThreadPoolAsyncManager {} impl Spawn for ThreadPoolAsyncManager { fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> { THREAD_POOL.spawn_obj(future) } } pub fn spawn<Fut>(future: Fut) -> Result<(), SpawnError> where Fut: Future<Output = ()> + Send + 'static, { ThreadPoolAsyncManager::default().spawn(future) } pub fn spawn_with_handle<Fut>(future: Fut) -> Result<RemoteHandle<Fut::Output>, SpawnError> where Fut: Future + Send + 'static, Fut::Output: Send, { ThreadPoolAsyncManager::default().spawn_with_handle(future) } pub fn block_on<F>(f: F) -> <F as Future>::Output where F: Future, { block_on_executor(f) }