/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use std::{
error::Error,
fmt,
future::Future,
io,
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use pin_project_lite::pin_project;
use smol::Timer;
/// Awaits an I/O future or times out after a duration of time.
///
/// If you want to await a non I/O future consider using
/// `timeout()` instead.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { smol::block_on(async {
/// #
/// use std::time::Duration;
/// use std::io;
///
/// io_timeout(Duration::from_secs(5), async {
/// let stdin = io::stdin();
/// let mut line = String::new();
/// let n = stdin.read_line(&mut line)?;
/// Ok(())
/// })
/// .await?;
/// #
/// # Ok(()) }) }
pub async fn io_timeout(dur: Duration, f: F) -> io::Result
where
F: Future