cluFlock/data/
err.rs

1
2//! Error structures used in cluFlock methods.
3//!
4
5use core::fmt::Debug;
6use core::fmt::Display;
7use std::error::Error;
8use crate::element::FlockElement;
9use core::ops::Deref;
10use std::io::ErrorKind;
11
12/// The standard error for Flock methods, from the error you can get a borrowed value.
13//#[derive(Debug)]
14pub 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	/*#[inline(always)] <<-- TODO, UNSTABLE unstable(feature = "backtrace", issue = "53487")
26	fn backtrace(&self) -> Option<&Backtrace> {
27		Error::backtrace(&self.err)
28	}*/
29	
30	/*#[inline(always)] <<-- TODO, UNSTABLE issue = "60784"
31	fn type_id(&self, _: private::Internal) -> TypeId {
32		
33	}
34	*/
35	
36	#[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
49// Required only for compatibility with StdErr(Error).
50impl<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
57// Required only for compatibility with StdErr(Error).
58impl<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	/// Retrieve only the data from the error structure.
92	#[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	/// The error occurred due to the presence of a lock.
103	#[inline(always)]
104	pub fn is_already_lock(&self) -> bool {
105		self.is_would_block()
106	}
107	
108	/// Get a link to data.
109	#[inline(always)]
110	pub fn as_data(&self) -> &T {
111		&self.data
112	}
113	
114	/// Get a link to err.
115	#[inline(always)]
116	pub fn as_err(&self) -> &std::io::Error {
117		&self.err
118	}
119	
120	/// Retrieve only the data from the error structure.
121	#[inline(always)]
122	pub fn into_data(self) -> T {
123		self.data
124	}
125	
126	/// Get all data from the error structure.
127	#[inline(always)]
128	pub fn into_all(self) -> (T, std::io::Error) {
129		(self.data, self.err)
130	}
131	
132	/// Get only the error from the error structure.
133	#[inline(always)]
134	pub fn into_err(self) -> std::io::Error {
135		self.err
136	}
137	
138	/// Get only the error from the error structure.
139	#[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	/// Get all data from the error structure.
146	#[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}