-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathspawn.rs
More file actions
24 lines (20 loc) · 800 Bytes
/
spawn.rs
File metadata and controls
24 lines (20 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copied and adapted from https://github.com/smol-rs/smol/blob/15447d6859df65fd1992f761ee46067bed62f8a5/src/spawn.rs
use std::{future::Future, panic::catch_unwind, thread};
use async_executor::Executor;
pub use async_executor::Task;
use futures_lite::future;
use once_cell::sync::Lazy;
pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
static GLOBAL: Lazy<Executor<'_>> = Lazy::new(|| {
thread::Builder::new()
.name("smol-one".into())
.spawn(|| {
loop {
catch_unwind(|| async_io::block_on(GLOBAL.run(future::pending::<()>()))).ok();
}
})
.expect("cannot spawn executor thread");
Executor::new()
});
GLOBAL.spawn(future)
}