From 71dab738ac80c2dfadc0d83bb251c70d72d261d6 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 28 Jul 2021 07:02:51 -0700 Subject: [PATCH] Synchronize minor differences between Unix and WASI implementations. --- library/std/src/os/unix/io/fd.rs | 9 +++++---- library/std/src/os/wasi/io/fd.rs | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/library/std/src/os/unix/io/fd.rs b/library/std/src/os/unix/io/fd.rs index 22b1151b92a..f22da625551 100644 --- a/library/std/src/os/unix/io/fd.rs +++ b/library/std/src/os/unix/io/fd.rs @@ -65,8 +65,9 @@ impl BorrowedFd<'_> { #[inline] #[unstable(feature = "io_safety", issue = "87074")] pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self { - assert_ne!(fd, -1_i32 as RawFd); - Self { fd, _phantom: PhantomData } + assert_ne!(fd, u32::MAX as RawFd); + // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned) + unsafe { Self { fd, _phantom: PhantomData } } } } @@ -106,9 +107,9 @@ impl FromRawFd for OwnedFd { /// ownership. The resource must not require any cleanup other than `close`. #[inline] unsafe fn from_raw_fd(fd: RawFd) -> Self { - assert_ne!(fd, -1i32); + assert_ne!(fd, u32::MAX as RawFd); // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned) - Self { fd } + unsafe { Self { fd } } } } diff --git a/library/std/src/os/wasi/io/fd.rs b/library/std/src/os/wasi/io/fd.rs index e07c2e12b7d..8029ad68da9 100644 --- a/library/std/src/os/wasi/io/fd.rs +++ b/library/std/src/os/wasi/io/fd.rs @@ -65,7 +65,8 @@ impl BorrowedFd<'_> { #[inline] #[unstable(feature = "io_safety", issue = "87074")] pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self { - assert_ne!(fd, -1_i32 as RawFd); + assert_ne!(fd, u32::MAX as RawFd); + // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned) unsafe { Self { fd, _phantom: PhantomData } } } } @@ -103,10 +104,10 @@ impl FromRawFd for OwnedFd { /// # Safety /// /// The resource pointed to by `fd` must be open and suitable for assuming - /// ownership. + /// ownership. The resource must not require any cleanup other than `close`. #[inline] unsafe fn from_raw_fd(fd: RawFd) -> Self { - assert_ne!(fd, RawFd::MAX); + assert_ne!(fd, u32::MAX as RawFd); // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned) unsafe { Self { fd } } }