base/
error.rs

1pub type Result<T> = core::result::Result<T, Box<dyn BaseError>>;
2
3pub trait BaseError: Debug + Display {
4   /// The lower-level source of error, if any.
5   ///
6   /// TODO: provide examples.
7   fn source(&self) -> Option<&(dyn BaseError + 'static)> {
8      return None;
9   }
10
11   /// Gets the [`TypeId`][core::any::TypeId] of `self`.
12   fn type_id(&self, _: private::Internal) -> TypeId
13   where
14      Self: 'static, {
15      return TypeId::of::<Self>();
16   }
17
18   /// Returns a stack backtrace.
19   ///
20   /// TODO: provide examples.
21   fn backtrace(&self) -> Option<()> {
22      return None;
23   }
24}
25
26impl<'a, E: BaseError + 'a> From<E> for Box<dyn BaseError + 'a> {
27   /// Converts a type of [`BaseError`] + [`Send`] + [`Sync`] into a unique pointer of
28   /// dyn [`BaseError`] + [`Send`] + [`Sync`].
29   fn from(value: E) -> Self {
30      return Box::new(value);
31   }
32}
33
34impl From<String> for Box<dyn BaseError + Send + Sync> {
35   /// Converts a [`String`] into a unique pointer of
36   /// dyn [`BaseError`] + [`Send`] + [`Sync`].
37   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   /// Converts a [`String`] into a unique pointer of
60   /// dyn [`BaseError`].
61   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   /// Converts a [`str`][prim@str] into a unique pointer of dyn [`BaseError`] + [`Send`] + [`Sync`].
71   #[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   /// Converts a [`str`][prim@str] into a unique pointer of dyn [`BaseError`].
79   fn from(value: &str) -> Self {
80      return From::from(String::from(value));
81   }
82}
83
84impl BaseError for AllocationError{}
85impl BaseError for LayoutError{}
86
87// MODULES //
88
89mod private {
90   /// A hack to prevent `type_id` from being overridden by `BaseError`
91   /// implementations, since that may enable unsound downcasting.
92   #[derive(Debug)]
93   pub struct Internal;
94}
95
96// IMPORTS //
97
98#[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};