180 lines
4.4 KiB
Rust
180 lines
4.4 KiB
Rust
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<E> {
|
|
type Ok;
|
|
|
|
fn log_err(self) -> Option<Self::Ok>;
|
|
/// 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<Self::Ok>;
|
|
fn inspect_error(self, func: impl FnOnce(&E)) -> Self;
|
|
}
|
|
|
|
impl<T, E> ResultExt<E> for Result<T, E>
|
|
where
|
|
E: std::fmt::Debug,
|
|
{
|
|
type Ok = T;
|
|
|
|
#[track_caller]
|
|
fn log_err(self) -> Option<T> {
|
|
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<T> {
|
|
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>(F, log::Level, core::panic::Location<'static>);
|
|
|
|
impl<F, T, E> Future for LogErrorFuture<F>
|
|
where
|
|
F: Future<Output = Result<T, E>>,
|
|
E: std::fmt::Debug,
|
|
{
|
|
type Output = Option<T>;
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
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>(F);
|
|
|
|
impl<F, T, E> Future for UnwrapFuture<F>
|
|
where
|
|
F: Future<Output = Result<T, E>>,
|
|
E: std::fmt::Debug,
|
|
{
|
|
type Output = T;
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
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<Self>
|
|
where
|
|
Self: Sized;
|
|
|
|
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
|
where
|
|
Self: Sized;
|
|
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
where
|
|
Self: Sized;
|
|
fn unwrap(self) -> UnwrapFuture<Self>
|
|
where
|
|
Self: Sized;
|
|
}
|
|
|
|
impl<F, T, E> TryFutureExt for F
|
|
where
|
|
F: Future<Output = Result<T, E>>,
|
|
E: std::fmt::Debug,
|
|
{
|
|
#[track_caller]
|
|
fn log_err(self) -> LogErrorFuture<Self>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
let location = Location::caller();
|
|
LogErrorFuture(self, log::Level::Error, *location)
|
|
}
|
|
|
|
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
LogErrorFuture(self, log::Level::Error, location)
|
|
}
|
|
|
|
#[track_caller]
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
let location = Location::caller();
|
|
LogErrorFuture(self, log::Level::Warn, *location)
|
|
}
|
|
|
|
fn unwrap(self) -> UnwrapFuture<Self>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
UnwrapFuture(self)
|
|
}
|
|
}
|