@@ -20,7 +20,7 @@ use prelude::*;
2020use sys:: { mod, timer, retry, c, set_nonblocking, wouldblock} ;
2121use sys:: fs:: { fd_t, FileDesc } ;
2222use sys_common:: net:: * ;
23- use sys_common:: { eof, mkerr_libc} ;
23+ use sys_common:: { AsFileDesc , eof, mkerr_libc} ;
2424
2525fn unix_socket ( ty : libc:: c_int ) -> IoResult < fd_t > {
2626 match unsafe { libc:: socket ( libc:: AF_UNIX , ty, 0 ) } {
@@ -56,7 +56,7 @@ fn addr_to_sockaddr_un(addr: &CString,
5656}
5757
5858struct Inner {
59- fd : fd_t ,
59+ fd : FileDesc ,
6060
6161 // Unused on Linux, where this lock is not necessary.
6262 #[ allow( dead_code) ]
@@ -65,14 +65,10 @@ struct Inner {
6565
6666impl Inner {
6767 fn new ( fd : fd_t ) -> Inner {
68- Inner { fd : fd , lock : unsafe { mutex:: NativeMutex :: new ( ) } }
68+ Inner { fd : FileDesc :: new ( fd , true ) , lock : unsafe { mutex:: NativeMutex :: new ( ) } }
6969 }
7070}
7171
72- impl Drop for Inner {
73- fn drop ( & mut self ) { unsafe { let _ = libc:: close ( self . fd ) ; } }
74- }
75-
7672fn connect ( addr : & CString , ty : libc:: c_int ,
7773 timeout : Option < u64 > ) -> IoResult < Inner > {
7874 let mut storage = unsafe { mem:: zeroed ( ) } ;
@@ -82,13 +78,13 @@ fn connect(addr: &CString, ty: libc::c_int,
8278
8379 match timeout {
8480 None => {
85- match retry ( || unsafe { libc:: connect ( inner. fd , addrp, len) } ) {
81+ match retry ( || unsafe { libc:: connect ( inner. fd . fd ( ) , addrp, len) } ) {
8682 -1 => Err ( super :: last_error ( ) ) ,
8783 _ => Ok ( inner)
8884 }
8985 }
9086 Some ( timeout_ms) => {
91- try!( connect_timeout ( inner. fd , addrp, len, timeout_ms) ) ;
87+ try!( connect_timeout ( inner. fd . fd ( ) , addrp, len, timeout_ms) ) ;
9288 Ok ( inner)
9389 }
9490 }
@@ -100,7 +96,7 @@ fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
10096 let inner = Inner :: new ( try!( unix_socket ( ty) ) ) ;
10197 let addrp = & storage as * const _ as * const libc:: sockaddr ;
10298 match unsafe {
103- libc:: bind ( inner. fd , addrp, len)
99+ libc:: bind ( inner. fd . fd ( ) , addrp, len)
104100 } {
105101 -1 => Err ( super :: last_error ( ) ) ,
106102 _ => Ok ( inner)
@@ -133,7 +129,7 @@ impl UnixStream {
133129 }
134130 }
135131
136- fn fd ( & self ) -> fd_t { self . inner . fd }
132+ fn fd ( & self ) -> fd_t { self . inner . fd . fd ( ) }
137133
138134 #[ cfg( target_os = "linux" ) ]
139135 fn lock_nonblocking ( & self ) { }
@@ -200,6 +196,12 @@ impl UnixStream {
200196 }
201197}
202198
199+ impl AsFileDesc for UnixStream {
200+ fn as_fd ( & self ) -> & FileDesc {
201+ & self . inner . fd
202+ }
203+ }
204+
203205impl Clone for UnixStream {
204206 fn clone ( & self ) -> UnixStream {
205207 UnixStream :: new ( self . inner . clone ( ) )
@@ -222,7 +224,7 @@ impl UnixListener {
222224 } )
223225 }
224226
225- fn fd ( & self ) -> fd_t { self . inner . fd }
227+ fn fd ( & self ) -> fd_t { self . inner . fd . fd ( ) }
226228
227229 pub fn listen ( self ) -> IoResult < UnixAcceptor > {
228230 match unsafe { libc:: listen ( self . fd ( ) , 128 ) } {
0 commit comments