scsys_core/error/
raw_error.rs

1/*
2    Appellation: raw_error <module>
3    Contrib: @FL03
4*/
5
6#[cfg(feature = "alloc")]
7/// A type alias for a [`Err`] whose generic type is a [`Box`] of a trait object
8/// implementing [`core::error::Error`].
9pub 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    /// returns a new instance wrapping the given error.
31    pub fn from_err(inner: E) -> Self {
32        Self { inner }
33    }
34    /// consumes the wrapper to return the inner value.
35    pub fn into_inner(self) -> E {
36        self.inner
37    }
38    /// returns an immutable reference to the underlying error.
39    pub const fn get(&self) -> &E {
40        &self.inner
41    }
42    /// returns a mutable reference to the underlying error.
43    #[inline]
44    pub fn get_mut(&mut self) -> &mut E {
45        &mut self.inner
46    }
47    /// uses the [`replace`](core::mem::replace) method to replace and return the current error
48    /// with another.
49    #[inline]
50    pub fn replace(&mut self, new: E) -> E {
51        core::mem::replace(&mut self.inner, new)
52    }
53    /// update the inner value before returning a mutable reference to the wrapper;
54    #[inline]
55    pub fn set(&mut self, new: E) -> &mut Self {
56        self.inner = new;
57        self
58    }
59    /// swap out the inner values of two instances of [`RawError`].
60    pub fn swap(&mut self, other: &mut Self) {
61        core::mem::swap(&mut self.inner, &mut other.inner)
62    }
63    /// apply a function to the error and return a new instance with the result.
64    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    /// mutate the error using the given function
72    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}