cubecl_common/
future.rs

1use alloc::boxed::Box;
2use core::future::Future;
3
4/// Block until the [future](Future) is completed and returns the result.
5pub fn block_on<O>(fut: impl Future<Output = O>) -> O {
6    #[cfg(target_family = "wasm")]
7    {
8        super::reader::read_sync(fut)
9    }
10
11    #[cfg(all(not(target_family = "wasm"), not(feature = "std")))]
12    {
13        embassy_futures::block_on(fut)
14    }
15
16    #[cfg(all(not(target_family = "wasm"), feature = "std"))]
17    {
18        futures_lite::future::block_on(fut)
19    }
20}
21
22/// Tries to catch panics within the future.
23pub async fn catch_unwind<O>(
24    future: impl Future<Output = O>,
25) -> Result<O, Box<dyn core::any::Any + core::marker::Send>> {
26    #[cfg(all(not(target_family = "wasm"), feature = "std"))]
27    {
28        use core::panic::AssertUnwindSafe;
29        use futures_lite::FutureExt;
30        AssertUnwindSafe(future).catch_unwind().await
31    }
32
33    #[cfg(any(target_family = "wasm", not(feature = "std")))]
34    {
35        Ok(future.await)
36    }
37}