error_repr/kind.rs
1use super::RawOsError;
2
3/// Base trait for the kind of [`Error`][crate::Error]s.
4///
5/// Error Kinds are simple types (usually flat enums) that describes the overall category of an error (with the payload being encoded in the [`Error`][crate::Error] type instead).
6/// Because [`ErrorKind`] implementors are simple, they are expected to implement [`Copy`], as well as [`Debug`][core::fmt::Debug] and [`Display`][core::fmt::Display].
7pub trait ErrorKind: core::fmt::Debug + core::fmt::Display + Copy {
8 /// A Constant for the [`ErrorKind`] that is an "Other" value
9 const OTHER: Self;
10
11 /// Returns a value of the [`ErrorKind`] that represents an uncategorized Error.
12 /// This should normally not be matachable outside of the crate that defines the [`ErrorKind`]
13 fn uncategorized() -> Self;
14}
15
16/// Trait for [`ErrorKind`]s that can be created from a [`RawOsError`]
17pub trait FromRawOsError: ErrorKind {
18 /// Returns an Error value that corresponds to the OS error code.
19 ///
20 /// ## Warning
21 /// Implementation of this function is target specific. OS Errors are inherently per-OS and this must be taken into account when implemented.
22 fn from_raw_os_error(raw: RawOsError) -> Self;
23}
24
25/// Trait for [`ErrorKind`]s that can be converted from [`std::io::ErrorKind`]. Implementing this trait allows converting from [`std::io::Error`] to [`Error<Self>`][crate::Error]
26#[cfg(feature = "std")]
27pub trait FromIoKind: ErrorKind {
28 /// Produces a value of the [`ErrorKind`] that corresponds to `kind`
29 ///
30 /// Except when mapping to an uncategorized error, this method is expected to be consistent with [`IntoIoKind::into_io_error_kind`] and [`FromRawOsError::from_raw_os_error`].
31 /// Surprising results from this crate may occur otherwise.
32 fn from_io_error_kind(kind: std::io::ErrorKind) -> Self;
33}
34
35/// Trait for [`ErrorKind`]s that can be converted to [`std::io::ErrorKind`]. Implementing this trait allows converting to [`std::io::Error`] from [`Error<Self>`][crate::Error]
36#[cfg(feature = "std")]
37pub trait IntoIoKind: ErrorKind {
38 /// Produces a value of [`std::io::ErrorKind`] that corresponds to `self`.
39 ///
40 /// Except when mapping to an uncategorized error, this method is expected to be consistent with [`FromIoKind::from_io_error_kind`] and [`FromRawOsError::from_raw_os_error`].
41 /// Surprising results from this crate may occur otherwise.
42 fn into_io_error_kind(self) -> std::io::ErrorKind;
43}
44
45#[cfg(feature = "std")]
46mod std_impls {
47 use super::*;
48 impl ErrorKind for std::io::ErrorKind {
49 const OTHER: Self = Self::Other;
50
51 fn uncategorized() -> Self {
52 Self::Other // We can't emit a properly uncategorized error here, so...
53 }
54 }
55
56 impl FromIoKind for std::io::ErrorKind {
57 fn from_io_error_kind(kind: std::io::ErrorKind) -> Self {
58 kind
59 }
60 }
61
62 impl IntoIoKind for std::io::ErrorKind {
63 fn into_io_error_kind(self) -> std::io::ErrorKind {
64 self
65 }
66 }
67
68 impl FromRawOsError for std::io::ErrorKind {
69 fn from_raw_os_error(raw: RawOsError) -> Self {
70 std::io::Error::from_raw_os_error(raw).kind() // Lol
71 }
72 }
73}