scsys_core/error/
std_error.rs

1/*
2    Appellation: std_error <module>
3    Contrib: @FL03
4*/
5#![cfg(feature = "alloc")]
6use crate::error::ErrorKind;
7use alloc::string::{String, ToString};
8
9/// The [`StdError`] type is a generic error type that is generic over the error kind; this
10/// enables us to distinguish between distinctly different error types.
11pub struct StdError<K: ErrorKind = String> {
12    pub(crate) kind: K,
13    pub(crate) message: String,
14}
15
16impl<K> StdError<K>
17where
18    K: ErrorKind,
19{
20    /// returns a reference to the kind of error
21    pub fn kind(&self) -> &K {
22        &self.kind
23    }
24    /// returns a mutable reference to the kind of error
25    pub fn kind_mut(&mut self) -> &mut K {
26        &mut self.kind
27    }
28    /// returns a reference to the message
29    pub fn message(&self) -> &str {
30        &self.message
31    }
32    /// update the kind before returning a mutable reference to the current instance.
33    pub fn set_kind(&mut self, value: K) -> &mut Self {
34        self.kind = value;
35        self
36    }
37    /// update the message before returning a mutable reference to the current instance.
38    pub fn set_message<V: ToString>(&mut self, value: V) -> &mut Self {
39        self.message = value.to_string();
40        self
41    }
42    /// consumes the current instance to create another with the given kind
43    pub fn with_kind(self, kind: K) -> Self {
44        Self { kind, ..self }
45    }
46    /// consumes the current instance to create another with the given message.
47    pub fn with_message(self, message: String) -> Self {
48        Self { message, ..self }
49    }
50}
51
52unsafe impl<K: ErrorKind> Send for StdError<K> {}
53
54unsafe impl<K: ErrorKind> Sync for StdError<K> {}