Skip to content

Commit 7d04865

Browse files
authored
Clarify what values BorrowedHandle, OwnedHandle etc. can hold. (#30)
* Clarify what values BorrowedHandle, OwnedHandle etc. can hold. This ports the documentation updates from rust-lang/rust#96932. * Relax the wording about the meaning of -1.
1 parent 36fd849 commit 7d04865

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/types.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ pub struct BorrowedFd<'fd> {
7070
/// so it can be used in FFI in places where a handle is passed as an argument,
7171
/// it is not captured or consumed, and it is never null.
7272
///
73-
/// Note that it *may* have the value [`INVALID_HANDLE_VALUE`]. See [here] for
74-
/// the full story.
73+
/// Note that it *may* have the value `-1`, which in `BorrowedHandle` always
74+
/// represents a valid handle value, such as [the current process handle], and
75+
/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See
76+
/// [here] for the full story.
7577
///
7678
/// This type's `.to_owned()` implementation returns another `BorrowedHandle`
7779
/// rather than an `OwnedHandle`. It just makes a trivial copy of the raw
7880
/// handle, which is then borrowed under the same lifetime.
7981
///
8082
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
83+
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
8184
#[cfg(windows)]
8285
#[derive(Copy, Clone)]
8386
#[repr(transparent)]
@@ -187,8 +190,10 @@ impl OwnedFd {
187190
///
188191
/// This closes the handle on drop.
189192
///
190-
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
191-
/// sometimes a valid handle value. See [here] for the full story.
193+
/// Note that it *may* have the value `-1`, which in `OwnedHandle` always
194+
/// represents a valid handle value, such as [the current process handle], and
195+
/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See
196+
/// [here] for the full story.
192197
///
193198
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
194199
/// detached from processes, or when `windows_subsystem` is used.
@@ -201,6 +206,7 @@ impl OwnedFd {
201206
/// [`RegCloseKey`]: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey
202207
///
203208
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
209+
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
204210
#[cfg(windows)]
205211
#[repr(transparent)]
206212
pub struct OwnedHandle {
@@ -375,11 +381,10 @@ impl OwnedSocket {
375381
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
376382
/// checking for `INVALID_HANDLE_VALUE` first.
377383
///
378-
/// This type concerns any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
379-
/// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL`
380-
/// under `windows_subsystem = "windows"` or other situations where I/O devices are detached.
384+
/// This type may hold any handle value that [`OwnedHandle`] may hold, except that when it holds
385+
/// `-1`, that value is interpreted to mean `INVALID_HANDLE_VALUE`.
381386
///
382-
/// If this holds a valid handle, it will close the handle on drop.
387+
/// If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.
383388
#[cfg(windows)]
384389
#[repr(transparent)]
385390
#[derive(Debug)]
@@ -395,11 +400,13 @@ pub struct HandleOrInvalid(RawHandle);
395400
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
396401
/// checking for `NULL` first.
397402
///
398-
/// This type concerns any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
399-
/// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE`
400-
/// as special.
403+
/// This type may hold any handle value that [`OwnedHandle`] may hold. As with `OwnedHandle`, when
404+
/// it holds `-1`, that value is interpreted as a valid handle value, such as
405+
/// [the current process handle], and not `INVALID_HANDLE_VALUE`.
401406
///
402-
/// If this holds a valid handle, it will close the handle on drop.
407+
/// If this holds a non-null handle, it will close the handle on drop.
408+
///
409+
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
403410
#[cfg(windows)]
404411
#[repr(transparent)]
405412
#[derive(Debug)]

0 commit comments

Comments
 (0)