do some programming

This commit is contained in:
Pat Hickey
2021-04-15 14:50:17 -07:00
parent 33dbd4388c
commit 9880d09f1f
3 changed files with 648 additions and 0 deletions

View File

@@ -109,3 +109,20 @@ impl WasiCtxBuilder {
self.0.build()
}
}
pub(crate) async fn asyncify<'a, F, T>(f: F) -> Result<T, Error>
where
F: FnOnce() -> Result<T, std::io::Error> + Send + 'a,
T: Send + 'static,
{
// spawn_blocking requires a 'static function, but since we await on the
// JoinHandle the lifetime of the spawn will be no longer than this function's body
let f: Box<dyn FnOnce() -> Result<T, std::io::Error> + Send + 'a> = Box::new(f);
let f = unsafe {
std::mem::transmute::<_, Box<dyn FnOnce() -> Result<T, std::io::Error> + Send + 'static>>(f)
};
match tokio::task::spawn_blocking(|| f()).await {
Ok(res) => Ok(res?),
Err(_) => panic!("spawn_blocking died"),
}
}