async_local_executor/
lib.rs1#![doc = include_str!("../README.md")]
2#![allow(clippy::missing_transmute_annotations)]
4
5mod executor;
6#[cfg(test)]
7mod tests;
8mod tls;
9
10use crate::tls::executor;
11use std::sync::mpsc;
12
13pub use crate::executor::{Executor, JoinHandle, TaskHandle};
14
15#[inline]
16pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
17where
18 F: IntoFuture + 'static,
19{
20 #[cfg(feature = "tokio")]
21 let future = async_compat::Compat::new(future.into_future());
22 executor().spawn_local(future.into_future())
23}
24
25#[inline]
26pub fn block_on<F>(future: F) -> F::Output
27where
28 F: IntoFuture + 'static,
29{
30 let (tx, rx) = mpsc::channel();
31 let mut ex = Executor::new(move |task| tx.send(task).unwrap());
32 ex.run_in(|| {
33 let main = spawn_local(future);
34 loop {
35 rx.recv().unwrap().tick();
36 if let Some(result) = main.result() {
37 break result;
38 }
39 }
40 })
41}