Skip to content

Commit 0afeb31

Browse files
committed
Make BorrowedFd::borrow_raw a const fn.
This corresponds to rust-lang/rust#96232. Also, update the documentation and remove the assert from `BorrowedHandle::borrow_raw` to match what's in std: `BorrowedHandle` can hold NULL pointers.
1 parent f33b156 commit 0afeb31

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/types.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ impl BorrowedFd<'_> {
436436
/// The resource pointed to by `raw` must remain open for the duration of
437437
/// the returned `BorrowedFd`, and it must not have the value `-1`.
438438
#[inline]
439-
pub unsafe fn borrow_raw(fd: RawFd) -> Self {
440-
debug_assert_ne!(fd, -1_i32 as RawFd);
439+
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
440+
debug_assert!(fd != -1_i32 as RawFd);
441441
Self {
442442
fd,
443443
_phantom: PhantomData,
@@ -451,11 +451,18 @@ impl BorrowedHandle<'_> {
451451
///
452452
/// # Safety
453453
///
454-
/// The resource pointed to by `raw` must remain open for the duration of
455-
/// the returned `BorrowedHandle`, and it must not be null.
454+
/// The resource pointed to by `handle` must be a valid open handle, it
455+
/// must remain open for the duration of the returned `BorrowedHandle`.
456+
///
457+
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
458+
/// sometimes a valid handle value. See [here] for the full story.
459+
///
460+
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
461+
/// detached from processes, or when `windows_subsystem` is used.
462+
///
463+
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
456464
#[inline]
457-
pub unsafe fn borrow_raw(handle: RawHandle) -> Self {
458-
debug_assert!(!handle.is_null());
465+
pub const unsafe fn borrow_raw(handle: RawHandle) -> Self {
459466
Self {
460467
handle,
461468
_phantom: PhantomData,
@@ -473,8 +480,8 @@ impl BorrowedSocket<'_> {
473480
/// the returned `BorrowedSocket`, and it must not have the value
474481
/// [`INVALID_SOCKET`].
475482
#[inline]
476-
pub unsafe fn borrow_raw(socket: RawSocket) -> Self {
477-
debug_assert_ne!(socket, INVALID_SOCKET as RawSocket);
483+
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
484+
debug_assert!(socket != INVALID_SOCKET as RawSocket);
478485
Self {
479486
socket,
480487
_phantom: PhantomData,

0 commit comments

Comments
 (0)