use core::{convert::Infallible, fmt};
use crate::{BoxError, std::Box};
pub struct OpaqueError(BoxError);
impl OpaqueError {
#[inline(always)]
pub(super) fn from_box_error(e: impl Into<BoxError>) -> Self {
Self(e.into())
}
#[inline(always)]
pub fn from_static_str(e: &'static str) -> Self {
Self(Box::new(StaticStrError(e)))
}
#[inline(always)]
pub fn into_box_error(self) -> BoxError {
self.0
}
}
impl fmt::Debug for OpaqueError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for OpaqueError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl core::error::Error for OpaqueError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Some(self.0.as_ref())
}
}
impl From<Infallible> for OpaqueError {
fn from(infallible: Infallible) -> Self {
match infallible {}
}
}
#[derive(Clone, Copy)]
struct StaticStrError(&'static str);
impl fmt::Debug for StaticStrError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for StaticStrError {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl core::error::Error for StaticStrError {}