5858// Disallow warnings in examples.
5959#![ doc( test( attr( deny( warnings) ) ) ) ]
6060
61+ #[ cfg( not( target_os = "wasi" ) ) ]
6162use std:: fmt;
62- #[ cfg( not( target_os = "redox" ) ) ]
63+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6364use std:: io:: IoSlice ;
64- #[ cfg( not( target_os = "redox" ) ) ]
65+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6566use std:: marker:: PhantomData ;
66- #[ cfg( not( target_os = "redox" ) ) ]
67+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
6768use std:: mem;
69+ #[ cfg( not( target_os = "wasi" ) ) ]
6870use std:: mem:: MaybeUninit ;
6971use std:: net:: SocketAddr ;
72+ #[ cfg( not( target_os = "wasi" ) ) ]
7073use std:: ops:: { Deref , DerefMut } ;
7174use 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" ) ]
179183mod sys;
180184
181- #[ cfg( not( any( windows, unix) ) ) ]
185+ #[ cfg( not( any( windows, unix, all ( target_os = "wasi" , target_env = "p2" ) ) ) ) ]
182186compile_error ! ( "Socket2 doesn't support the compile target" ) ;
183187
184188use 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 {
306320pub struct Protocol ( c_int ) ;
307321
308322impl 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 ) ]
369385pub struct RecvFlags ( c_int ) ;
370386
371- #[ cfg( not( target_os = "redox" ) ) ]
387+ #[ cfg( not( any ( target_os = "redox" , target_os = "wasi" ) ) ) ]
372388impl 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) ]
390407pub struct MaybeUninitSlice < ' a > ( sys:: MaybeUninitSlice < ' a > ) ;
391408
409+ #[ cfg( not( target_os = "wasi" ) ) ]
392410impl < ' 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" ) ) ]
398417impl < ' 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" ) ) ]
409429impl < ' 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" ) ) ]
417438impl < ' 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" ) ) ) ]
605630pub 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" ) ) ) ]
612637impl < ' 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" ) ) ) ]
662687impl < ' 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" ) ) ) ]
673698pub 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" ) ) ) ]
684709impl < ' 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" ) ) ) ]
739764impl < ' name , ' bufs , ' control > fmt:: Debug for MsgHdrMut < ' name , ' bufs , ' control > {
740765 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
741766 "MsgHdrMut" . fmt ( fmt)
0 commit comments