16
16
//! datagram protocol.
17
17
18
18
use clone:: Clone ;
19
- use io:: net:: ip:: { SocketAddr , IpAddr } ;
19
+ use io:: net:: ip:: { SocketAddr , IpAddr , ToSocketAddr } ;
20
20
use io:: { Reader , Writer , IoResult , IoError } ;
21
21
use kinds:: Send ;
22
22
use boxed:: Box ;
@@ -65,18 +65,13 @@ pub struct UdpSocket {
65
65
66
66
impl UdpSocket {
67
67
/// Creates a UDP socket from the given socket address.
68
- pub fn bind ( addr : SocketAddr ) -> IoResult < UdpSocket > {
69
- let SocketAddr { ip, port } = addr;
70
- LocalIo :: maybe_raise ( |io| {
71
- let addr = rtio:: SocketAddr { ip : super :: to_rtio ( ip) , port : port } ;
72
- io. udp_bind ( addr) . map ( |s| UdpSocket { obj : s } )
73
- } ) . map_err ( IoError :: from_rtio_error)
68
+ pub fn bind < A : ToSocketAddr > ( addr : A ) -> IoResult < UdpSocket > {
69
+ super :: with_addresses_io ( addr, |io, addr| io. udp_bind ( addr) . map ( |s| UdpSocket { obj : s } ) )
74
70
}
75
71
76
72
/// Receives data from the socket. On success, returns the number of bytes
77
73
/// read and the address from whence the data came.
78
- pub fn recv_from ( & mut self , buf : & mut [ u8 ] )
79
- -> IoResult < ( uint , SocketAddr ) > {
74
+ pub fn recv_from ( & mut self , buf : & mut [ u8 ] ) -> IoResult < ( uint , SocketAddr ) > {
80
75
match self . obj . recv_from ( buf) {
81
76
Ok ( ( amt, rtio:: SocketAddr { ip, port } ) ) => {
82
77
Ok ( ( amt, SocketAddr { ip : super :: from_rtio ( ip) , port : port } ) )
@@ -87,11 +82,11 @@ impl UdpSocket {
87
82
88
83
/// Sends data on the socket to the given address. Returns nothing on
89
84
/// success.
90
- pub fn send_to ( & mut self , buf : & [ u8 ] , dst : SocketAddr ) -> IoResult < ( ) > {
91
- self . obj . send_to ( buf, rtio:: SocketAddr {
92
- ip : super :: to_rtio ( dst . ip ) ,
93
- port : dst . port ,
94
- } ) . map_err ( IoError :: from_rtio_error)
85
+ pub fn send_to < A : ToSocketAddr > ( & mut self , buf : & [ u8 ] , addr : A ) -> IoResult < ( ) > {
86
+ super :: with_addresses ( addr , |addr| self . obj . send_to ( buf, rtio:: SocketAddr {
87
+ ip : super :: to_rtio ( addr . ip ) ,
88
+ port : addr . port ,
89
+ } ) . map_err ( IoError :: from_rtio_error) )
95
90
}
96
91
97
92
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`
0 commit comments