@@ -76,7 +76,7 @@ pub struct OwnedHandle {
76
76
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
77
77
/// checking for `NULL` first.
78
78
///
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`.
80
80
/// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE`
81
81
/// as special.
82
82
///
@@ -96,7 +96,7 @@ pub struct HandleOrNull(OwnedHandle);
96
96
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
97
97
/// checking for `INVALID_HANDLE_VALUE` first.
98
98
///
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`.
100
100
/// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL`
101
101
/// under `windows_subsystem = "windows"` or other situations where I/O devices are detached.
102
102
///
@@ -143,17 +143,17 @@ impl BorrowedHandle<'_> {
143
143
}
144
144
145
145
impl TryFrom < HandleOrNull > for OwnedHandle {
146
- type Error = ( ) ;
146
+ type Error = NullHandleError ;
147
147
148
148
#[ inline]
149
- fn try_from ( handle_or_null : HandleOrNull ) -> Result < Self , ( ) > {
149
+ fn try_from ( handle_or_null : HandleOrNull ) -> Result < Self , NullHandleError > {
150
150
let owned_handle = handle_or_null. 0 ;
151
151
if owned_handle. handle . is_null ( ) {
152
152
// Don't call `CloseHandle`; it'd be harmless, except that it could
153
153
// overwrite the `GetLastError` error.
154
154
forget ( owned_handle) ;
155
155
156
- Err ( ( ) )
156
+ Err ( NullHandleError ( ( ) ) )
157
157
} else {
158
158
Ok ( owned_handle)
159
159
}
@@ -201,23 +201,59 @@ impl OwnedHandle {
201
201
}
202
202
203
203
impl TryFrom < HandleOrInvalid > for OwnedHandle {
204
- type Error = ( ) ;
204
+ type Error = InvalidHandleError ;
205
205
206
206
#[ inline]
207
- fn try_from ( handle_or_invalid : HandleOrInvalid ) -> Result < Self , ( ) > {
207
+ fn try_from ( handle_or_invalid : HandleOrInvalid ) -> Result < Self , InvalidHandleError > {
208
208
let owned_handle = handle_or_invalid. 0 ;
209
209
if owned_handle. handle == c:: INVALID_HANDLE_VALUE {
210
210
// Don't call `CloseHandle`; it'd be harmless, except that it could
211
211
// overwrite the `GetLastError` error.
212
212
forget ( owned_handle) ;
213
213
214
- Err ( ( ) )
214
+ Err ( InvalidHandleError ( ( ) ) )
215
215
} else {
216
216
Ok ( owned_handle)
217
217
}
218
218
}
219
219
}
220
220
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
+
221
257
impl AsRawHandle for BorrowedHandle < ' _ > {
222
258
#[ inline]
223
259
fn as_raw_handle ( & self ) -> RawHandle {
0 commit comments