8
8
9
9
#[ cfg( windows) ]
10
10
fn main ( ) -> std:: io:: Result < ( ) > {
11
+ use std:: ops:: Deref ;
12
+ use std:: os:: windows:: io:: { AsRawSocket , AsSocket , BorrowedSocket } ;
11
13
use std:: path:: PathBuf ;
12
14
13
15
use async_io:: Async ;
14
16
use blocking:: Unblock ;
15
- use futures_lite:: { future, io, prelude:: * } ;
17
+ use futures_lite:: { future, prelude:: * } ;
18
+ use std:: io;
16
19
use tempfile:: tempdir;
17
- use uds_windows:: { UnixListener , UnixStream } ;
20
+
21
+ // n.b.: notgull: uds_windows does not support I/O safety uet, hence the wrapper types
22
+
23
+ struct UnixListener ( uds_windows:: UnixListener ) ;
24
+
25
+ impl From < uds_windows:: UnixListener > for UnixListener {
26
+ fn from ( ul : uds_windows:: UnixListener ) -> Self {
27
+ Self ( ul)
28
+ }
29
+ }
30
+
31
+ impl Deref for UnixListener {
32
+ type Target = uds_windows:: UnixListener ;
33
+
34
+ fn deref ( & self ) -> & uds_windows:: UnixListener {
35
+ & self . 0
36
+ }
37
+ }
38
+
39
+ impl AsSocket for UnixListener {
40
+ fn as_socket ( & self ) -> BorrowedSocket < ' _ > {
41
+ unsafe { BorrowedSocket :: borrow_raw ( self . as_raw_socket ( ) ) }
42
+ }
43
+ }
44
+
45
+ struct UnixStream ( uds_windows:: UnixStream ) ;
46
+
47
+ impl From < uds_windows:: UnixStream > for UnixStream {
48
+ fn from ( ul : uds_windows:: UnixStream ) -> Self {
49
+ Self ( ul)
50
+ }
51
+ }
52
+
53
+ impl Deref for UnixStream {
54
+ type Target = uds_windows:: UnixStream ;
55
+
56
+ fn deref ( & self ) -> & uds_windows:: UnixStream {
57
+ & self . 0
58
+ }
59
+ }
60
+
61
+ impl AsSocket for UnixStream {
62
+ fn as_socket ( & self ) -> BorrowedSocket < ' _ > {
63
+ unsafe { BorrowedSocket :: borrow_raw ( self . as_raw_socket ( ) ) }
64
+ }
65
+ }
66
+
67
+ impl io:: Read for UnixStream {
68
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
69
+ io:: Read :: read ( & mut self . 0 , buf)
70
+ }
71
+ }
72
+
73
+ impl io:: Write for UnixStream {
74
+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
75
+ io:: Write :: write ( & mut self . 0 , buf)
76
+ }
77
+
78
+ fn flush ( & mut self ) -> io:: Result < ( ) > {
79
+ io:: Write :: flush ( & mut self . 0 )
80
+ }
81
+ }
82
+
83
+ unsafe impl async_io:: IoSafe for UnixStream { }
18
84
19
85
async fn client ( addr : PathBuf ) -> io:: Result < ( ) > {
20
86
// Connect to the address.
21
- let stream = Async :: new ( UnixStream :: connect ( addr) ?) ?;
87
+ let stream = Async :: new ( UnixStream :: from ( uds_windows :: UnixStream :: connect ( addr) ?) ) ?;
22
88
println ! ( "Connected to {:?}" , stream. get_ref( ) . peer_addr( ) ?) ;
23
89
24
90
// Pipe the stream to stdout.
25
91
let mut stdout = Unblock :: new ( std:: io:: stdout ( ) ) ;
26
- io:: copy ( & stream, & mut stdout) . await ?;
92
+ futures_lite :: io:: copy ( stream, & mut stdout) . await ?;
27
93
Ok ( ( ) )
28
94
}
29
95
@@ -32,7 +98,7 @@ fn main() -> std::io::Result<()> {
32
98
33
99
future:: block_on ( async {
34
100
// Create a listener.
35
- let listener = Async :: new ( UnixListener :: bind ( & path) ?) ?;
101
+ let listener = Async :: new ( UnixListener :: from ( uds_windows :: UnixListener :: bind ( & path) ?) ) ?;
36
102
println ! ( "Listening on {:?}" , listener. get_ref( ) . local_addr( ) ?) ;
37
103
38
104
future:: try_zip (
@@ -42,7 +108,9 @@ fn main() -> std::io::Result<()> {
42
108
println ! ( "Accepted a client" ) ;
43
109
44
110
// Send a message, drop the stream, and wait for the client.
45
- Async :: new ( stream) ?. write_all ( b"Hello!\n " ) . await ?;
111
+ Async :: new ( UnixStream :: from ( stream) ) ?
112
+ . write_all ( b"Hello!\n " )
113
+ . await ?;
46
114
Ok ( ( ) )
47
115
} ,
48
116
client ( path) ,
0 commit comments