Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename BorrowedFd::borrow_raw_fd to BorrowedFd::borrow_raw. #93663

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -62,7 +62,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 @@ -215,7 +215,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 @@ -131,7 +131,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 @@ -329,7 +329,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 @@ -357,49 +357,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 @@ -413,7 +413,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 @@ -427,7 +427,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 @@ -441,7 +441,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 @@ -67,7 +67,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 @@ -223,14 +223,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 @@ -251,7 +251,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 @@ -272,7 +272,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) }
}
}