1pub type Result<T> = core::result::Result<T, Box<dyn BaseError>>;
2
3pub trait BaseError: Debug + Display {
4 fn source(&self) -> Option<&(dyn BaseError + 'static)> {
8 return None;
9 }
10
11 fn type_id(&self, _: private::Internal) -> TypeId
13 where
14 Self: 'static, {
15 return TypeId::of::<Self>();
16 }
17
18 fn backtrace(&self) -> Option<()> {
22 return None;
23 }
24}
25
26impl<'a, E: BaseError + 'a> From<E> for Box<dyn BaseError + 'a> {
27 fn from(value: E) -> Self {
30 return Box::new(value);
31 }
32}
33
34impl From<String> for Box<dyn BaseError + Send + Sync> {
35 fn from(value: String) -> Self {
38 struct StringError(String);
39
40 impl BaseError for StringError {}
41
42 impl Display for StringError {
43 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
44 Display::fmt(&self.0, f)
45 }
46 }
47
48 impl Debug for StringError {
49 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
50 Debug::fmt(&self.0, f)
51 }
52 }
53
54 return Box::new(StringError(value));
55 }
56}
57
58impl From<String> for Box<dyn BaseError> {
59 fn from(value: String) -> Self {
62 let e: Box<dyn BaseError + Send + Sync> = From::from(value);
63 let h: Box<dyn BaseError> = e;
64
65 return h;
66 }
67}
68
69impl<'a> From<&str> for Box<dyn BaseError + Send + Sync + 'a> {
70 #[inline]
72 fn from(value: &str) -> Self {
73 return From::from(String::from(value));
74 }
75}
76
77impl From<&str> for Box<dyn BaseError> {
78 fn from(value: &str) -> Self {
80 return From::from(String::from(value));
81 }
82}
83
84impl BaseError for AllocationError{}
85impl BaseError for LayoutError{}
86
87mod private {
90 #[derive(Debug)]
93 pub struct Internal;
94}
95
96#[cfg(not(feature="allocators"))]
99use std_alloc::{boxed::Box, string::String};
100
101#[cfg(feature="allocators")]
102use crate::{pointer::Unique as Box, string::String};
103
104use {
105 crate::alloc::{AllocationError, layout::LayoutError},
106 core::{
107 any::TypeId,
108 fmt::{self, Debug, Display, Formatter},
109 },
110};