1#[cfg(feature = "alloc")]
2use alloc::boxed::Box;
3use core::{convert::Infallible, fmt};
4#[cfg(feature = "std")]
5use std::io;
6
7#[cfg(not(feature = "std"))]
8use core2::io;
9
10pub type Result<T> = core::result::Result<T, Error>;
12
13#[derive(Debug)]
14#[non_exhaustive]
15#[allow(missing_docs)]
16pub enum Error {
17 Io(io::Error),
18 #[cfg(feature = "alloc")]
19 FromUtf8(alloc::string::FromUtf8Error),
20 #[cfg(feature = "alloc")]
21 Nul(alloc::ffi::NulError),
22 TryFromInt(core::num::TryFromIntError),
23 Borrow(core::cell::BorrowError),
24 Discriminant,
25 TagConvert,
26 #[cfg(feature = "std")]
27 Poison,
28 Underrun {
29 read_bits: u64,
30 available_bits: u64,
31 },
32 EncodeSkipped,
33 Magic(&'static [u8]),
34 #[cfg(feature = "alloc")]
35 Boxed(Box<dyn core::error::Error + Send + Sync>),
37 Other(&'static str),
39}
40
41impl fmt::Display for Error {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match self {
44 Self::Io(e) => write!(f, "{e}"),
45 #[cfg(feature = "alloc")]
46 Self::FromUtf8(e) => write!(f, "{e}"),
47 #[cfg(feature = "alloc")]
48 Self::Nul(e) => write!(f, "{e}"),
49 Self::TryFromInt(e) => write!(f, "{e}"),
50 Self::Borrow(e) => write!(f, "{e}"),
51 Self::Discriminant => write!(f, "unknown enum discriminant"),
52 Self::TagConvert => write!(f, "failed to convert tag"),
53 #[cfg(feature = "std")]
54 Self::Poison => write!(f, "poisoned lock"),
55 Self::Magic(expected) => write!(f, "magic mismatch. Expected: {expected:?}."),
56 Self::Underrun {
57 read_bits: read,
58 available_bits: available,
59 } => {
60 write!(f, "buffer underrun: read {read} of {available} bits")
61 }
62 Self::EncodeSkipped => write!(f, "attempted to encode skipped enum variant"),
63 #[cfg(feature = "alloc")]
64 Self::Boxed(e) => write!(f, "{e}"),
65 Self::Other(e) => write!(f, "other: {e}"),
66 }
67 }
68}
69
70impl From<io::Error> for Error {
71 #[inline]
72 fn from(value: io::Error) -> Self {
73 Self::Io(value)
74 }
75}
76
77#[cfg(feature = "alloc")]
78impl From<alloc::string::FromUtf8Error> for Error {
79 #[inline]
80 fn from(value: alloc::string::FromUtf8Error) -> Self {
81 Self::FromUtf8(value)
82 }
83}
84
85#[cfg(feature = "alloc")]
86impl From<alloc::ffi::NulError> for Error {
87 #[inline]
88 fn from(value: alloc::ffi::NulError) -> Self {
89 Self::Nul(value)
90 }
91}
92
93impl From<core::num::TryFromIntError> for Error {
94 #[inline]
95 fn from(value: core::num::TryFromIntError) -> Self {
96 Self::TryFromInt(value)
97 }
98}
99
100impl From<core::cell::BorrowError> for Error {
101 #[inline]
102 fn from(value: core::cell::BorrowError) -> Self {
103 Self::Borrow(value)
104 }
105}
106
107impl From<Infallible> for Error {
108 #[inline]
109 fn from(_: Infallible) -> Self {
110 unreachable!()
111 }
112}
113
114#[cfg(feature = "std")]
115impl<T> From<std::sync::PoisonError<T>> for Error {
116 fn from(_: std::sync::PoisonError<T>) -> Self {
117 Self::Poison
118 }
119}
120
121impl core::error::Error for Error {}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[allow(unused)]
128 trait IsSizedSendSync: Sized + Send + Sync {}
129
130 impl IsSizedSendSync for Error {}
131}