Line data Source code
1 : use std::pin::pin;
2 :
3 : use futures::future::{Either, select};
4 : use tokio_util::sync::CancellationToken;
5 :
6 0 : pub async fn run_until_cancelled<F: Future>(
7 0 : f: F,
8 0 : cancellation_token: &CancellationToken,
9 0 : ) -> Option<F::Output> {
10 0 : run_until(f, cancellation_token.cancelled()).await.ok()
11 0 : }
12 :
13 : /// Runs the future `f` unless interrupted by future `condition`.
14 0 : pub async fn run_until<F1: Future, F2: Future>(
15 0 : f: F1,
16 0 : condition: F2,
17 0 : ) -> Result<F1::Output, F2::Output> {
18 0 : match select(pin!(f), pin!(condition)).await {
19 0 : Either::Left((f1, _)) => Ok(f1),
20 0 : Either::Right((f2, _)) => Err(f2),
21 : }
22 0 : }
|