Skip to content

Commit 866869b

Browse files
authored
Rollup merge of #131449 - nickrum:wasip2-net-decouple-fd, r=alexcrichton
Decouple WASIp2 sockets from WasiFd This is a follow up to #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.
2 parents 47344c3 + 01e248f commit 866869b

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

‎library/std/src/os/wasi/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
pubmod ffi;
3737
pubmod fs;
3838
pubmod io;
39+
40+
#[cfg(all(target_os = "wasi", target_env = "p1"))]
3941
pubmod net;
4042

4143
/// A prelude for conveniently writing platform-specific code.

‎library/std/src/sys/pal/wasip2/net.rs‎

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

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

5-
usesuper::fd::WasiFd;
65
usecrate::ffi::CStr;
76
usecrate::io::{self,BorrowedBuf,BorrowedCursor,IoSlice,IoSliceMut};
87
usecrate::net::{Shutdown,SocketAddr};
9-
usecrate::os::wasi::io::{AsFd,AsRawFd,BorrowedFd,FromRawFd,IntoRawFd,RawFd};
8+
usecrate::os::wasi::io::{AsFd,AsRawFd,BorrowedFd,FromRawFd,IntoRawFd,OwnedFd,RawFd};
109
usecrate::sys::unsupported;
11-
usecrate::sys_common::net::{TcpListener,getsockopt, setsockopt, sockaddr_to_addr};
10+
usecrate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
1211
usecrate::sys_common::{AsInner,FromInner,IntoInner};
1312
usecrate::time::{Duration,Instant};
1413
usecrate::{cmp, mem, str};
@@ -71,7 +70,9 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
7170

7271
pubfninit(){}
7372

74-
pubstructSocket(WasiFd);
73+
pubstructWasiSocket(OwnedFd);
74+
75+
pubstructSocket(WasiSocket);
7576

7677
implSocket{
7778
pubfnnew(addr:&SocketAddr,ty:c_int) -> io::Result<Socket>{
@@ -327,53 +328,90 @@ impl Socket {
327328
}
328329
}
329330

330-
implAsInner<WasiFd>forSocket{
331+
implAsInner<OwnedFd>forWasiSocket{
331332
#[inline]
332-
fnas_inner(&self) -> &WasiFd{
333+
fnas_inner(&self) -> &OwnedFd{
333334
&self.0
334335
}
335336
}
336337

337-
implIntoInner<WasiFd>forSocket{
338-
fninto_inner(self) -> WasiFd{
338+
implIntoInner<OwnedFd>forWasiSocket{
339+
fninto_inner(self) -> OwnedFd{
339340
self.0
340341
}
341342
}
342343

343-
implFromInner<WasiFd>forSocket{
344-
fnfrom_inner(inner:WasiFd) -> Socket{
345-
Socket(inner)
344+
implFromInner<OwnedFd>forWasiSocket{
345+
fnfrom_inner(owned_fd:OwnedFd) -> Self{
346+
Self(owned_fd)
346347
}
347348
}
348349

349-
implAsFdforSocket{
350+
implAsFdforWasiSocket{
350351
fnas_fd(&self) -> BorrowedFd<'_>{
351352
self.0.as_fd()
352353
}
353354
}
354355

355-
implAsRawFdforSocket{
356+
implAsRawFdforWasiSocket{
356357
#[inline]
357358
fnas_raw_fd(&self) -> RawFd{
358359
self.0.as_raw_fd()
359360
}
360361
}
361362

362-
implIntoRawFdforSocket{
363+
implIntoRawFdforWasiSocket{
363364
fninto_raw_fd(self) -> RawFd{
364365
self.0.into_raw_fd()
365366
}
366367
}
367368

368-
implFromRawFdforSocket{
369+
implFromRawFdforWasiSocket{
369370
unsafefnfrom_raw_fd(raw_fd:RawFd) -> Self{
370371
unsafe{Self(FromRawFd::from_raw_fd(raw_fd))}
371372
}
372373
}
373374

374-
implAsInner<Socket>forTcpListener{
375+
implAsInner<WasiSocket>forSocket{
376+
#[inline]
377+
fnas_inner(&self) -> &WasiSocket{
378+
&self.0
379+
}
380+
}
381+
382+
implIntoInner<WasiSocket>forSocket{
383+
fninto_inner(self) -> WasiSocket{
384+
self.0
385+
}
386+
}
387+
388+
implFromInner<WasiSocket>forSocket{
389+
fnfrom_inner(sock:WasiSocket) -> Socket{
390+
Socket(sock)
391+
}
392+
}
393+
394+
implAsFdforSocket{
395+
fnas_fd(&self) -> BorrowedFd<'_>{
396+
self.0.as_fd()
397+
}
398+
}
399+
400+
implAsRawFdforSocket{
375401
#[inline]
376-
fnas_inner(&self) -> &Socket{
377-
&self.socket()
402+
fnas_raw_fd(&self) -> RawFd{
403+
self.0.as_raw_fd()
404+
}
405+
}
406+
407+
implIntoRawFdforSocket{
408+
fninto_raw_fd(self) -> RawFd{
409+
self.0.into_raw_fd()
410+
}
411+
}
412+
413+
implFromRawFdforSocket{
414+
unsafefnfrom_raw_fd(raw_fd:RawFd) -> Self{
415+
unsafe{Self(FromRawFd::from_raw_fd(raw_fd))}
378416
}
379417
}

0 commit comments

Comments
 (0)