Skip to content

Commit 7d3ce41

Browse files
committed
Use OwnedFd/OwnedSocket in Socket
Instead of the workaround where we use std::net::TcpStream.
1 parent d6f97e8 commit 7d3ce41

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

src/socket.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,9 @@ use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};
7373
/// # Ok(()) }
7474
/// ```
7575
pub struct Socket {
76-
inner: Inner,
76+
inner: sys::Socket,
7777
}
7878

79-
/// Store a `TcpStream` internally to take advantage of its niche optimizations on Unix platforms.
80-
pub(crate) type Inner = std::net::TcpStream;
81-
8279
impl Socket {
8380
/// # Safety
8481
///
@@ -88,24 +85,11 @@ impl Socket {
8885
/// inconvenient to mark it as `unsafe`.
8986
pub(crate) fn from_raw(raw: sys::RawSocket) -> Socket {
9087
Socket {
91-
inner: unsafe {
92-
// SAFETY: the caller must ensure that `raw` is a valid file
93-
// descriptor, but when it isn't it could return I/O errors, or
94-
// potentially close a fd it doesn't own. All of that isn't
95-
// memory unsafe, so it's not desired but never memory unsafe or
96-
// causes UB.
97-
//
98-
// However there is one exception. We use `TcpStream` to
99-
// represent the `Socket` internally (see `Inner` type),
100-
// `TcpStream` has a layout optimisation that doesn't allow for
101-
// negative file descriptors (as those are always invalid).
102-
// Violating this assumption (fd never negative) causes UB,
103-
// something we don't want. So check for that we have this
104-
// `assert!`.
105-
#[cfg(unix)]
106-
assert!(raw >= 0, "tried to create a `Socket` with an invalid fd");
107-
sys::socket_from_raw(raw)
108-
},
88+
// SAFETY: the caller must ensure that `raw` is a valid file
89+
// descriptor, but when it isn't it could return I/O errors, or
90+
// potentially close a fd it doesn't own. All of that isn't memory
91+
// unsafe, so it's not desired but never memory unsafe or causes UB.
92+
inner: unsafe { sys::socket_from_raw(raw) },
10993
}
11094
}
11195

src/sys/unix.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,17 +859,18 @@ impl SockAddr {
859859
}
860860
}
861861

862+
pub(crate) type Socket = std::os::fd::OwnedFd;
862863
pub(crate) type RawSocket = c_int;
863864

864-
pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> crate::socket::Inner {
865-
crate::socket::Inner::from_raw_fd(socket)
865+
pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> Socket {
866+
Socket::from_raw_fd(socket)
866867
}
867868

868-
pub(crate) fn socket_as_raw(socket: &crate::socket::Inner) -> RawSocket {
869+
pub(crate) fn socket_as_raw(socket: &Socket) -> RawSocket {
869870
socket.as_raw_fd()
870871
}
871872

872-
pub(crate) fn socket_into_raw(socket: crate::socket::Inner) -> RawSocket {
873+
pub(crate) fn socket_into_raw(socket: Socket) -> RawSocket {
873874
socket.into_raw_fd()
874875
}
875876

src/sys/windows.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,18 @@ fn init() {
235235
});
236236
}
237237

238+
pub(crate) type Socket = std::os::windows::io::OwnedSocket;
238239
pub(crate) type RawSocket = windows_sys::Win32::Networking::WinSock::SOCKET;
239240

240-
pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> crate::socket::Inner {
241-
crate::socket::Inner::from_raw_socket(socket as StdRawSocket)
241+
pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> Socket {
242+
Socket::from_raw_socket(socket as StdRawSocket)
242243
}
243244

244-
pub(crate) fn socket_as_raw(socket: &crate::socket::Inner) -> RawSocket {
245+
pub(crate) fn socket_as_raw(socket: &Socket) -> RawSocket {
245246
socket.as_raw_socket() as RawSocket
246247
}
247248

248-
pub(crate) fn socket_into_raw(socket: crate::socket::Inner) -> RawSocket {
249+
pub(crate) fn socket_into_raw(socket: Socket) -> RawSocket {
249250
socket.into_raw_socket() as RawSocket
250251
}
251252

tests/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn protocol_fmt_debug() {
140140
}
141141

142142
#[test]
143-
#[should_panic = "tried to create a `Socket` with an invalid fd"]
143+
#[should_panic = "fd != -1"]
144144
#[cfg(unix)]
145145
fn from_invalid_raw_fd_should_panic() {
146146
use std::os::unix::io::FromRawFd;

0 commit comments

Comments
 (0)