pub type Result<T> = core::result::Result<T, Box<dyn BaseError>>;
pub trait BaseError: Debug + Display {
fn source(&self) -> Option<&(dyn BaseError + 'static)> {
return None;
}
fn type_id(&self, _: private::Internal) -> TypeId
where
Self: 'static, {
return TypeId::of::<Self>();
}
fn backtrace(&self) -> Option<()> {
return None;
}
}
impl<'a, E: BaseError + 'a> From<E> for Box<dyn BaseError + 'a> {
fn from(value: E) -> Self {
return Box::new(value);
}
}
impl From<String> for Box<dyn BaseError + Send + Sync> {
fn from(value: String) -> Self {
struct StringError(String);
impl BaseError for StringError {}
impl Display for StringError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl Debug for StringError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
return Box::new(StringError(value));
}
}
impl From<String> for Box<dyn BaseError> {
fn from(value: String) -> Self {
let e: Box<dyn BaseError + Send + Sync> = From::from(value);
let h: Box<dyn BaseError> = e;
return h;
}
}
impl<'a> From<&str> for Box<dyn BaseError + Send + Sync + 'a> {
#[inline]
fn from(value: &str) -> Self {
return From::from(String::from(value));
}
}
impl From<&str> for Box<dyn BaseError> {
fn from(value: &str) -> Self {
return From::from(String::from(value));
}
}
impl BaseError for AllocationError{}
impl BaseError for LayoutError{}
mod private {
#[derive(Debug)]
pub struct Internal;
}
#[cfg(not(feature="allocators"))]
use std_alloc::{boxed::Box, string::String};
#[cfg(feature="allocators")]
use crate::{pointer::Unique as Box, string::String};
use {
crate::alloc::{AllocationError, layout::LayoutError},
core::{
any::TypeId,
fmt::{self, Debug, Display, Formatter},
},
};