mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-11-17 08:16:12 +00:00
20 lines
377 B
Rust
20 lines
377 B
Rust
use std::{
|
|
future::Future,
|
|
io::{self, Error},
|
|
time::Duration,
|
|
};
|
|
|
|
use tokio::time;
|
|
|
|
pub async fn try_timeout<T, E, F>(fut: F, timeout: Option<Duration>) -> io::Result<T>
|
|
where
|
|
F: Future<Output = Result<T, E>>,
|
|
Error: From<E>,
|
|
{
|
|
match timeout {
|
|
Some(t) => time::timeout(t, fut).await?,
|
|
None => fut.await,
|
|
}
|
|
.map_err(From::from)
|
|
}
|