use pin_utils::unsafe_pinned; use std::any::Any; use std::future::Future; use std::panic::{catch_unwind, AssertUnwindSafe, UnwindSafe}; use std::pin::Pin; use std::task::{Context, Poll}; #[derive(Debug)] pub(crate) struct CatchUnwind where F: Future, { future: F, } impl CatchUnwind where F: Future + UnwindSafe, { unsafe_pinned!(future: F); pub(crate) fn new(future: F) -> CatchUnwind { CatchUnwind { future } } } impl Future for CatchUnwind where F: Future + UnwindSafe, { type Output = Result>; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { catch_unwind(AssertUnwindSafe(|| self.future().poll(cx)))?.map(Ok) } }