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