actix-form-data/src/spawn.rs
2020-06-14 13:39:12 -05:00

24 lines
514 B
Rust

use tracing::error;
use std::future::Future;
use tokio::sync::oneshot::channel;
#[derive(Debug, thiserror::Error)]
#[error("Task panicked")]
pub(crate) struct Canceled;
pub(crate) async fn spawn<F, T>(f: F) -> Result<T, Canceled>
where
F: Future<Output = T> + 'static,
T: 'static,
{
let (tx, rx) = channel();
actix_rt::spawn(async move {
if let Err(_) = tx.send(f.await) {
error!("rx dropped (this shouldn't happen)");
}
});
rx.await.map_err(|_| Canceled)
}