@@ -11,6 +11,7 @@ use super::SocketAddr;
1111use crate :: io:: { self , Read , Write } ;
1212use crate :: os:: unix:: io:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
1313use crate :: path:: Path ;
14+ use crate :: sync:: Arc ;
1415use crate :: task:: { Context , Poll } ;
1516
1617/// A Unix stream socket.
@@ -36,8 +37,9 @@ use crate::task::{Context, Poll};
3637/// #
3738/// # Ok(()) }) }
3839/// ```
40+ #[ derive( Clone ) ]
3941pub struct UnixStream {
40- pub ( super ) watcher : Async < StdUnixStream > ,
42+ pub ( super ) watcher : Arc < Async < StdUnixStream > > ,
4143}
4244
4345impl UnixStream {
@@ -56,7 +58,7 @@ impl UnixStream {
5658 /// ```
5759 pub async fn connect < P : AsRef < Path > > ( path : P ) -> io:: Result < UnixStream > {
5860 let path = path. as_ref ( ) . to_owned ( ) ;
59- let stream = Async :: < StdUnixStream > :: connect ( path) . await ?;
61+ let stream = Arc :: new ( Async :: < StdUnixStream > :: connect ( path) . await ?) ;
6062
6163 Ok ( UnixStream { watcher : stream } )
6264 }
@@ -78,8 +80,12 @@ impl UnixStream {
7880 /// ```
7981 pub fn pair ( ) -> io:: Result < ( UnixStream , UnixStream ) > {
8082 let ( a, b) = Async :: < StdUnixStream > :: pair ( ) ?;
81- let a = UnixStream { watcher : a } ;
82- let b = UnixStream { watcher : b } ;
83+ let a = UnixStream {
84+ watcher : Arc :: new ( a) ,
85+ } ;
86+ let b = UnixStream {
87+ watcher : Arc :: new ( b) ,
88+ } ;
8389 Ok ( ( a, b) )
8490 }
8591
@@ -158,7 +164,7 @@ impl Read for &UnixStream {
158164 cx : & mut Context < ' _ > ,
159165 buf : & mut [ u8 ] ,
160166 ) -> Poll < io:: Result < usize > > {
161- Pin :: new ( & mut & self . watcher ) . poll_read ( cx, buf)
167+ Pin :: new ( & mut & * self . watcher ) . poll_read ( cx, buf)
162168 }
163169}
164170
@@ -186,15 +192,15 @@ impl Write for &UnixStream {
186192 cx : & mut Context < ' _ > ,
187193 buf : & [ u8 ] ,
188194 ) -> Poll < io:: Result < usize > > {
189- Pin :: new ( & mut & self . watcher ) . poll_write ( cx, buf)
195+ Pin :: new ( & mut & * self . watcher ) . poll_write ( cx, buf)
190196 }
191197
192198 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
193- Pin :: new ( & mut & self . watcher ) . poll_flush ( cx)
199+ Pin :: new ( & mut & * self . watcher ) . poll_flush ( cx)
194200 }
195201
196202 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
197- Pin :: new ( & mut & self . watcher ) . poll_close ( cx)
203+ Pin :: new ( & mut & * self . watcher ) . poll_close ( cx)
198204 }
199205}
200206
@@ -219,7 +225,7 @@ impl From<StdUnixStream> for UnixStream {
219225 /// Converts a `std::os::unix::net::UnixStream` into its asynchronous equivalent.
220226 fn from ( stream : StdUnixStream ) -> UnixStream {
221227 let stream = Async :: new ( stream) . expect ( "UnixStream is known to be good" ) ;
222- UnixStream { watcher : stream }
228+ UnixStream { watcher : Arc :: new ( stream) }
223229 }
224230}
225231
@@ -238,6 +244,6 @@ impl FromRawFd for UnixStream {
238244
239245impl IntoRawFd for UnixStream {
240246 fn into_raw_fd ( self ) -> RawFd {
241- self . watcher . into_raw_fd ( )
247+ self . as_raw_fd ( )
242248 }
243249}
0 commit comments