Skip to content

Commit 4ce68c1

Browse files
committed
Clarify what values BorrowedHandle, OwnedHandle etc. can hold.
Clarify that when `BorrowedHandle`, `OwnedHandle`, or `HandleOrNull` hold the value `-1`, it always means the current process handle, and not `INVALID_HANDLE_VALUE`.
1 parent ecd4495 commit 4ce68c1

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ 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.
2424
///
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.
25+
/// Note that it *may* have the value `-1`, which in `BorrowedFd` always
26+
/// represents the current process handle, and not `INVALID_HANDLE_VALUE`,
27+
/// despite the two having the same value. See [here] for the full story.
2728
///
2829
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
2930
/// detached from processes, or when `windows_subsystem` is used.
@@ -45,8 +46,9 @@ pub struct BorrowedHandle<'handle> {
4546
///
4647
/// This closes the handle on drop.
4748
///
48-
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
49-
/// sometimes a valid handle value. See [here] for the full story.
49+
/// Note that it *may* have the value `-1`, which in `OwnedFd` always
50+
/// represents the current process handle, and not `INVALID_HANDLE_VALUE`,
51+
/// despite the two having the same value. See [here] for the full story.
5052
///
5153
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
5254
/// detached from processes, or when `windows_subsystem` is used.
@@ -75,11 +77,11 @@ pub struct OwnedHandle {
7577
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
7678
/// checking for `NULL` first.
7779
///
78-
/// This type considers any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
79-
/// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE`
80-
/// as special.
80+
/// This type may hold any handle value that [`OwnedFd`] may hold, except `NULL`. It may
81+
/// hold `-1`, even though `-1` has the same value as `INVALID_HANDLE_VALUE`, because in
82+
/// `HandleOrNull`, `-1` is interpreted to mean the current process handle.
8183
///
82-
/// If this holds a valid handle, it will close the handle on drop.
84+
/// If this holds a non-null handle, it will close the handle on drop.
8385
#[repr(transparent)]
8486
#[unstable(feature = "io_safety", issue = "87074")]
8587
#[derive(Debug)]
@@ -95,11 +97,14 @@ pub struct HandleOrNull(OwnedHandle);
9597
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
9698
/// checking for `INVALID_HANDLE_VALUE` first.
9799
///
98-
/// This type considers any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
99-
/// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL`
100-
/// under `windows_subsystem = "windows"` or other situations where I/O devices are detached.
100+
/// This type may hold any handle value that [`OwnedFd`] may hold, except `-1`. It must not
101+
/// hold `-1`, because `-1` in `HandleOrInvalid` is interpreted to mean `INVALID_HANDLE_VALUE`.
101102
///
102-
/// If this holds a valid handle, it will close the handle on drop.
103+
/// This type may hold `NULL`, because APIs that use `INVALID_HANDLE_VALUE` as their sentry value
104+
/// may return `NULL` under `windows_subsystem = "windows"` or other situations where I/O devices
105+
/// are detached.
106+
///
107+
/// If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.
103108
#[repr(transparent)]
104109
#[unstable(feature = "io_safety", issue = "87074")]
105110
#[derive(Debug)]

0 commit comments

Comments
 (0)