Skip to content

Commit 907f00b

Browse files
committed
Add more comments about the INVALID_HANDLE_VALUE situation.
1 parent ab08639 commit 907f00b

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
2222
/// so it can be used in FFI in places where a handle is passed as an argument,
2323
/// it is not captured or consumed, and it is never null.
2424
///
25-
/// Note that it *may* have the value `INVALID_HANDLE_VALUE`. See [here] for
26-
/// the full story.
25+
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
26+
/// sometimes a valid handle value. See [here] for the full story.
2727
///
2828
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
2929
#[derive(Copy, Clone)]
@@ -42,10 +42,10 @@ pub struct BorrowedHandle<'handle> {
4242
/// so it can be used in FFI in places where a handle is passed as a consumed
4343
/// argument or returned as an owned value, and is never null.
4444
///
45-
/// Note that it *may* have the value `INVALID_HANDLE_VALUE`. See [here] for
46-
/// the full story. For APIs like `CreateFileW` which report errors with
47-
/// `INVALID_HANDLE_VALUE` instead of null, use [`OptionFileHandle`] instead
48-
/// of `Option<OwnedHandle>`.
45+
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
46+
/// sometimes a valid handle value. See [here] for the full story. For APIs
47+
/// like `CreateFileW` which report errors with `INVALID_HANDLE_VALUE` instead
48+
/// of null, use [`OptionFileHandle`] instead of `Option<OwnedHandle>`.
4949
///
5050
/// `OwnedHandle` uses [`CloseHandle`] to close its handle on drop. As such,
5151
/// it must not be used with handles to open registry keys which need to be
@@ -98,8 +98,14 @@ impl BorrowedHandle<'_> {
9898
///
9999
/// # Safety
100100
///
101-
/// The resource pointed to by `handle` must remain open for the duration
102-
/// of the returned `BorrowedHandle`, and it must not be null.
101+
/// The resource pointed to by `handle` must be a valid open handle, it
102+
/// must remain open for the duration of the returned `BorrowedHandle`, and
103+
/// it must not be null.
104+
///
105+
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
106+
/// sometimes a valid handle value. See [here] for the full story.
107+
///
108+
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
103109
#[inline]
104110
#[unstable(feature = "io_safety", issue = "87074")]
105111
pub unsafe fn borrow_raw_handle(handle: RawHandle) -> Self {
@@ -182,6 +188,9 @@ impl IntoRawHandle for OwnedHandle {
182188
impl FromRawHandle for OwnedHandle {
183189
/// Constructs a new instance of `Self` from the given raw handle.
184190
///
191+
/// Use `OptionFileHandle` instead of `Option<OwnedHandle>` for APIs that
192+
/// use `INVALID_HANDLE_VALUE` to indicate failure.
193+
///
185194
/// # Safety
186195
///
187196
/// The resource pointed to by `handle` must be open and suitable for
@@ -191,7 +200,11 @@ impl FromRawHandle for OwnedHandle {
191200
/// In particular, it must not be used with handles to open registry
192201
/// keys which need to be closed with [`RegCloseKey`] instead.
193202
///
203+
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
204+
/// sometimes a valid handle value. See [here] for the full story.
205+
///
194206
/// [`RegCloseKey`]: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey
207+
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
195208
#[inline]
196209
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
197210
assert!(!handle.is_null());
@@ -200,14 +213,19 @@ impl FromRawHandle for OwnedHandle {
200213
}
201214

202215
impl FromRawHandle for OptionFileHandle {
203-
/// Constructs a new instance of `Self` from the given raw handle.
216+
/// Constructs a new instance of `Self` from the given raw handle returned
217+
/// from a Windows API that uses `INVALID_HANDLE_VALUE` to indicate
218+
/// failure, such as `CreateFileW`.
219+
///
220+
/// Use `Option<OwnedHandle>` instead of `OptionFileHandle` for APIs that
221+
/// use null to indicate failure.
204222
///
205223
/// # Safety
206224
///
207225
/// The resource pointed to by `handle` must be either open and otherwise
208-
/// unowned, or equal to `INVALID_HANDLE_VALUE``. Note that not all Windows
209-
/// APIs use `INVALID_HANDLE_VALUE` for errors; see [here] for the full
210-
/// story.
226+
/// unowned, or equal to `INVALID_HANDLE_VALUE` (-1). It must not be null.
227+
/// Note that not all Windows APIs use `INVALID_HANDLE_VALUE` for errors;
228+
/// see [here] for the full story.
211229
///
212230
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
213231
#[inline]

0 commit comments

Comments
 (0)