scsys_core/error/
raw_error.rs1#[cfg(feature = "alloc")]
7pub type DynamicError =
10 ErrorBase<alloc::boxed::Box<dyn core::error::Error + Send + Sync + 'static>>;
11
12#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
13#[cfg_attr(
14 feature = "serde",
15 derive(serde::Serialize, serde::Deserialize),
16 serde(default, transparent, rename_all = "snake_case")
17)]
18#[repr(transparent)]
19pub struct ErrorBase<E>
20where
21 E: core::error::Error,
22{
23 inner: E,
24}
25
26impl<E> ErrorBase<E>
27where
28 E: core::error::Error,
29{
30 pub fn from_err(inner: E) -> Self {
32 Self { inner }
33 }
34 pub fn into_inner(self) -> E {
36 self.inner
37 }
38 pub const fn get(&self) -> &E {
40 &self.inner
41 }
42 #[inline]
44 pub fn get_mut(&mut self) -> &mut E {
45 &mut self.inner
46 }
47 #[inline]
50 pub fn replace(&mut self, new: E) -> E {
51 core::mem::replace(&mut self.inner, new)
52 }
53 #[inline]
55 pub fn set(&mut self, new: E) -> &mut Self {
56 self.inner = new;
57 self
58 }
59 pub fn swap(&mut self, other: &mut Self) {
61 core::mem::swap(&mut self.inner, &mut other.inner)
62 }
63 pub fn map<V, F>(self, f: F) -> ErrorBase<V>
65 where
66 F: FnOnce(E) -> V,
67 V: core::error::Error,
68 {
69 ErrorBase::from_err(f(self.inner))
70 }
71 pub fn map_mut<F>(&mut self, f: F)
73 where
74 F: FnOnce(&mut E),
75 {
76 f(&mut self.inner)
77 }
78}
79
80impl<E> From<E> for ErrorBase<E>
81where
82 E: core::error::Error,
83{
84 fn from(inner: E) -> Self {
85 Self::from_err(inner)
86 }
87}
88
89impl<'a, E> From<&'a ErrorBase<E>> for ErrorBase<E>
90where
91 E: core::error::Error + Clone,
92{
93 fn from(inner: &'a ErrorBase<E>) -> Self {
94 Self::from_err(inner.inner.clone())
95 }
96}
97
98impl<E: core::error::Error> core::error::Error for ErrorBase<E> {}
99
100impl<E> core::fmt::Display for ErrorBase<E>
101where
102 E: core::error::Error,
103{
104 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105 write!(f, "{}", self.inner)
106 }
107}