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 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    /// returns a new instance wrapping the given error.
30    pub fn from_err(inner: E) -> Self {
31        Self { inner }
32    }
33    /// consumes the wrapper to return the inner value.
34    pub fn into_inner(self) -> E {
35        self.inner
36    }
37    /// returns an immutable reference to the underlying error.
38    pub const fn get(&self) -> &E {
39        &self.inner
40    }
41    /// returns a mutable reference to the underlying error.
42    #[inline]
43    pub fn get_mut(&mut self) -> &mut E {
44        &mut self.inner
45    }
46    /// uses the [`replace`](core::mem::replace) method to replace and return the current error
47    /// with another.
48    #[inline]
49    pub fn replace(&mut self, new: E) -> E {
50        core::mem::replace(&mut self.inner, new)
51    }
52    /// update the inner value before returning a mutable reference to the wrapper;
53    #[inline]
54    pub fn set(&mut self, new: E) -> &mut Self {
55        self.inner = new;
56        self
57    }
58    /// swap out the inner values of two instances of [`RawError`].
59    pub fn swap(&mut self, other: &mut Self) {
60        core::mem::swap(&mut self.inner, &mut other.inner)
61    }
62    /// apply a function to the error and return a new instance with the result.
63    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    /// mutate the error using the given function
71    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}