futures_util/future/future/
catch_unwind.rs1use core::any::Any;
2use core::pin::Pin;
3use std::boxed::Box;
4use std::panic::{catch_unwind, AssertUnwindSafe, UnwindSafe};
5
6use futures_core::future::Future;
7use futures_core::task::{Context, Poll};
8use pin_project_lite::pin_project;
9
10pin_project! {
11 #[derive(Debug)]
13 #[must_use = "futures do nothing unless you `.await` or poll them"]
14 pub struct CatchUnwind<Fut> {
15 #[pin]
16 future: Fut,
17 }
18}
19
20impl<Fut> CatchUnwind<Fut>
21where
22 Fut: Future + UnwindSafe,
23{
24 pub(super) fn new(future: Fut) -> Self {
25 Self { future }
26 }
27}
28
29impl<Fut> Future for CatchUnwind<Fut>
30where
31 Fut: Future + UnwindSafe,
32{
33 type Output = Result<Fut::Output, Box<dyn Any + Send>>;
34
35 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36 let f = self.project().future;
37 catch_unwind(AssertUnwindSafe(|| f.poll(cx)))?.map(Ok)
38 }
39}