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 : }
23 :
24 3 : pub fn deserialize_json_string<'de, D, T>(deserializer: D) -> Result<T, D::Error>
25 3 : where
26 3 : T: for<'de2> serde::Deserialize<'de2>,
27 3 : D: serde::Deserializer<'de>,
28 : {
29 : use serde::Deserialize;
30 3 : let s = String::deserialize(deserializer)?;
31 3 : serde_json::from_str(&s).map_err(<D::Error as serde::de::Error>::custom)
32 3 : }
|