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

Implement more I/O safety traits. #1831

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Changes from 1 commit
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
53 changes: 50 additions & 3 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::net::{self, SocketAddr};
#[cfg(any(unix, target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
// TODO: once <https://github.com/rust-lang/rust/issues/126198> is fixed this
// can use `std::os::fd` and be merged with the above.
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket, OwnedSocket, BorrowedSocket, AsSocket};
use std::{fmt, io};

use crate::io_source::IoSource;
@@ -195,13 +195,33 @@ impl FromRawFd for TcpListener {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<TcpListener> for OwnedFd {
fn from(tcp_listener: TcpListener) -> Self {
tcp_listener.inner.into_inner().into()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<OwnedFd> for TcpListener {
/// Converts a `RawFd` to a `TcpListener`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(fd: OwnedFd) -> Self {
TcpListener::from_std(From::from(fd))
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpListener {
fn into_raw_socket(self) -> RawSocket {
@@ -229,6 +249,33 @@ impl FromRawSocket for TcpListener {
}
}

#[cfg(windows)]
impl From<TcpListener> for OwnedSocket {
fn from(tcp_listener: TcpListener) -> Self {
tcp_listener.inner.into_inner().into()
}
}

#[cfg(windows)]
impl AsSocket for TcpListener {
fn as_socket(&self) -> BorrowedSocket<'_> {
self.inner.as_socket()
}
}

#[cfg(windows)]
impl From<OwnedSocket> for TcpListener {
/// Converts a `RawSocket` to a `TcpListener`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(socket: OwnedSocket) -> Self {
TcpListener::from_std(From::from(socket))
}
}

impl From<TcpListener> for net::TcpListener {
fn from(listener: TcpListener) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
53 changes: 50 additions & 3 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@ use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::{self, Shutdown, SocketAddr};
#[cfg(any(unix, target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
// TODO: once <https://github.com/rust-lang/rust/issues/126198> is fixed this
// can use `std::os::fd` and be merged with the above.
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket, OwnedSocket, BorrowedSocket, AsSocket};

use crate::io_source::IoSource;
#[cfg(not(target_os = "wasi"))]
@@ -377,13 +377,33 @@ impl FromRawFd for TcpStream {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<TcpStream> for OwnedFd {
fn from(tcp_stream: TcpStream) -> Self {
tcp_stream.inner.into_inner().into()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<OwnedFd> for TcpStream {
/// Converts a `RawFd` to a `TcpStream`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(fd: OwnedFd) -> Self {
TcpStream::from_std(From::from(fd))
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpStream {
fn into_raw_socket(self) -> RawSocket {
@@ -411,6 +431,33 @@ impl FromRawSocket for TcpStream {
}
}

#[cfg(windows)]
impl From<TcpStream> for OwnedSocket {
fn from(tcp_stream: TcpStream) -> Self {
tcp_stream.inner.into_inner().into()
}
}

#[cfg(windows)]
impl AsSocket for TcpStream {
fn as_socket(&self) -> BorrowedSocket<'_> {
self.inner.as_socket()
}
}

#[cfg(windows)]
impl From<OwnedSocket> for TcpStream {
/// Converts a `RawSocket` to a `TcpStream`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(socket: OwnedSocket) -> Self {
TcpStream::from_std(From::from(socket))
}
}

impl From<TcpStream> for net::TcpStream {
fn from(stream: TcpStream) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
53 changes: 50 additions & 3 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
@@ -9,13 +9,13 @@

use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
#[cfg(any(unix, target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
// TODO: once <https://github.com/rust-lang/rust/issues/126198> is fixed this
// can use `std::os::fd` and be merged with the above.
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket, AsSocket, OwnedSocket, BorrowedSocket};
use std::{fmt, io, net};

use crate::io_source::IoSource;
@@ -671,13 +671,33 @@ impl FromRawFd for UdpSocket {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<UdpSocket> for OwnedFd {
fn from(udp_socket: UdpSocket) -> Self {
udp_socket.inner.into_inner().into()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for UdpSocket {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl From<OwnedFd> for UdpSocket {
/// Converts a `RawFd` to a `UdpSocket`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(fd: OwnedFd) -> Self {
UdpSocket::from_std(From::from(fd))
}
}

#[cfg(windows)]
impl IntoRawSocket for UdpSocket {
fn into_raw_socket(self) -> RawSocket {
@@ -705,6 +725,33 @@ impl FromRawSocket for UdpSocket {
}
}

#[cfg(windows)]
impl From<UdpSocket> for OwnedSocket {
fn from(udp_socket: UdpSocket) -> Self {
udp_socket.inner.into_inner().into()
}
}

#[cfg(windows)]
impl AsSocket for UdpSocket {
fn as_socket(&self) -> BorrowedSocket<'_> {
self.inner.as_socket()
}
}

#[cfg(windows)]
impl From<OwnedSocket> for UdpSocket {
/// Converts a `RawSocket` to a `UdpSocket`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
fn from(socket: OwnedSocket) -> Self {
UdpSocket::from_std(From::from(socket))
}
}

impl From<UdpSocket> for net::UdpSocket {
fn from(socket: UdpSocket) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
14 changes: 13 additions & 1 deletion src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::net::Shutdown;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
use std::{fmt, io};
@@ -249,8 +249,20 @@ impl From<UnixDatagram> for net::UnixDatagram {
}
}

impl From<UnixDatagram> for OwnedFd {
fn from(unix_datagram: UnixDatagram) -> Self {
unix_datagram.inner.into_inner().into()
}
}

impl AsFd for UnixDatagram {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

impl From<OwnedFd> for UnixDatagram {
fn from(fd: OwnedFd) -> Self {
UnixDatagram::from_std(From::from(fd))
}
}
14 changes: 13 additions & 1 deletion src/net/uds/listener.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
use std::{fmt, io};
@@ -118,8 +118,20 @@ impl From<UnixListener> for net::UnixListener {
}
}

impl From<UnixListener> for OwnedFd {
fn from(unix_listener: UnixListener) -> Self {
unix_listener.inner.into_inner().into()
}
}

impl AsFd for UnixListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

impl From<OwnedFd> for UnixListener {
fn from(fd: OwnedFd) -> Self {
UnixListener::from_std(From::from(fd))
}
}
14 changes: 13 additions & 1 deletion src/net/uds/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::Shutdown;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd, OwnedFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;

@@ -262,8 +262,20 @@ impl From<UnixStream> for net::UnixStream {
}
}

impl From<UnixStream> for OwnedFd {
fn from(unix_stream: UnixStream) -> Self {
unix_stream.inner.into_inner().into()
}
}

impl AsFd for UnixStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

impl From<OwnedFd> for UnixStream {
fn from(fd: OwnedFd) -> Self {
UnixStream::from_std(From::from(fd))
}
}
30 changes: 29 additions & 1 deletion src/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ pub(crate) fn new_raw() -> io::Result<[RawFd; 2]> {
cfg_os_ext! {
use std::fs::File;
use std::io::{IoSlice, IoSliceMut, Read, Write};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd};
use std::process::{ChildStderr, ChildStdin, ChildStdout};

use crate::io_source::IoSource;
@@ -378,12 +378,26 @@ impl IntoRawFd for Sender {
}
}

impl From<Sender> for OwnedFd {
fn from(sender: Sender) -> Self {
sender.inner.into_inner().into()
}
}

impl AsFd for Sender {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

impl From<OwnedFd> for Sender {
fn from(fd: OwnedFd) -> Self {
Sender {
inner: IoSource::new(File::from(fd)),
}
}
}

/// Receiving end of an Unix pipe.
///
/// See [`new`] for documentation, including examples.
@@ -551,12 +565,26 @@ impl FromRawFd for Receiver {
}
}

impl From<Receiver> for OwnedFd {
fn from(receiver: Receiver) -> Self {
receiver.inner.into_inner().into()
}
}

impl AsFd for Receiver {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

impl From<OwnedFd> for Receiver {
fn from(fd: OwnedFd) -> Self {
Receiver {
inner: IoSource::new(File::from(fd)),
}
}
}

#[cfg(not(any(target_os = "illumos", target_os = "solaris", target_os = "vita")))]
fn set_nonblocking(fd: RawFd, nonblocking: bool) -> io::Result<()> {
let value = nonblocking as libc::c_int;