Skip to content

Commit 29c5aea

Browse files
committed
Auto merge of #96195 - sunfishcode:sunfishcode/handle-or-error-type, r=joshtriplett
Define a dedicated error type for `HandleOrNull` and `HandleOrInvalid`. Define `NullHandleError` and `InvalidHandleError` types, that implement std::error::Error, and use them as the error types in `HandleOrNull` and `HandleOrInvalid`, This addresses [this concern](rust-lang/rust#87074 (comment)). This is the same as #95387. r? `@joshtriplett`
2 parents a8b990e + 46bf70c commit 29c5aea

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

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

+44-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct OwnedHandle {
7676
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
7777
/// checking for `NULL` first.
7878
///
79-
/// This type concerns any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
79+
/// This type considers any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
8080
/// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE`
8181
/// as special.
8282
///
@@ -96,7 +96,7 @@ pub struct HandleOrNull(OwnedHandle);
9696
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
9797
/// checking for `INVALID_HANDLE_VALUE` first.
9898
///
99-
/// This type concerns any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
99+
/// This type considers any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
100100
/// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL`
101101
/// under `windows_subsystem = "windows"` or other situations where I/O devices are detached.
102102
///
@@ -143,17 +143,17 @@ impl BorrowedHandle<'_> {
143143
}
144144

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

148148
#[inline]
149-
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
149+
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
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(NullHandleError(()))
157157
} else {
158158
Ok(owned_handle)
159159
}
@@ -201,23 +201,59 @@ impl OwnedHandle {
201201
}
202202

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

206206
#[inline]
207-
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
207+
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
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(InvalidHandleError(()))
215215
} else {
216216
Ok(owned_handle)
217217
}
218218
}
219219
}
220220

221+
/// This is the error type used by [`HandleOrNull`] when attempting to convert
222+
/// into a handle, to indicate that the value is null.
223+
// The empty field prevents constructing this, and allows extending it in the future.
224+
#[unstable(feature = "io_safety", issue = "87074")]
225+
#[derive(Debug, Clone, PartialEq, Eq)]
226+
pub struct NullHandleError(());
227+
228+
#[unstable(feature = "io_safety", issue = "87074")]
229+
impl fmt::Display for NullHandleError {
230+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
231+
"A HandleOrNull could not be converted to a handle because it was null".fmt(fmt)
232+
}
233+
}
234+
235+
#[unstable(feature = "io_safety", issue = "87074")]
236+
impl crate::error::Error for NullHandleError {}
237+
238+
/// This is the error type used by [`HandleOrInvalid`] when attempting to
239+
/// convert into a handle, to indicate that the value is
240+
/// `INVALID_HANDLE_VALUE`.
241+
// The empty field prevents constructing this, and allows extending it in the future.
242+
#[unstable(feature = "io_safety", issue = "87074")]
243+
#[derive(Debug, Clone, PartialEq, Eq)]
244+
pub struct InvalidHandleError(());
245+
246+
#[unstable(feature = "io_safety", issue = "87074")]
247+
impl fmt::Display for InvalidHandleError {
248+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
249+
"A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE"
250+
.fmt(fmt)
251+
}
252+
}
253+
254+
#[unstable(feature = "io_safety", issue = "87074")]
255+
impl crate::error::Error for InvalidHandleError {}
256+
221257
impl AsRawHandle for BorrowedHandle<'_> {
222258
#[inline]
223259
fn as_raw_handle(&self) -> RawHandle {

0 commit comments

Comments
 (0)