actix-form-data/src/spawn.rs

24 lines
510 B
Rust

use log::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)
}