6060// Disallow warnings in examples.
6161#![ doc( test( attr( deny( warnings) ) ) ) ]
6262
63+ #[ cfg( not( target_os = "wasi" ) ) ]
6364use std:: fmt;
64- #[ cfg( not( target_os = "redox" ) ) ]
65+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6566use std:: io:: IoSlice ;
66- #[ cfg( not( target_os = "redox" ) ) ]
67+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6768use std:: marker:: PhantomData ;
68- #[ cfg( not( target_os = "redox" ) ) ]
69+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6970use std:: mem;
71+ #[ cfg( not( target_os = "wasi" ) ) ]
7072use std:: mem:: MaybeUninit ;
7173use std:: net:: SocketAddr ;
74+ #[ cfg( not( target_os = "wasi" ) ) ]
7275use std:: ops:: { Deref , DerefMut } ;
7376use 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" ) ]
181185mod sys;
182186
183- #[ cfg( not( any( windows, unix) ) ) ]
187+ #[ cfg( not( any( windows, unix, all ( target_os = "wasi" , target_env = "p2" ) ) ) ) ]
184188compile_error ! ( "Socket2 doesn't support the compile target" ) ;
185189
186190use 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 {
302310pub struct Protocol ( c_int ) ;
303311
304312impl 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 ) ]
363373pub struct RecvFlags ( c_int ) ;
364374
365- #[ cfg( not( target_os = "redox" ) ) ]
375+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
366376impl 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) ]
384395pub struct MaybeUninitSlice < ' a > ( sys:: MaybeUninitSlice < ' a > ) ;
385396
397+ #[ cfg( not( target_os = "wasi" ) ) ]
386398impl < ' 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" ) ) ]
392405impl < ' 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" ) ) ]
403417impl < ' 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" ) ) ]
411426impl < ' 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" ) ) ) ]
561578pub 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" ) ) ) ]
568585impl < ' 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" ) ) ) ]
618635impl < ' 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" ) ) ) ]
629646pub 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" ) ) ) ]
640657impl < ' 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" ) ) ) ]
695712impl < ' name , ' bufs , ' control > fmt:: Debug for MsgHdrMut < ' name , ' bufs , ' control > {
696713 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
697714 "MsgHdrMut" . fmt ( fmt)
0 commit comments