Skip to content

Commit f68db8c

Browse files
authored
Rollup merge of rust-lang#96232 - sunfishcode:sunfishcode/io-safety-const-fns, r=joshtriplett
Make `BorrowedFd::borrow_raw` a const fn. Making `BorrowedFd::borrow_raw` a const fn allows it to be used to create a constant `BorrowedFd<'static>` holding constants such as `AT_FDCWD`. This will allow [`rustix::fs::cwd`] to become a const fn. For consistency, make similar changes to `BorrowedHandle::borrow_raw` and `BorrowedSocket::borrow_raw`. [`rustix::fs::cwd`]: https://docs.rs/rustix/latest/rustix/fs/fn.cwd.html r? `@joshtriplett`
2 parents d4c3643 + 0a1ce82 commit f68db8c

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

library/std/src/os/fd/owned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl BorrowedFd<'_> {
6666
/// the returned `BorrowedFd`, and it must not have the value `-1`.
6767
#[inline]
6868
#[unstable(feature = "io_safety", issue = "87074")]
69-
pub unsafe fn borrow_raw(fd: RawFd) -> Self {
70-
assert_ne!(fd, u32::MAX as RawFd);
69+
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
70+
assert!(fd != u32::MAX as RawFd);
7171
// 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)
7272
unsafe { Self { fd, _phantom: PhantomData } }
7373
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl BorrowedHandle<'_> {
136136
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
137137
#[inline]
138138
#[unstable(feature = "io_safety", issue = "87074")]
139-
pub unsafe fn borrow_raw(handle: RawHandle) -> Self {
139+
pub const unsafe fn borrow_raw(handle: RawHandle) -> Self {
140140
Self { handle, _phantom: PhantomData }
141141
}
142142
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ impl BorrowedSocket<'_> {
7171
/// `INVALID_SOCKET`.
7272
#[inline]
7373
#[unstable(feature = "io_safety", issue = "87074")]
74-
pub unsafe fn borrow_raw(socket: RawSocket) -> Self {
75-
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
74+
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
75+
assert!(socket != c::INVALID_SOCKET as RawSocket);
7676
Self { socket, _phantom: PhantomData }
7777
}
7878
}

0 commit comments

Comments
 (0)