1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(
clippy::all,
clippy::pedantic,
warnings,
future_incompatible,
rust_2018_idioms,
rustdoc,
unused,
deprecated_in_future,
missing_copy_implementations,
missing_debug_implementations,
non_ascii_idents,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_import_braces,
unused_lifetimes,
unused_results
)]
#![allow(clippy::needless_doctest_main, clippy::must_use_candidate)]
#[cfg(not(target_os = "windows"))]
compile_error!("w32-error only supports Windows-based targets");
#[cfg(not(feature = "std"))]
use core as std_crate;
#[cfg(feature = "std")]
use std as std_crate;
use std_crate::{
char,
fmt::{self, Display, Formatter, Write},
hint, mem, ptr,
};
#[cfg(feature = "std")]
use std_crate::{convert::TryFrom, error::Error, io};
use winapi::{
shared::minwindef::DWORD,
um::{
errhandlingapi::GetLastError,
winbase::{
FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS,
FORMAT_MESSAGE_MAX_WIDTH_MASK,
},
winnt::WCHAR,
},
};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[must_use = "this `W32Error` is unhandled"]
#[repr(transparent)]
pub struct W32Error(DWORD);
#[cfg(feature = "std")]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[must_use = "this `TryFromIoError` is unhandled"]
pub struct TryFromIoError;
impl W32Error {
pub const fn new(code: DWORD) -> Self {
Self(code)
}
#[cfg(feature = "std")]
#[allow(clippy::cast_sign_loss)]
pub fn from_io_error(other: &io::Error) -> Option<Self> {
if let Some(code) = other.raw_os_error() {
Some(W32Error::new(code as DWORD))
} else {
None
}
}
pub fn last_thread_error() -> Self {
Self::new(unsafe { GetLastError() })
}
pub const fn into_inner(self) -> DWORD {
self.0
}
}
impl Display for W32Error {
#[allow(clippy::cast_possible_truncation)]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
const MAX_CHARACTERS: usize = 1024;
debug_assert!(mem::size_of::<[WCHAR; MAX_CHARACTERS]>() <= 65536);
let mut wide_buffer = [WCHAR::default(); MAX_CHARACTERS];
let len = unsafe {
FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_MAX_WIDTH_MASK,
ptr::null(),
self.0,
0,
wide_buffer.as_mut_ptr(),
MAX_CHARACTERS as DWORD,
ptr::null_mut(),
) as usize
};
if len == 0 {
f.write_fmt(format_args!("{:#08X}", self.0))
} else {
let mut char_buffer = [char::default(); MAX_CHARACTERS];
let char_msg = &mut char_buffer[..len];
let wide_msg = &wide_buffer[..len];
char::decode_utf16(wide_msg.iter().copied())
.map(|res| res.unwrap_or(char::REPLACEMENT_CHARACTER))
.zip(char_msg.iter_mut())
.for_each(|(src, dst)| *dst = src);
if let Some(a) = char_msg.iter().position(|c| !c.is_whitespace()) {
let b = char_msg
.iter()
.rposition(|c| !c.is_whitespace())
.unwrap_or_else(|| unsafe { hint::unreachable_unchecked() });
for &c in &char_msg[a..=b] {
f.write_char(c)?;
}
}
Ok(())
}
}
}
#[cfg(feature = "std")]
impl Error for W32Error {}
#[cfg(feature = "std")]
impl From<W32Error> for io::Error {
#[allow(clippy::cast_possible_wrap)]
fn from(other: W32Error) -> Self {
io::Error::from_raw_os_error(other.into_inner() as i32)
}
}
impl From<DWORD> for W32Error {
fn from(other: DWORD) -> Self {
Self::new(other)
}
}
impl From<W32Error> for DWORD {
fn from(other: W32Error) -> Self {
other.into_inner()
}
}
#[cfg(feature = "std")]
impl TryFrom<io::Error> for W32Error {
type Error = TryFromIoError;
fn try_from(other: io::Error) -> Result<Self, Self::Error> {
Self::from_io_error(&other).ok_or(TryFromIoError)
}
}
#[cfg(feature = "std")]
impl Display for TryFromIoError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("the given `io::Error` did not contain a Windows API error code")
}
}
#[cfg(feature = "std")]
impl Error for TryFromIoError {}