10
10
11
11
use std:: fmt;
12
12
use std:: io:: { self , Read , Write } ;
13
- use std:: net:: { self , SocketAddr , Ipv4Addr , Ipv6Addr , Shutdown } ;
13
+ use std:: net:: { self , Ipv4Addr , Ipv6Addr , Shutdown } ;
14
14
use std:: time:: Duration ;
15
15
16
16
#[ cfg( unix) ]
@@ -19,7 +19,7 @@ use libc as c;
19
19
use winapi as c;
20
20
21
21
use sys;
22
- use { Socket , Protocol , Domain , Type } ;
22
+ use { Socket , Protocol , Domain , Type , SockAddr } ;
23
23
24
24
impl Socket {
25
25
/// Creates a new socket ready to be configured.
@@ -58,7 +58,7 @@ impl Socket {
58
58
///
59
59
/// An error will be returned if `listen` or `connect` has already been
60
60
/// called on this builder.
61
- pub fn connect ( & self , addr : & SocketAddr ) -> io:: Result < ( ) > {
61
+ pub fn connect ( & self , addr : & SockAddr ) -> io:: Result < ( ) > {
62
62
self . inner . connect ( addr)
63
63
}
64
64
@@ -81,15 +81,15 @@ impl Socket {
81
81
///
82
82
/// If the connection request times out, it may still be processing in the
83
83
/// background - a second call to `connect` or `connect_timeout` may fail.
84
- pub fn connect_timeout ( & self , addr : & SocketAddr , timeout : Duration ) -> io:: Result < ( ) > {
84
+ pub fn connect_timeout ( & self , addr : & SockAddr , timeout : Duration ) -> io:: Result < ( ) > {
85
85
self . inner . connect_timeout ( addr, timeout)
86
86
}
87
87
88
88
/// Binds this socket to the specified address.
89
89
///
90
90
/// This function directly corresponds to the bind(2) function on Windows
91
91
/// and Unix.
92
- pub fn bind ( & self , addr : & SocketAddr ) -> io:: Result < ( ) > {
92
+ pub fn bind ( & self , addr : & SockAddr ) -> io:: Result < ( ) > {
93
93
self . inner . bind ( addr)
94
94
}
95
95
@@ -110,19 +110,19 @@ impl Socket {
110
110
/// This function will block the calling thread until a new connection is
111
111
/// established. When established, the corresponding `Socket` and the
112
112
/// remote peer's address will be returned.
113
- pub fn accept ( & self ) -> io:: Result < ( Socket , SocketAddr ) > {
113
+ pub fn accept ( & self ) -> io:: Result < ( Socket , SockAddr ) > {
114
114
self . inner . accept ( ) . map ( |( socket, addr) | {
115
115
( Socket { inner : socket } , addr)
116
116
} )
117
117
}
118
118
119
119
/// Returns the socket address of the local half of this TCP connection.
120
- pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
120
+ pub fn local_addr ( & self ) -> io:: Result < SockAddr > {
121
121
self . inner . local_addr ( )
122
122
}
123
123
124
124
/// Returns the socket address of the remote peer of this TCP connection.
125
- pub fn peer_addr ( & self ) -> io:: Result < SocketAddr > {
125
+ pub fn peer_addr ( & self ) -> io:: Result < SockAddr > {
126
126
self . inner . peer_addr ( )
127
127
}
128
128
@@ -184,7 +184,7 @@ impl Socket {
184
184
185
185
/// Receives data from the socket. On success, returns the number of bytes
186
186
/// read and the address from whence the data came.
187
- pub fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
187
+ pub fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SockAddr ) > {
188
188
self . inner . recv_from ( buf)
189
189
}
190
190
@@ -195,7 +195,7 @@ impl Socket {
195
195
///
196
196
/// On success, returns the number of bytes peeked and the address from
197
197
/// whence the data came.
198
- pub fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
198
+ pub fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SockAddr ) > {
199
199
self . inner . peek_from ( buf)
200
200
}
201
201
@@ -214,7 +214,7 @@ impl Socket {
214
214
///
215
215
/// This is typically used on UDP or datagram-oriented sockets. On success
216
216
/// returns the number of bytes that were sent.
217
- pub fn send_to ( & self , buf : & [ u8 ] , addr : & SocketAddr ) -> io:: Result < usize > {
217
+ pub fn send_to ( & self , buf : & [ u8 ] , addr : & SockAddr ) -> io:: Result < usize > {
218
218
self . inner . send_to ( buf, addr)
219
219
}
220
220
@@ -693,12 +693,14 @@ impl From<Protocol> for i32 {
693
693
694
694
#[ cfg( test) ]
695
695
mod test {
696
+ use std:: net:: SocketAddr ;
697
+
696
698
use super :: * ;
697
699
698
700
#[ test]
699
701
fn connect_timeout_unrouteable ( ) {
700
702
// this IP is unroutable, so connections should always time out
701
- let addr: SocketAddr = "10.255.255.1:80" . parse ( ) . unwrap ( ) ;
703
+ let addr = "10.255.255.1:80" . parse :: < SocketAddr > ( ) . unwrap ( ) . into ( ) ;
702
704
703
705
let socket = Socket :: new ( Domain :: ipv4 ( ) , Type :: stream ( ) , None ) . unwrap ( ) ;
704
706
match socket. connect_timeout ( & addr, Duration :: from_millis ( 250 ) ) {
@@ -711,7 +713,7 @@ mod test {
711
713
#[ test]
712
714
fn connect_timeout_valid ( ) {
713
715
let socket = Socket :: new ( Domain :: ipv4 ( ) , Type :: stream ( ) , None ) . unwrap ( ) ;
714
- socket. bind ( & "127.0.0.1:0" . parse ( ) . unwrap ( ) ) . unwrap ( ) ;
716
+ socket. bind ( & "127.0.0.1:0" . parse :: < SocketAddr > ( ) . unwrap ( ) . into ( ) ) . unwrap ( ) ;
715
717
socket. listen ( 128 ) . unwrap ( ) ;
716
718
717
719
let addr = socket. local_addr ( ) . unwrap ( ) ;
0 commit comments