use std::{ future::Future, marker::PhantomData, pin::Pin, task::{Context, Poll}, }; pub trait Try: Sized { fn tri(f: F) -> Result<(), Self> where F: FnOnce() -> Result<(), Self>; #[allow(dead_code)] fn tria(f: F) -> Tria where F: Future>; } impl Try for E { fn tri(f: F) -> Result<(), Self> where F: FnOnce() -> Result<(), Self>, { f() } fn tria(f: F) -> Tria where F: Future>, { Tria { f, _phantom: Default::default(), } } } pub struct Tria { f: F, _phantom: PhantomData, } impl Future for Tria where F: Future>, { type Output = Result<(), E>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { unsafe { Pin::new_unchecked(&mut Pin::get_unchecked_mut(self).f).poll(cx) } } }