zed/gpui/src/util.rs
Antonio Scandurra 37444acc9c Time out condition after 200ms and add basic unit tests for it
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-04-20 17:21:29 +02:00

20 lines
441 B
Rust

use smol::future::FutureExt;
use std::{future::Future, time::Duration};
pub fn post_inc(value: &mut usize) -> usize {
let prev = *value;
*value += 1;
prev
}
pub async fn timeout<F, T>(timeout: Duration, f: F) -> Result<T, ()>
where
F: Future<Output = T>,
{
let timer = async {
smol::Timer::after(timeout).await;
Err(())
};
let future = async move { Ok(f.await) };
timer.race(future).await
}