use core::fmt;
pub(crate) fn format_helper<State, DisplayFn, DebugFn>(
state: State,
display_fn: DisplayFn,
debug_fn: DebugFn,
) -> impl fmt::Display + fmt::Debug
where
State: Copy,
for<'a, 'b> DisplayFn: Copy + Fn(State, &'a mut fmt::Formatter<'b>) -> fmt::Result,
for<'a, 'b> DebugFn: Copy + Fn(State, &'a mut fmt::Formatter<'b>) -> fmt::Result,
{
FormatHelper {
state,
display_fn,
debug_fn,
}
}
struct FormatHelper<State, DisplayFn, DebugFn> {
state: State,
display_fn: DisplayFn,
debug_fn: DebugFn,
}
impl<State, DisplayFn, DebugFn> core::fmt::Display for FormatHelper<State, DisplayFn, DebugFn>
where
State: Copy,
for<'a, 'b> DisplayFn: Fn(State, &'a mut core::fmt::Formatter<'b>) -> core::fmt::Result,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.display_fn)(self.state, f)
}
}
impl<State, DisplayFn, DebugFn> core::fmt::Debug for FormatHelper<State, DisplayFn, DebugFn>
where
State: Copy,
for<'a, 'b> DebugFn: Fn(State, &'a mut core::fmt::Formatter<'b>) -> core::fmt::Result,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.debug_fn)(self.state, f)
}
}
#[repr(transparent)]
pub(crate) struct ErrorNoSourceWrapper<T>(T);
impl<T> ErrorNoSourceWrapper<T> {
pub(crate) fn new(inner: &T) -> &Self {
let ptr: *const T = core::ptr::from_ref(inner);
let ptr: *const ErrorNoSourceWrapper<T> = ptr.cast::<ErrorNoSourceWrapper<T>>();
unsafe { &*ptr }
}
}
impl<T> core::fmt::Display for ErrorNoSourceWrapper<T>
where
T: core::fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.0, f)
}
}
impl<T> core::fmt::Debug for ErrorNoSourceWrapper<T>
where
T: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&self.0, f)
}
}
impl<T> core::error::Error for ErrorNoSourceWrapper<T> where T: core::fmt::Display + core::fmt::Debug
{}