use std::{
future::Future,
time::{Duration, Instant},
};
pub trait AsyncTimeout<F: Future + Send>:
Future<Output = Result<F::Output, Elapsed>> + Send
{
fn timeout(timeout: Duration, fut: F) -> Self
where
Self: Sized;
fn timeout_at(deadline: Instant, fut: F) -> Self
where
Self: Sized;
}
pub trait AsyncLocalTimeout<F: Future>: Future<Output = Result<F::Output, Elapsed>> {
fn timeout_local(timeout: Duration, fut: F) -> Self
where
Self: Sized;
fn timeout_local_at(deadline: Instant, fut: F) -> Self
where
Self: Sized;
}
#[derive(Debug, PartialEq, Eq)]
pub struct Elapsed;
impl core::fmt::Display for Elapsed {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "deadline has elapsed")
}
}
impl std::error::Error for Elapsed {}
impl From<Elapsed> for std::io::Error {
fn from(_: Elapsed) -> Self {
std::io::ErrorKind::TimedOut.into()
}
}
#[cfg(feature = "tokio")]
impl From<::tokio::time::error::Elapsed> for Elapsed {
fn from(_: ::tokio::time::error::Elapsed) -> Self {
Elapsed
}
}
#[test]
fn test_elapsed_error() {
assert_eq!(Elapsed.to_string(), "deadline has elapsed");
let _: std::io::Error = Elapsed.into();
}