Skip to content

Commit 9e108da

Browse files
committed
Add wasm32-wasip2 target support
1 parent 33291e2 commit 9e108da

File tree

7 files changed

+637
-87
lines changed

7 files changed

+637
-87
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ targets = [
5050
[package.metadata.playground]
5151
features = ["all"]
5252

53-
[target."cfg(unix)".dependencies]
53+
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
5454
libc = "0.2.171"
5555

5656
[target.'cfg(windows)'.dependencies.windows-sys]

src/lib.rs

+38-21
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@
6060
// Disallow warnings in examples.
6161
#![doc(test(attr(deny(warnings))))]
6262

63+
#[cfg(not(target_os = "wasi"))]
6364
use std::fmt;
64-
#[cfg(not(target_os = "redox"))]
65+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6566
use std::io::IoSlice;
66-
#[cfg(not(target_os = "redox"))]
67+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6768
use std::marker::PhantomData;
68-
#[cfg(not(target_os = "redox"))]
69+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6970
use std::mem;
71+
#[cfg(not(target_os = "wasi"))]
7072
use std::mem::MaybeUninit;
7173
use std::net::SocketAddr;
74+
#[cfg(not(target_os = "wasi"))]
7275
use std::ops::{Deref, DerefMut};
7376
use std::time::Duration;
7477

@@ -109,7 +112,7 @@ macro_rules! from {
109112
($from: ty, $for: ty) => {
110113
impl From<$from> for $for {
111114
fn from(socket: $from) -> $for {
112-
#[cfg(unix)]
115+
#[cfg(any(unix, target_os = "wasi"))]
113116
unsafe {
114117
<$for>::from_raw_fd(socket.into_raw_fd())
115118
}
@@ -178,9 +181,10 @@ mod sockref;
178181

179182
#[cfg_attr(unix, path = "sys/unix.rs")]
180183
#[cfg_attr(windows, path = "sys/windows.rs")]
184+
#[cfg_attr(target_os = "wasi", path = "sys/wasi.rs")]
181185
mod sys;
182186

183-
#[cfg(not(any(windows, unix)))]
187+
#[cfg(not(any(windows, unix, all(target_os = "wasi", target_env = "p2"))))]
184188
compile_error!("Socket2 doesn't support the compile target");
185189

186190
use sys::c_int;
@@ -218,6 +222,7 @@ impl Domain {
218222
pub const IPV6: Domain = Domain(sys::AF_INET6);
219223

220224
/// Domain for Unix socket communication, corresponding to `AF_UNIX`.
225+
#[cfg(not(target_os = "wasi"))]
221226
pub const UNIX: Domain = Domain(sys::AF_UNIX);
222227

223228
/// Returns the correct domain for `address`.
@@ -271,11 +276,14 @@ impl Type {
271276
pub const DCCP: Type = Type(sys::SOCK_DCCP);
272277

273278
/// Type corresponding to `SOCK_SEQPACKET`.
274-
#[cfg(all(feature = "all", not(target_os = "espidf")))]
279+
#[cfg(all(feature = "all", not(any(target_os = "espidf", target_os = "wasi"))))]
275280
pub const SEQPACKET: Type = Type(sys::SOCK_SEQPACKET);
276281

277282
/// Type corresponding to `SOCK_RAW`.
278-
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
283+
#[cfg(all(
284+
feature = "all",
285+
not(any(target_os = "redox", target_os = "espidf", target_os = "wasi"))
286+
))]
279287
pub const RAW: Type = Type(sys::SOCK_RAW);
280288
}
281289

@@ -302,18 +310,20 @@ impl From<Type> for c_int {
302310
pub struct Protocol(c_int);
303311

304312
impl Protocol {
305-
/// Protocol corresponding to `ICMPv4`.
306-
pub const ICMPV4: Protocol = Protocol(sys::IPPROTO_ICMP);
307-
308-
/// Protocol corresponding to `ICMPv6`.
309-
pub const ICMPV6: Protocol = Protocol(sys::IPPROTO_ICMPV6);
310-
311313
/// Protocol corresponding to `TCP`.
312314
pub const TCP: Protocol = Protocol(sys::IPPROTO_TCP);
313315

314316
/// Protocol corresponding to `UDP`.
315317
pub const UDP: Protocol = Protocol(sys::IPPROTO_UDP);
316318

319+
#[cfg(not(target_os = "wasi"))]
320+
/// Protocol corresponding to `ICMPv4`.
321+
pub const ICMPV4: Protocol = Protocol(sys::IPPROTO_ICMP);
322+
323+
#[cfg(not(target_os = "wasi"))]
324+
/// Protocol corresponding to `ICMPv6`.
325+
pub const ICMPV6: Protocol = Protocol(sys::IPPROTO_ICMPV6);
326+
317327
#[cfg(target_os = "linux")]
318328
/// Protocol corresponding to `MPTCP`.
319329
pub const MPTCP: Protocol = Protocol(sys::IPPROTO_MPTCP);
@@ -358,11 +368,11 @@ impl From<Protocol> for c_int {
358368
/// Flags for incoming messages.
359369
///
360370
/// Flags provide additional information about incoming messages.
361-
#[cfg(not(target_os = "redox"))]
371+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
362372
#[derive(Copy, Clone, Eq, PartialEq)]
363373
pub struct RecvFlags(c_int);
364374

365-
#[cfg(not(target_os = "redox"))]
375+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
366376
impl RecvFlags {
367377
/// Check if the message contains a truncated datagram.
368378
///
@@ -380,15 +390,18 @@ impl RecvFlags {
380390
/// A version of [`IoSliceMut`] that allows the buffer to be uninitialised.
381391
///
382392
/// [`IoSliceMut`]: std::io::IoSliceMut
393+
#[cfg(not(target_os = "wasi"))]
383394
#[repr(transparent)]
384395
pub struct MaybeUninitSlice<'a>(sys::MaybeUninitSlice<'a>);
385396

397+
#[cfg(not(target_os = "wasi"))]
386398
impl<'a> fmt::Debug for MaybeUninitSlice<'a> {
387399
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
388400
fmt::Debug::fmt(self.0.as_slice(), fmt)
389401
}
390402
}
391403

404+
#[cfg(not(target_os = "wasi"))]
392405
impl<'a> MaybeUninitSlice<'a> {
393406
/// Creates a new `MaybeUninitSlice` wrapping a byte slice.
394407
///
@@ -400,6 +413,7 @@ impl<'a> MaybeUninitSlice<'a> {
400413
}
401414
}
402415

416+
#[cfg(not(target_os = "wasi"))]
403417
impl<'a> Deref for MaybeUninitSlice<'a> {
404418
type Target = [MaybeUninit<u8>];
405419

@@ -408,6 +422,7 @@ impl<'a> Deref for MaybeUninitSlice<'a> {
408422
}
409423
}
410424

425+
#[cfg(not(target_os = "wasi"))]
411426
impl<'a> DerefMut for MaybeUninitSlice<'a> {
412427
fn deref_mut(&mut self) -> &mut [MaybeUninit<u8>] {
413428
self.0.as_mut_slice()
@@ -514,6 +529,7 @@ impl TcpKeepalive {
514529
target_os = "macos",
515530
target_os = "netbsd",
516531
target_os = "tvos",
532+
target_os = "wasi",
517533
target_os = "watchos",
518534
target_os = "windows",
519535
))]
@@ -542,6 +558,7 @@ impl TcpKeepalive {
542558
target_os = "macos",
543559
target_os = "netbsd",
544560
target_os = "tvos",
561+
target_os = "wasi",
545562
target_os = "watchos",
546563
)
547564
))]
@@ -557,14 +574,14 @@ impl TcpKeepalive {
557574
///
558575
/// This wraps `msghdr` on Unix and `WSAMSG` on Windows. Also see [`MsgHdrMut`]
559576
/// for the variant used by `recvmsg(2)`.
560-
#[cfg(not(target_os = "redox"))]
577+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
561578
pub struct MsgHdr<'addr, 'bufs, 'control> {
562579
inner: sys::msghdr,
563580
#[allow(clippy::type_complexity)]
564581
_lifetimes: PhantomData<(&'addr SockAddr, &'bufs IoSlice<'bufs>, &'control [u8])>,
565582
}
566583

567-
#[cfg(not(target_os = "redox"))]
584+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
568585
impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
569586
/// Create a new `MsgHdr` with all empty/zero fields.
570587
#[allow(clippy::new_without_default)]
@@ -614,7 +631,7 @@ impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
614631
}
615632
}
616633

617-
#[cfg(not(target_os = "redox"))]
634+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
618635
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdr<'name, 'bufs, 'control> {
619636
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
620637
"MsgHdr".fmt(fmt)
@@ -625,7 +642,7 @@ impl<'name, 'bufs, 'control> fmt::Debug for MsgHdr<'name, 'bufs, 'control> {
625642
///
626643
/// This wraps `msghdr` on Unix and `WSAMSG` on Windows. Also see [`MsgHdr`] for
627644
/// the variant used by `sendmsg(2)`.
628-
#[cfg(not(target_os = "redox"))]
645+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
629646
pub struct MsgHdrMut<'addr, 'bufs, 'control> {
630647
inner: sys::msghdr,
631648
#[allow(clippy::type_complexity)]
@@ -636,7 +653,7 @@ pub struct MsgHdrMut<'addr, 'bufs, 'control> {
636653
)>,
637654
}
638655

639-
#[cfg(not(target_os = "redox"))]
656+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
640657
impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
641658
/// Create a new `MsgHdrMut` with all empty/zero fields.
642659
#[allow(clippy::new_without_default)]
@@ -691,7 +708,7 @@ impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
691708
}
692709
}
693710

694-
#[cfg(not(target_os = "redox"))]
711+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
695712
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdrMut<'name, 'bufs, 'control> {
696713
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
697714
"MsgHdrMut".fmt(fmt)

src/sockaddr.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use std::hash::Hash;
22
use std::mem::{self, size_of, MaybeUninit};
33
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
4+
#[cfg(not(target_os = "wasi"))]
45
use std::path::Path;
56
use std::{fmt, io, ptr};
67

78
#[cfg(windows)]
89
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;
910

11+
#[cfg(not(target_os = "wasi"))]
12+
use crate::sys::AF_UNIX;
1013
use crate::sys::{
1114
c_int, sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET,
12-
AF_INET6, AF_UNIX,
15+
AF_INET6,
1316
};
1417
use crate::Domain;
1518

@@ -146,6 +149,7 @@ impl SockAddr {
146149
/// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path.
147150
///
148151
/// Returns an error if the path is longer than `SUN_LEN`.
152+
#[cfg(not(target_os = "wasi"))]
149153
pub fn unix<P>(path: P) -> io::Result<SockAddr>
150154
where
151155
P: AsRef<Path>,
@@ -201,6 +205,7 @@ impl SockAddr {
201205

202206
/// Returns true if this address is of a unix socket (for local interprocess communication),
203207
/// i.e. it is from the `AF_UNIX` family, false otherwise.
208+
#[cfg(not(target_os = "wasi"))]
204209
pub fn is_unix(&self) -> bool {
205210
self.storage.ss_family == AF_UNIX as sa_family_t
206211
}
@@ -225,7 +230,7 @@ impl SockAddr {
225230
ip,
226231
port,
227232
addr.sin6_flowinfo,
228-
#[cfg(unix)]
233+
#[cfg(any(unix, target_os = "wasi"))]
229234
addr.sin6_scope_id,
230235
#[cfg(windows)]
231236
unsafe {
@@ -282,7 +287,10 @@ impl From<SocketAddrV4> for SockAddr {
282287
storage.sin_family = AF_INET as sa_family_t;
283288
storage.sin_port = addr.port().to_be();
284289
storage.sin_addr = crate::sys::to_in_addr(addr.ip());
285-
storage.sin_zero = Default::default();
290+
#[cfg(not(target_os = "wasi"))]
291+
{
292+
storage.sin_zero = Default::default();
293+
}
286294
mem::size_of::<sockaddr_in>() as socklen_t
287295
};
288296
#[cfg(any(
@@ -317,7 +325,7 @@ impl From<SocketAddrV6> for SockAddr {
317325
storage.sin6_port = addr.port().to_be();
318326
storage.sin6_addr = crate::sys::to_in6_addr(addr.ip());
319327
storage.sin6_flowinfo = addr.flowinfo();
320-
#[cfg(unix)]
328+
#[cfg(any(unix, target_os = "wasi"))]
321329
{
322330
storage.sin6_scope_id = addr.scope_id();
323331
}
@@ -401,6 +409,7 @@ mod tests {
401409
let addr = SockAddr::from(std);
402410
assert!(addr.is_ipv4());
403411
assert!(!addr.is_ipv6());
412+
#[cfg(not(target_os = "wasi"))]
404413
assert!(!addr.is_unix());
405414
assert_eq!(addr.family(), AF_INET as sa_family_t);
406415
assert_eq!(addr.domain(), Domain::IPV4);
@@ -429,6 +438,7 @@ mod tests {
429438
let addr = SockAddr::from(std);
430439
assert!(addr.is_ipv6());
431440
assert!(!addr.is_ipv4());
441+
#[cfg(not(target_os = "wasi"))]
432442
assert!(!addr.is_unix());
433443
assert_eq!(addr.family(), AF_INET6 as sa_family_t);
434444
assert_eq!(addr.domain(), Domain::IPV6);

0 commit comments

Comments
 (0)