Skip to content

Commit

Permalink
Unrolled build for rust-lang#131449
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#131449 - nickrum:wasip2-net-decouple-fd, r=alexcrichton

Decouple WASIp2 sockets from WasiFd

This is a follow up to rust-lang#129638, decoupling WASIp2's socket implementation from WASIp1's `WasiFd` as discussed with `@alexcrichton.`

Quite a few trait implementations in `std::os::fd` rely on the fact that there is an additional layer of abstraction between `Socket` and `OwnedFd`. I thus had to add a thin `WasiSocket` wrapper struct that just "forwards" to `OwnedFd`. Alternatively, I could have added a lot of conditional compilation to `std::os::fd`, which feels even worse.

Since `WasiFd::sock_accept` is no longer accessible from `TcpListener` and since WASIp2 has proper support for accepting sockets through `Socket::accept`, the `std::os::wasi::net` module has been removed from WASIp2, which only contains a single `TcpListenerExt` trait with a `sock_accept` method as well as an implementation for `TcpListener`. Let me know if this is an acceptable solution.
  • Loading branch information
rust-timer authored Oct 10, 2024
2 parents fc0f045 + 01e248f commit 800c2cf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
2 changes: 2 additions & 0 deletions library/std/src/os/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
pub mod ffi;
pub mod fs;
pub mod io;

#[cfg(all(target_os = "wasi", target_env = "p1"))]
pub mod net;

/// A prelude for conveniently writing platform-specific code.
Expand Down
74 changes: 56 additions & 18 deletions library/std/src/sys/pal/wasip2/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use libc::{c_int, c_void, size_t};

use super::fd::WasiFd;
use crate::ffi::CStr;
use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
use crate::net::{Shutdown, SocketAddr};
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use crate::sys::unsupported;
use crate::sys_common::net::{TcpListener, getsockopt, setsockopt, sockaddr_to_addr};
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::time::{Duration, Instant};
use crate::{cmp, mem, str};
Expand Down Expand Up @@ -71,7 +70,9 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {

pub fn init() {}

pub struct Socket(WasiFd);
pub struct WasiSocket(OwnedFd);

pub struct Socket(WasiSocket);

impl Socket {
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
Expand Down Expand Up @@ -327,53 +328,90 @@ impl Socket {
}
}

impl AsInner<WasiFd> for Socket {
impl AsInner<OwnedFd> for WasiSocket {
#[inline]
fn as_inner(&self) -> &WasiFd {
fn as_inner(&self) -> &OwnedFd {
&self.0
}
}

impl IntoInner<WasiFd> for Socket {
fn into_inner(self) -> WasiFd {
impl IntoInner<OwnedFd> for WasiSocket {
fn into_inner(self) -> OwnedFd {
self.0
}
}

impl FromInner<WasiFd> for Socket {
fn from_inner(inner: WasiFd) -> Socket {
Socket(inner)
impl FromInner<OwnedFd> for WasiSocket {
fn from_inner(owned_fd: OwnedFd) -> Self {
Self(owned_fd)
}
}

impl AsFd for Socket {
impl AsFd for WasiSocket {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

impl AsRawFd for Socket {
impl AsRawFd for WasiSocket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}

impl IntoRawFd for Socket {
impl IntoRawFd for WasiSocket {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
}
}

impl FromRawFd for Socket {
impl FromRawFd for WasiSocket {
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
unsafe { Self(FromRawFd::from_raw_fd(raw_fd)) }
}
}

impl AsInner<Socket> for TcpListener {
impl AsInner<WasiSocket> for Socket {
#[inline]
fn as_inner(&self) -> &WasiSocket {
&self.0
}
}

impl IntoInner<WasiSocket> for Socket {
fn into_inner(self) -> WasiSocket {
self.0
}
}

impl FromInner<WasiSocket> for Socket {
fn from_inner(sock: WasiSocket) -> Socket {
Socket(sock)
}
}

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

impl AsRawFd for Socket {
#[inline]
fn as_inner(&self) -> &Socket {
&self.socket()
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}

impl IntoRawFd for Socket {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
}
}

impl FromRawFd for Socket {
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
unsafe { Self(FromRawFd::from_raw_fd(raw_fd)) }
}
}

0 comments on commit 800c2cf

Please sign in to comment.