Skip to content

Commit 0efbd34

Browse files
committed
Split NotHandle into NullHandleError and InvalidHandleError.
Also, make the display messages more specific, and remove the `Copy` implementation.
1 parent fc3b8b3 commit 0efbd34

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

library/std/src/os/windows/io/handle.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ impl BorrowedHandle<'_> {
142142
}
143143

144144
impl TryFrom<HandleOrNull> for OwnedHandle {
145-
type Error = NotHandle;
145+
type Error = NullHandleError;
146146

147147
#[inline]
148-
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NotHandle> {
148+
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
149149
let owned_handle = handle_or_null.0;
150150
if owned_handle.handle.is_null() {
151151
// Don't call `CloseHandle`; it'd be harmless, except that it could
152152
// overwrite the `GetLastError` error.
153153
forget(owned_handle);
154154

155-
Err(NotHandle(()))
155+
Err(NullHandleError(()))
156156
} else {
157157
Ok(owned_handle)
158158
}
@@ -200,39 +200,56 @@ impl OwnedHandle {
200200
}
201201

202202
impl TryFrom<HandleOrInvalid> for OwnedHandle {
203-
type Error = NotHandle;
203+
type Error = InvalidHandleError;
204204

205205
#[inline]
206-
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, NotHandle> {
206+
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
207207
let owned_handle = handle_or_invalid.0;
208208
if owned_handle.handle == c::INVALID_HANDLE_VALUE {
209209
// Don't call `CloseHandle`; it'd be harmless, except that it could
210210
// overwrite the `GetLastError` error.
211211
forget(owned_handle);
212212

213-
Err(NotHandle(()))
213+
Err(InvalidHandleError(()))
214214
} else {
215215
Ok(owned_handle)
216216
}
217217
}
218218
}
219219

220-
/// This is the error type used by [`HandleOrInvalid`] and
221-
/// [`HandleOrNull`] when attempting to convert into a handle,
222-
/// to indicate that the value is not a handle.
220+
/// This is the error type used by [`HandleOrNull`] when attempting to convert
221+
/// into a handle, to indicate that the value is null.
223222
#[unstable(feature = "io_safety", issue = "87074")]
224-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
225-
pub struct NotHandle(());
223+
#[derive(Debug, Clone, PartialEq, Eq)]
224+
pub struct NullHandleError(());
226225

227226
#[unstable(feature = "io_safety", issue = "87074")]
228-
impl fmt::Display for NotHandle {
227+
impl fmt::Display for NullHandleError {
229228
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
230-
"the return value of a Windows API call indicated an error".fmt(fmt)
229+
"A HandleOrNull could not be converted to a handle because it was null".fmt(fmt)
231230
}
232231
}
233232

234233
#[unstable(feature = "io_safety", issue = "87074")]
235-
impl crate::error::Error for NotHandle {}
234+
impl crate::error::Error for NullHandleError {}
235+
236+
/// This is the error type used by [`HandleOrInvalid`] when attempting to
237+
/// convert into a handle, to indicate that the value is
238+
/// `INVALID_HANDLE_VALUE`.
239+
#[unstable(feature = "io_safety", issue = "87074")]
240+
#[derive(Debug, Clone, PartialEq, Eq)]
241+
pub struct InvalidHandleError(());
242+
243+
#[unstable(feature = "io_safety", issue = "87074")]
244+
impl fmt::Display for InvalidHandleError {
245+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
246+
"A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE"
247+
.fmt(fmt)
248+
}
249+
}
250+
251+
#[unstable(feature = "io_safety", issue = "87074")]
252+
impl crate::error::Error for InvalidHandleError {}
236253

237254
impl AsRawHandle for BorrowedHandle<'_> {
238255
#[inline]

0 commit comments

Comments
 (0)