Skip to content

Commit a481e6e

Browse files
committed
Add wasm32-wasip2 target support
1 parent c78061f commit a481e6e

File tree

7 files changed

+694
-110
lines changed

7 files changed

+694
-110
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ targets = ["aarch64-apple-ios", "aarch64-linux-android", "x86_64-apple-darwin",
3434
[package.metadata.playground]
3535
features = ["all"]
3636

37-
[target."cfg(unix)".dependencies]
37+
[target.'cfg(any(unix, target_os = "wasi"))'.dependencies]
3838
libc = "0.2.162"
3939

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

src/lib.rs

+49-24
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,18 @@
5858
// Disallow warnings in examples.
5959
#![doc(test(attr(deny(warnings))))]
6060

61+
#[cfg(not(target_os = "wasi"))]
6162
use std::fmt;
62-
#[cfg(not(target_os = "redox"))]
63+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6364
use std::io::IoSlice;
64-
#[cfg(not(target_os = "redox"))]
65+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6566
use std::marker::PhantomData;
66-
#[cfg(not(target_os = "redox"))]
67+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
6768
use std::mem;
69+
#[cfg(not(target_os = "wasi"))]
6870
use std::mem::MaybeUninit;
6971
use std::net::SocketAddr;
72+
#[cfg(not(target_os = "wasi"))]
7073
use std::ops::{Deref, DerefMut};
7174
use std::time::Duration;
7275

@@ -107,7 +110,7 @@ macro_rules! from {
107110
($from: ty, $for: ty) => {
108111
impl From<$from> for $for {
109112
fn from(socket: $from) -> $for {
110-
#[cfg(unix)]
113+
#[cfg(any(unix, target_os = "wasi"))]
111114
unsafe {
112115
<$for>::from_raw_fd(socket.into_raw_fd())
113116
}
@@ -176,9 +179,10 @@ mod sockref;
176179

177180
#[cfg_attr(unix, path = "sys/unix.rs")]
178181
#[cfg_attr(windows, path = "sys/windows.rs")]
182+
#[cfg_attr(target_os = "wasi", path = "sys/wasi.rs")]
179183
mod sys;
180184

181-
#[cfg(not(any(windows, unix)))]
185+
#[cfg(not(any(windows, unix, all(target_os = "wasi", target_env = "p2"))))]
182186
compile_error!("Socket2 doesn't support the compile target");
183187

184188
use sys::c_int;
@@ -216,6 +220,7 @@ impl Domain {
216220
pub const IPV6: Domain = Domain(sys::AF_INET6);
217221

218222
/// Domain for Unix socket communication, corresponding to `AF_UNIX`.
223+
#[cfg(not(target_os = "wasi"))]
219224
pub const UNIX: Domain = Domain(sys::AF_UNIX);
220225

221226
/// Returns the correct domain for `address`.
@@ -270,15 +275,24 @@ impl Type {
270275
pub const DCCP: Type = Type(sys::SOCK_DCCP);
271276

272277
/// Type corresponding to `SOCK_SEQPACKET`.
273-
#[cfg(all(feature = "all", not(target_os = "espidf")))]
274-
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "espidf")))))]
278+
#[cfg(all(feature = "all", not(any(target_os = "espidf", target_os = "wasi"))))]
279+
#[cfg_attr(
280+
docsrs,
281+
doc(cfg(all(feature = "all", not(any(target_os = "espidf", target_os = "wasi")))))
282+
)]
275283
pub const SEQPACKET: Type = Type(sys::SOCK_SEQPACKET);
276284

277285
/// Type corresponding to `SOCK_RAW`.
278-
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
286+
#[cfg(all(
287+
feature = "all",
288+
not(any(target_os = "redox", target_os = "espidf", target_os = "wasi"))
289+
))]
279290
#[cfg_attr(
280291
docsrs,
281-
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
292+
doc(cfg(all(
293+
feature = "all",
294+
not(any(target_os = "redox", target_os = "espidf", target_os = "wasi"))
295+
)))
282296
)]
283297
pub const RAW: Type = Type(sys::SOCK_RAW);
284298
}
@@ -306,18 +320,20 @@ impl From<Type> for c_int {
306320
pub struct Protocol(c_int);
307321

308322
impl Protocol {
309-
/// Protocol corresponding to `ICMPv4`.
310-
pub const ICMPV4: Protocol = Protocol(sys::IPPROTO_ICMP);
311-
312-
/// Protocol corresponding to `ICMPv6`.
313-
pub const ICMPV6: Protocol = Protocol(sys::IPPROTO_ICMPV6);
314-
315323
/// Protocol corresponding to `TCP`.
316324
pub const TCP: Protocol = Protocol(sys::IPPROTO_TCP);
317325

318326
/// Protocol corresponding to `UDP`.
319327
pub const UDP: Protocol = Protocol(sys::IPPROTO_UDP);
320328

329+
#[cfg(not(target_os = "wasi"))]
330+
/// Protocol corresponding to `ICMPv4`.
331+
pub const ICMPV4: Protocol = Protocol(sys::IPPROTO_ICMP);
332+
333+
#[cfg(not(target_os = "wasi"))]
334+
/// Protocol corresponding to `ICMPv6`.
335+
pub const ICMPV6: Protocol = Protocol(sys::IPPROTO_ICMPV6);
336+
321337
#[cfg(target_os = "linux")]
322338
/// Protocol corresponding to `MPTCP`.
323339
pub const MPTCP: Protocol = Protocol(sys::IPPROTO_MPTCP);
@@ -363,12 +379,12 @@ impl From<Protocol> for c_int {
363379
/// Flags for incoming messages.
364380
///
365381
/// Flags provide additional information about incoming messages.
366-
#[cfg(not(target_os = "redox"))]
367-
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
382+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
383+
#[cfg_attr(docsrs, doc(cfg(not(any(target_os = "redox", target_os = "wasi")))))]
368384
#[derive(Copy, Clone, Eq, PartialEq)]
369385
pub struct RecvFlags(c_int);
370386

371-
#[cfg(not(target_os = "redox"))]
387+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
372388
impl RecvFlags {
373389
/// Check if the message contains a truncated datagram.
374390
///
@@ -386,15 +402,18 @@ impl RecvFlags {
386402
/// A version of [`IoSliceMut`] that allows the buffer to be uninitialised.
387403
///
388404
/// [`IoSliceMut`]: std::io::IoSliceMut
405+
#[cfg(not(target_os = "wasi"))]
389406
#[repr(transparent)]
390407
pub struct MaybeUninitSlice<'a>(sys::MaybeUninitSlice<'a>);
391408

409+
#[cfg(not(target_os = "wasi"))]
392410
impl<'a> fmt::Debug for MaybeUninitSlice<'a> {
393411
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
394412
fmt::Debug::fmt(self.0.as_slice(), fmt)
395413
}
396414
}
397415

416+
#[cfg(not(target_os = "wasi"))]
398417
impl<'a> MaybeUninitSlice<'a> {
399418
/// Creates a new `MaybeUninitSlice` wrapping a byte slice.
400419
///
@@ -406,6 +425,7 @@ impl<'a> MaybeUninitSlice<'a> {
406425
}
407426
}
408427

428+
#[cfg(not(target_os = "wasi"))]
409429
impl<'a> Deref for MaybeUninitSlice<'a> {
410430
type Target = [MaybeUninit<u8>];
411431

@@ -414,6 +434,7 @@ impl<'a> Deref for MaybeUninitSlice<'a> {
414434
}
415435
}
416436

437+
#[cfg(not(target_os = "wasi"))]
417438
impl<'a> DerefMut for MaybeUninitSlice<'a> {
418439
fn deref_mut(&mut self) -> &mut [MaybeUninit<u8>] {
419440
self.0.as_mut_slice()
@@ -520,6 +541,7 @@ impl TcpKeepalive {
520541
target_os = "macos",
521542
target_os = "netbsd",
522543
target_os = "tvos",
544+
target_os = "wasi",
523545
target_os = "watchos",
524546
target_os = "windows",
525547
))]
@@ -537,6 +559,7 @@ impl TcpKeepalive {
537559
target_os = "macos",
538560
target_os = "netbsd",
539561
target_os = "tvos",
562+
target_os = "wasi",
540563
target_os = "watchos",
541564
target_os = "windows",
542565
)))
@@ -566,6 +589,7 @@ impl TcpKeepalive {
566589
target_os = "macos",
567590
target_os = "netbsd",
568591
target_os = "tvos",
592+
target_os = "wasi",
569593
target_os = "watchos",
570594
)
571595
))]
@@ -585,6 +609,7 @@ impl TcpKeepalive {
585609
target_os = "macos",
586610
target_os = "netbsd",
587611
target_os = "tvos",
612+
target_os = "wasi",
588613
target_os = "watchos",
589614
)
590615
)))
@@ -601,14 +626,14 @@ impl TcpKeepalive {
601626
///
602627
/// This wraps `msghdr` on Unix and `WSAMSG` on Windows. Also see [`MsgHdrMut`]
603628
/// for the variant used by `recvmsg(2)`.
604-
#[cfg(not(target_os = "redox"))]
629+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
605630
pub struct MsgHdr<'addr, 'bufs, 'control> {
606631
inner: sys::msghdr,
607632
#[allow(clippy::type_complexity)]
608633
_lifetimes: PhantomData<(&'addr SockAddr, &'bufs IoSlice<'bufs>, &'control [u8])>,
609634
}
610635

611-
#[cfg(not(target_os = "redox"))]
636+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
612637
impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
613638
/// Create a new `MsgHdr` with all empty/zero fields.
614639
#[allow(clippy::new_without_default)]
@@ -658,7 +683,7 @@ impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
658683
}
659684
}
660685

661-
#[cfg(not(target_os = "redox"))]
686+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
662687
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdr<'name, 'bufs, 'control> {
663688
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
664689
"MsgHdr".fmt(fmt)
@@ -669,7 +694,7 @@ impl<'name, 'bufs, 'control> fmt::Debug for MsgHdr<'name, 'bufs, 'control> {
669694
///
670695
/// This wraps `msghdr` on Unix and `WSAMSG` on Windows. Also see [`MsgHdr`] for
671696
/// the variant used by `sendmsg(2)`.
672-
#[cfg(not(target_os = "redox"))]
697+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
673698
pub struct MsgHdrMut<'addr, 'bufs, 'control> {
674699
inner: sys::msghdr,
675700
#[allow(clippy::type_complexity)]
@@ -680,7 +705,7 @@ pub struct MsgHdrMut<'addr, 'bufs, 'control> {
680705
)>,
681706
}
682707

683-
#[cfg(not(target_os = "redox"))]
708+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
684709
impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
685710
/// Create a new `MsgHdrMut` with all empty/zero fields.
686711
#[allow(clippy::new_without_default)]
@@ -735,7 +760,7 @@ impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
735760
}
736761
}
737762

738-
#[cfg(not(target_os = "redox"))]
763+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
739764
impl<'name, 'bufs, 'control> fmt::Debug for MsgHdrMut<'name, 'bufs, 'control> {
740765
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
741766
"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)