Rollup merge of #93663 - sunfishcode:sunfishcode/as-raw-name, 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``````
This commit is contained in:
Dylan DPC 2022-03-03 01:09:10 +01:00 committed by GitHub
commit c9dc44be24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 25 deletions

View File

@ -11,3 +11,6 @@ pub mod owned;
// Implementations for `AsRawFd` etc. for network types.
mod net;
#[cfg(test)]
mod tests;

View File

@ -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 } }
@ -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()) }
}
}

View File

@ -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);
}

View File

@ -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 }
}
}
@ -349,7 +349,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()) }
}
}
@ -377,49 +377,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()) }
}
}
@ -433,7 +433,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()) }
}
}
@ -447,7 +447,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()) }
}
}
@ -461,7 +461,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()) }
}
}

View File

@ -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 }
}
@ -243,14 +243,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()) }
}
}
@ -271,7 +271,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()) }
}
}
@ -292,7 +292,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()) }
}
}

View File

@ -96,7 +96,7 @@ 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) }
}
}
@ -104,7 +104,7 @@ impl AsFd for io::Stdin {
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) }
}
}
@ -112,7 +112,7 @@ impl<'a> AsFd for io::StdinLock<'a> {
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) }
}
}
@ -120,7 +120,7 @@ impl AsFd for io::Stdout {
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) }
}
}
@ -128,7 +128,7 @@ impl<'a> AsFd for io::StdoutLock<'a> {
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) }
}
}
@ -136,6 +136,6 @@ impl AsFd for io::Stderr {
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) }
}
}