use std::pin::pin; use futures::future::{Either, select}; use tokio_util::sync::CancellationToken; pub async fn run_until_cancelled( f: F, cancellation_token: &CancellationToken, ) -> Option { run_until(f, cancellation_token.cancelled()).await.ok() } /// Runs the future `f` unless interrupted by future `condition`. pub async fn run_until( f: F1, condition: F2, ) -> Result { match select(pin!(f), pin!(condition)).await { Either::Left((f1, _)) => Ok(f1), Either::Right((f2, _)) => Err(f2), } } pub fn deserialize_json_string<'de, D, T>(deserializer: D) -> Result where T: for<'de2> serde::Deserialize<'de2>, D: serde::Deserializer<'de>, { use serde::Deserialize; let s = String::deserialize(deserializer)?; serde_json::from_str(&s).map_err(::custom) }