use std::time::Duration;
use futures::future::{self, Either, Future};
use crate::error::{Error, ErrorKind, Result};
pub async fn timeout<O>(dur: Option<Duration>, f: impl Future<Output = O> + Send) -> Result<O> {
futures::pin_mut!(f);
if let Some(dur) = dur {
match future::select(f, smol::Timer::after(dur)).await {
Either::Left((out, _)) => Ok(out),
Either::Right(_) => Err(Error::new("Timed out.").set_kind(ErrorKind::TimedOut)),
}
} else {
Ok(f.await)
}
}
pub async fn sleep(dur: Duration) {
smol::Timer::after(dur).await;
}