Skip to content

Commit 703a336

Browse files
committed
Define a dedicated error type for HandleOrNull and HandleOrInvalid.
Define a `NotHandle` type, that implements `std::error::Error`, and use it as the error type in `HandleOrNull` and `HandleOrInvalid`.
1 parent 311e268 commit 703a336

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,17 @@ impl BorrowedHandle<'_> {
143143
}
144144

145145
impl TryFrom<HandleOrNull> for OwnedHandle {
146-
type Error = ();
146+
type Error = NotHandle;
147147

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

156-
Err(())
156+
Err(NotHandle(()))
157157
} else {
158158
Ok(owned_handle)
159159
}
@@ -201,23 +201,37 @@ impl OwnedHandle {
201201
}
202202

203203
impl TryFrom<HandleOrInvalid> for OwnedHandle {
204-
type Error = ();
204+
type Error = NotHandle;
205205

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

214-
Err(())
214+
Err(NotHandle(()))
215215
} else {
216216
Ok(owned_handle)
217217
}
218218
}
219219
}
220220

221+
/// This is the error type used by [`HandleOrInvalid`] and
222+
/// [`HandleOrNull`] when attempting to convert into a handle,
223+
/// to indicate that the value is not a handle.
224+
#[unstable(feature = "io_safety", issue = "87074")]
225+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
226+
pub struct NotHandle(());
227+
228+
#[unstable(feature = "io_safety", issue = "87074")]
229+
impl fmt::Display for NotHandle {
230+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
231+
"the return value of a Windows API call indicated an error".fmt(fmt)
232+
}
233+
}
234+
221235
impl AsRawHandle for BorrowedHandle<'_> {
222236
#[inline]
223237
fn as_raw_handle(&self) -> RawHandle {

0 commit comments

Comments
 (0)