use std::{ future::Future, panic::Location, pin::Pin, task::{Context, Poll}, }; #[macro_export] macro_rules! debug_panic { ( $($fmt_arg:tt)* ) => { if cfg!(debug_assertions) { panic!( $($fmt_arg)* ); } else { let backtrace = std::backtrace::Backtrace::capture(); log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace); } }; } pub trait ResultExt { type Ok; fn log_err(self) -> Option; /// Assert that this result should never be an error in development or tests. fn debug_assert_ok(self, reason: &str) -> Self; fn warn_on_err(self) -> Option; fn inspect_error(self, func: impl FnOnce(&E)) -> Self; } impl ResultExt for Result where E: std::fmt::Debug, { type Ok = T; #[track_caller] fn log_err(self) -> Option { match self { Ok(value) => Some(value), Err(error) => { let caller = Location::caller(); log::error!("{}:{}: {:?}", caller.file(), caller.line(), error); None } } } #[track_caller] fn debug_assert_ok(self, reason: &str) -> Self { if let Err(error) = &self { debug_panic!("{reason} - {error:?}"); } self } fn warn_on_err(self) -> Option { match self { Ok(value) => Some(value), Err(error) => { log::warn!("{:?}", error); None } } } /// https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err fn inspect_error(self, func: impl FnOnce(&E)) -> Self { if let Err(err) = &self { func(err); } self } } #[must_use] pub struct LogErrorFuture(F, log::Level, core::panic::Location<'static>); impl Future for LogErrorFuture where F: Future>, E: std::fmt::Debug, { type Output = Option; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let level = self.1; let location = self.2; let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) }; match inner.poll(cx) { Poll::Ready(output) => Poll::Ready(match output { Ok(output) => Some(output), Err(error) => { log::log!( level, "{}:{}: {:?}", location.file(), location.line(), error ); None } }), Poll::Pending => Poll::Pending, } } } pub struct UnwrapFuture(F); impl Future for UnwrapFuture where F: Future>, E: std::fmt::Debug, { type Output = T; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) }; match inner.poll(cx) { Poll::Ready(result) => Poll::Ready(result.unwrap()), Poll::Pending => Poll::Pending, } } } pub trait TryFutureExt { fn log_err(self) -> LogErrorFuture where Self: Sized; fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture where Self: Sized; fn warn_on_err(self) -> LogErrorFuture where Self: Sized; fn unwrap(self) -> UnwrapFuture where Self: Sized; } impl TryFutureExt for F where F: Future>, E: std::fmt::Debug, { #[track_caller] fn log_err(self) -> LogErrorFuture where Self: Sized, { let location = Location::caller(); LogErrorFuture(self, log::Level::Error, *location) } fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture where Self: Sized, { LogErrorFuture(self, log::Level::Error, location) } #[track_caller] fn warn_on_err(self) -> LogErrorFuture where Self: Sized, { let location = Location::caller(); LogErrorFuture(self, log::Level::Warn, *location) } fn unwrap(self) -> UnwrapFuture where Self: Sized, { UnwrapFuture(self) } }