Skip to content

Commit 5217347

Browse files
committed
Auto merge of rust-lang#87329 - sunfishcode:sunfishcode/io-safety, r=joshtriplett
I/O safety. Introduce `OwnedFd` and `BorrowedFd`, and the `AsFd` trait, and implementations of `AsFd`, `From<OwnedFd>` and `From<T> for OwnedFd` for relevant types, along with Windows counterparts for handles and sockets. Tracking issue: <rust-lang#87074> RFC: <https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md> Highlights: - The doc comments at the top of library/std/src/os/unix/io/mod.rs and library/std/src/os/windows/io/mod.rs - The new types and traits in library/std/src/os/unix/io/fd.rs and library/std/src/os/windows/io/handle.rs - The removal of the `RawHandle` struct the Windows impl, which had the same name as the `RawHandle` type alias, and its functionality is now folded into `Handle`. Managing five levels of wrapping (File wraps sys::fs::File wraps sys::fs::FileDesc wraps OwnedFd wraps RawFd, etc.) made for a fair amount of churn and verbose as/into/from sequences in some places. I've managed to simplify some of them, but I'm open to ideas here. r? `@joshtriplett`
2 parents 9ccf661 + b4dfa19 commit 5217347

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2229
-711
lines changed

library/std/src/fs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,12 @@ impl File {
587587
}
588588
}
589589

590+
// In addition to the `impl`s here, `File` also has `impl`s for
591+
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
592+
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
593+
// `AsHandle`/`From<OwnedHandle>`/`Into<OwnedHandle>` and
594+
// `AsRawHandle`/`IntoRawHandle`/`FromRawHandle` on Windows.
595+
590596
impl AsInner<fs_imp::File> for File {
591597
fn as_inner(&self) -> &fs_imp::File {
592598
&self.inner

library/std/src/net/tcp.rs

+12
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ impl TcpStream {
546546
}
547547
}
548548

549+
// In addition to the `impl`s here, `TcpStream` also has `impl`s for
550+
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
551+
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
552+
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
553+
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
554+
549555
#[stable(feature = "rust1", since = "1.0.0")]
550556
impl Read for TcpStream {
551557
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
@@ -915,6 +921,12 @@ impl TcpListener {
915921
}
916922
}
917923

924+
// In addition to the `impl`s here, `TcpListener` also has `impl`s for
925+
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
926+
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
927+
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
928+
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
929+
918930
#[stable(feature = "rust1", since = "1.0.0")]
919931
impl<'a> Iterator for Incoming<'a> {
920932
type Item = io::Result<TcpStream>;

library/std/src/net/udp.rs

+6
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,12 @@ impl UdpSocket {
779779
}
780780
}
781781

782+
// In addition to the `impl`s here, `UdpSocket` also has `impl`s for
783+
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
784+
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
785+
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
786+
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
787+
782788
impl AsInner<net_imp::UdpSocket> for UdpSocket {
783789
fn as_inner(&self) -> &net_imp::UdpSocket {
784790
&self.0

library/std/src/net/udp/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::io::ErrorKind;
22
use crate::net::test::{next_test_ip4, next_test_ip6};
33
use crate::net::*;
44
use crate::sync::mpsc::channel;
5-
use crate::sys_common::AsInner;
65
use crate::thread;
76
use crate::time::{Duration, Instant};
87

@@ -173,7 +172,7 @@ fn debug() {
173172
let socket_addr = next_test_ip4();
174173

175174
let udpsock = t!(UdpSocket::bind(&socket_addr));
176-
let udpsock_inner = udpsock.0.socket().as_inner();
175+
let udpsock_inner = udpsock.0.socket().as_raw();
177176
let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}", socket_addr, name, udpsock_inner);
178177
assert_eq!(format!("{:?}", udpsock), compare);
179178
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Owned and borrowed Unix-like file descriptors.
2+
3+
#![unstable(feature = "io_safety", issue = "87074")]
4+
#![deny(unsafe_op_in_unsafe_fn)]
5+
6+
// `RawFd`, `AsRawFd`, etc.
7+
pub mod raw;
8+
9+
// `OwnedFd`, `AsFd`, etc.
10+
pub mod owned;
11+
12+
// Implementations for `AsRawFd` etc. for network types.
13+
mod net;

library/std/src/os/unix/net/raw_fd.rs renamed to library/std/src/os/fd/net.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
1+
use crate::os::fd::owned::OwnedFd;
2+
use crate::os::fd::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
23
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
34
use crate::{net, sys};
45

@@ -8,7 +9,7 @@ macro_rules! impl_as_raw_fd {
89
impl AsRawFd for net::$t {
910
#[inline]
1011
fn as_raw_fd(&self) -> RawFd {
11-
*self.as_inner().socket().as_inner()
12+
self.as_inner().socket().as_raw_fd()
1213
}
1314
}
1415
)*};
@@ -21,8 +22,10 @@ macro_rules! impl_from_raw_fd {
2122
impl FromRawFd for net::$t {
2223
#[inline]
2324
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
24-
let socket = sys::net::Socket::from_inner(fd);
25-
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
25+
unsafe {
26+
let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
27+
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
28+
}
2629
}
2730
}
2831
)*};
@@ -35,7 +38,7 @@ macro_rules! impl_into_raw_fd {
3538
impl IntoRawFd for net::$t {
3639
#[inline]
3740
fn into_raw_fd(self) -> RawFd {
38-
self.into_inner().into_socket().into_inner()
41+
self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
3942
}
4043
}
4144
)*};

0 commit comments

Comments
 (0)