You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
612 B
29 lines
612 B
use std::fmt;
|
|
use std::future::Future;
|
|
use std::time::Duration;
|
|
use tokio::time::{timeout, Timeout};
|
|
|
|
pub fn spawn_fallible<F, E>(future: F)
|
|
where
|
|
F: Future<Output = Result<(), E>> + Send + 'static,
|
|
E: fmt::Display,
|
|
{
|
|
tokio::spawn(async move {
|
|
if let Err(e) = future.await {
|
|
tracing::warn!("Task failed: {:#}", e);
|
|
}
|
|
});
|
|
}
|
|
|
|
pub trait FutureExt: Future + Sized {
|
|
fn timeout(self, duration: Duration) -> Timeout<Self>;
|
|
}
|
|
|
|
impl<F> FutureExt for F
|
|
where
|
|
F: Future,
|
|
{
|
|
fn timeout(self, duration: Duration) -> Timeout<F> {
|
|
timeout(duration, self)
|
|
}
|
|
}
|
|
|