Mirror of https://github.com/roostorg/osprey
github.com/roostorg/osprey
1use crate::future_utils::time::timeout_at;
2use anyhow::{Error, Result};
3use futures::TryFutureExt;
4use std::future::Future;
5use std::marker::Unpin;
6use std::time::Instant;
7use tokio::sync::oneshot;
8
9/// Creates a oneshot that will time out at a deadline.
10///
11/// # Arguments
12///
13/// * deadline - the time to timeout at
14pub fn channel_with_deadline<T: Unpin>(
15 deadline: Instant,
16) -> (oneshot::Sender<Result<T>>, impl Future<Output = Result<T>>) {
17 let (tx, rx) = oneshot::channel();
18 let awaitable = async move {
19 let rx = rx.map_err(Error::from);
20 timeout_at(deadline, rx).await.unwrap_or_else(Err)
21 };
22 (tx, awaitable)
23}