1
2use core::fmt::Debug;
6use core::fmt::Display;
7use std::error::Error;
8use crate::element::FlockElement;
9use core::ops::Deref;
10use std::io::ErrorKind;
11
12pub struct FlockError<T> where T: FlockElement {
15 data: T,
16 err: std::io::Error,
17}
18
19impl<T> Error for FlockError<T> where T: FlockElement {
20 #[inline(always)]
21 fn source(&self) -> Option<&(dyn Error + 'static)> {
22 Error::source(&self.err)
23 }
24
25 #[allow(deprecated)]
37 #[inline(always)]
38 fn description(&self) -> &str {
39 Error::description(&self.err)
40 }
41
42 #[allow(deprecated)]
43 #[inline(always)]
44 fn cause(&self) -> Option<&dyn Error> {
45 Error::cause(&self.err)
46 }
47}
48
49impl<T> Display for FlockError<T> where T: FlockElement {
51 #[inline(always)]
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
53 Display::fmt(&self.err, f)
54 }
55}
56
57impl<T> Debug for FlockError<T> where T: FlockElement {
59 #[inline(always)]
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
61 Debug::fmt(&self.err, f)
62 }
63}
64
65impl<T> From<(T, std::io::Error)> for FlockError<T> where T: FlockElement {
66 #[inline(always)]
67 fn from((data, err): (T, std::io::Error)) -> Self {
68 Self::new(data, err)
69 }
70}
71
72impl<T> FlockError<T> where T: FlockElement {
73 #[inline]
74 pub fn new(a: T, err: std::io::Error) -> Self {
75 Self {
76 data: a,
77 err: err,
78 }
79 }
80
81 #[inline(always)]
82 pub fn get_debug_data(&self) -> &impl Debug where T: Debug {
83 &self.data
84 }
85
86 #[inline(always)]
87 pub fn get_debug_err(&self) -> &impl Debug {
88 &self.err
89 }
90
91 #[inline(always)]
93 pub fn into(self) -> T {
94 self.into_data()
95 }
96
97 #[inline(always)]
98 pub fn is_would_block(&self) -> bool {
99 self.err.kind() == ErrorKind::WouldBlock
100 }
101
102 #[inline(always)]
104 pub fn is_already_lock(&self) -> bool {
105 self.is_would_block()
106 }
107
108 #[inline(always)]
110 pub fn as_data(&self) -> &T {
111 &self.data
112 }
113
114 #[inline(always)]
116 pub fn as_err(&self) -> &std::io::Error {
117 &self.err
118 }
119
120 #[inline(always)]
122 pub fn into_data(self) -> T {
123 self.data
124 }
125
126 #[inline(always)]
128 pub fn into_all(self) -> (T, std::io::Error) {
129 (self.data, self.err)
130 }
131
132 #[inline(always)]
134 pub fn into_err(self) -> std::io::Error {
135 self.err
136 }
137
138 #[inline(always)]
140 #[deprecated(since="1.2.6", note="please use `into_err` instead")]
141 pub fn err(self) -> std::io::Error {
142 self.into_err()
143 }
144
145 #[inline(always)]
147 #[deprecated(since="1.2.6", note="please use `into_all` instead")]
148 pub fn all(self) -> (T, std::io::Error) {
149 self.into_all()
150 }
151}
152
153impl<T> From<FlockError<T>> for std::io::Error where T: FlockElement {
154 #[inline(always)]
155 fn from(a: FlockError<T>) -> std::io::Error {
156 a.err
157 }
158}
159
160impl<T> Deref for FlockError<T> where T: FlockElement {
161 type Target = std::io::Error;
162
163 #[inline(always)]
164 fn deref(&self) -> &Self::Target {
165 self.as_err()
166 }
167}