@@ -20,7 +20,7 @@ use prelude::*;
20
20
use sys:: { mod, timer, retry, c, set_nonblocking, wouldblock} ;
21
21
use sys:: fs:: { fd_t, FileDesc } ;
22
22
use sys_common:: net:: * ;
23
- use sys_common:: { eof, mkerr_libc} ;
23
+ use sys_common:: { AsFileDesc , eof, mkerr_libc} ;
24
24
25
25
fn unix_socket ( ty : libc:: c_int ) -> IoResult < fd_t > {
26
26
match unsafe { libc:: socket ( libc:: AF_UNIX , ty, 0 ) } {
@@ -56,7 +56,7 @@ fn addr_to_sockaddr_un(addr: &CString,
56
56
}
57
57
58
58
struct Inner {
59
- fd : fd_t ,
59
+ fd : FileDesc ,
60
60
61
61
// Unused on Linux, where this lock is not necessary.
62
62
#[ allow( dead_code) ]
@@ -65,14 +65,10 @@ struct Inner {
65
65
66
66
impl Inner {
67
67
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 ( ) } }
69
69
}
70
70
}
71
71
72
- impl Drop for Inner {
73
- fn drop ( & mut self ) { unsafe { let _ = libc:: close ( self . fd ) ; } }
74
- }
75
-
76
72
fn connect ( addr : & CString , ty : libc:: c_int ,
77
73
timeout : Option < u64 > ) -> IoResult < Inner > {
78
74
let mut storage = unsafe { mem:: zeroed ( ) } ;
@@ -82,13 +78,13 @@ fn connect(addr: &CString, ty: libc::c_int,
82
78
83
79
match timeout {
84
80
None => {
85
- match retry ( || unsafe { libc:: connect ( inner. fd , addrp, len) } ) {
81
+ match retry ( || unsafe { libc:: connect ( inner. fd . fd ( ) , addrp, len) } ) {
86
82
-1 => Err ( super :: last_error ( ) ) ,
87
83
_ => Ok ( inner)
88
84
}
89
85
}
90
86
Some ( timeout_ms) => {
91
- try!( connect_timeout ( inner. fd , addrp, len, timeout_ms) ) ;
87
+ try!( connect_timeout ( inner. fd . fd ( ) , addrp, len, timeout_ms) ) ;
92
88
Ok ( inner)
93
89
}
94
90
}
@@ -100,7 +96,7 @@ fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
100
96
let inner = Inner :: new ( try!( unix_socket ( ty) ) ) ;
101
97
let addrp = & storage as * const _ as * const libc:: sockaddr ;
102
98
match unsafe {
103
- libc:: bind ( inner. fd , addrp, len)
99
+ libc:: bind ( inner. fd . fd ( ) , addrp, len)
104
100
} {
105
101
-1 => Err ( super :: last_error ( ) ) ,
106
102
_ => Ok ( inner)
@@ -133,7 +129,7 @@ impl UnixStream {
133
129
}
134
130
}
135
131
136
- fn fd ( & self ) -> fd_t { self . inner . fd }
132
+ fn fd ( & self ) -> fd_t { self . inner . fd . fd ( ) }
137
133
138
134
#[ cfg( target_os = "linux" ) ]
139
135
fn lock_nonblocking ( & self ) { }
@@ -200,6 +196,12 @@ impl UnixStream {
200
196
}
201
197
}
202
198
199
+ impl AsFileDesc for UnixStream {
200
+ fn as_fd ( & self ) -> & FileDesc {
201
+ & self . inner . fd
202
+ }
203
+ }
204
+
203
205
impl Clone for UnixStream {
204
206
fn clone ( & self ) -> UnixStream {
205
207
UnixStream :: new ( self . inner . clone ( ) )
@@ -222,7 +224,7 @@ impl UnixListener {
222
224
} )
223
225
}
224
226
225
- fn fd ( & self ) -> fd_t { self . inner . fd }
227
+ fn fd ( & self ) -> fd_t { self . inner . fd . fd ( ) }
226
228
227
229
pub fn listen ( self ) -> IoResult < UnixAcceptor > {
228
230
match unsafe { libc:: listen ( self . fd ( ) , 128 ) } {
0 commit comments