Skip to content

Commit

Permalink
Rollup merge of rust-lang#93663 - sunfishcode:sunfishcode/as-raw-name…
Browse files Browse the repository at this point in the history
…, r=joshtriplett

Rename `BorrowedFd::borrow_raw_fd` to `BorrowedFd::borrow_raw`.

Also, rename `BorrowedHandle::borrow_raw_handle` and
`BorrowedSocket::borrow_raw_socket` to `BorrowedHandle::borrow_raw` and
`BorrowedSocket::borrow_raw`.

This is just a minor rename to reduce redundancy in the user code calling
these functions, and to eliminate an inessential difference between
`BorrowedFd` code and `BorrowedHandle`/`BorrowedSocket` code.

While here, add a simple test exercising `BorrowedFd::borrow_raw_fd`.

r? `@joshtriplett`
  • Loading branch information
matthiaskrgr authored Mar 2, 2022
2 parents 1de70bc + 7d603dc commit c6fa33a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
3 changes: 3 additions & 0 deletions library/std/src/os/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub mod owned;

// Implementations for `AsRawFd` etc. for network types.
mod net;

#[cfg(test)]
mod tests;
4 changes: 2 additions & 2 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl BorrowedFd<'_> {
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self {
pub unsafe fn borrow_raw(fd: RawFd) -> Self {
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 } }
Expand Down Expand Up @@ -235,7 +235,7 @@ impl AsFd for OwnedFd {
// Safety: `OwnedFd` and `BorrowedFd` have the same validity
// invariants, and the `BorrowdFd` is bounded by the lifetime
// of `&self`.
unsafe { BorrowedFd::borrow_raw_fd(self.as_raw_fd()) }
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}

Expand Down
34 changes: 34 additions & 0 deletions library/std/src/os/fd/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[cfg(any(unix, target_os = "wasi"))]
#[test]
fn test_raw_fd() {
#[cfg(unix)]
use crate::os::unix::io::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use crate::os::wasi::io::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};

let raw_fd: RawFd = crate::io::stdin().as_raw_fd();

let stdin_as_file = unsafe { crate::fs::File::from_raw_fd(raw_fd) };
assert_eq!(stdin_as_file.as_raw_fd(), raw_fd);
assert_eq!(unsafe { BorrowedFd::borrow_raw(raw_fd).as_raw_fd() }, raw_fd);
assert_eq!(stdin_as_file.into_raw_fd(), 0);
}

#[cfg(any(unix, target_os = "wasi"))]
#[test]
fn test_fd() {
#[cfg(unix)]
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
#[cfg(target_os = "wasi")]
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};

let stdin = crate::io::stdin();
let fd: BorrowedFd<'_> = stdin.as_fd();
let raw_fd: RawFd = fd.as_raw_fd();
let owned_fd: OwnedFd = unsafe { OwnedFd::from_raw_fd(raw_fd) };

let stdin_as_file = crate::fs::File::from(owned_fd);

assert_eq!(stdin_as_file.as_fd().as_raw_fd(), raw_fd);
assert_eq!(Into::<OwnedFd>::into(stdin_as_file).into_raw_fd(), raw_fd);
}
24 changes: 12 additions & 12 deletions library/std/src/os/windows/io/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl BorrowedHandle<'_> {
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_handle(handle: RawHandle) -> Self {
pub unsafe fn borrow_raw(handle: RawHandle) -> Self {
Self { handle, _phantom: PhantomData }
}
}
Expand Down Expand Up @@ -333,7 +333,7 @@ impl AsHandle for OwnedHandle {
// Safety: `OwnedHandle` and `BorrowedHandle` have the same validity
// invariants, and the `BorrowdHandle` is bounded by the lifetime
// of `&self`.
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

Expand Down Expand Up @@ -361,49 +361,49 @@ impl From<OwnedHandle> for fs::File {
impl AsHandle for crate::io::Stdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl<'a> AsHandle for crate::io::StdinLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl AsHandle for crate::io::Stdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl<'a> AsHandle for crate::io::StdoutLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl AsHandle for crate::io::Stderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl<'a> AsHandle for crate::io::StderrLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

impl AsHandle for crate::process::ChildStdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

Expand All @@ -417,7 +417,7 @@ impl From<crate::process::ChildStdin> for OwnedHandle {
impl AsHandle for crate::process::ChildStdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

Expand All @@ -431,7 +431,7 @@ impl From<crate::process::ChildStdout> for OwnedHandle {
impl AsHandle for crate::process::ChildStderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

Expand All @@ -445,7 +445,7 @@ impl From<crate::process::ChildStderr> for OwnedHandle {
impl<T> AsHandle for crate::thread::JoinHandle<T> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) }
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

Expand Down
10 changes: 5 additions & 5 deletions library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl BorrowedSocket<'_> {
/// `INVALID_SOCKET`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_socket(socket: RawSocket) -> Self {
pub unsafe fn borrow_raw(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
Self { socket, _phantom: PhantomData }
}
Expand Down Expand Up @@ -236,14 +236,14 @@ impl AsSocket for OwnedSocket {
// Safety: `OwnedSocket` and `BorrowedSocket` have the same validity
// invariants, and the `BorrowdSocket` is bounded by the lifetime
// of `&self`.
unsafe { BorrowedSocket::borrow_raw_socket(self.as_raw_socket()) }
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

impl AsSocket for crate::net::TcpStream {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw_socket(self.as_raw_socket()) }
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

Expand All @@ -264,7 +264,7 @@ impl From<OwnedSocket> for crate::net::TcpStream {
impl AsSocket for crate::net::TcpListener {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw_socket(self.as_raw_socket()) }
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

Expand All @@ -285,7 +285,7 @@ impl From<OwnedSocket> for crate::net::TcpListener {
impl AsSocket for crate::net::UdpSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw_socket(self.as_raw_socket()) }
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

Expand Down
12 changes: 6 additions & 6 deletions library/std/src/sys/unix/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,46 @@ pub fn panic_output() -> Option<impl io::Write> {
impl AsFd for io::Stdin {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDIN_FILENO) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
impl<'a> AsFd for io::StdinLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDIN_FILENO) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
impl AsFd for io::Stdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDOUT_FILENO) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
impl<'a> AsFd for io::StdoutLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDOUT_FILENO) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
impl AsFd for io::Stderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) }
}
}

#[unstable(feature = "io_safety", issue = "87074")]
impl<'a> AsFd for io::StderrLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) }
}
}

0 comments on commit c6fa33a

Please sign in to comment.