util.rs

 1use smol::future::FutureExt;
 2use std::{future::Future, time::Duration};
 3
 4pub fn post_inc(value: &mut usize) -> usize {
 5    let prev = *value;
 6    *value += 1;
 7    prev
 8}
 9
10pub async fn timeout<F, T>(timeout: Duration, f: F) -> Result<T, ()>
11where
12    F: Future<Output = T>,
13{
14    let timer = async {
15        smol::Timer::after(timeout).await;
16        Err(())
17    };
18    let future = async move { Ok(f.await) };
19    timer.race(future).await
20}