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}
21
22#[cfg(any(test, feature = "test-support"))]
23pub struct CwdBacktrace<'a>(pub &'a backtrace::Backtrace);
24
25#[cfg(any(test, feature = "test-support"))]
26impl<'a> std::fmt::Debug for CwdBacktrace<'a> {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 use backtrace::{BacktraceFmt, BytesOrWideString};
29
30 let cwd = std::env::current_dir().unwrap();
31 let cwd = cwd.parent().unwrap();
32 let mut print_path = |fmt: &mut std::fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
33 std::fmt::Display::fmt(&path, fmt)
34 };
35 let mut fmt = BacktraceFmt::new(f, backtrace::PrintFmt::Full, &mut print_path);
36 for frame in self.0.frames() {
37 let mut formatted_frame = fmt.frame();
38 if frame
39 .symbols()
40 .iter()
41 .any(|s| s.filename().map_or(false, |f| f.starts_with(&cwd)))
42 {
43 formatted_frame.backtrace_frame(frame)?;
44 }
45 }
46 fmt.finish()
47 }
48}