@@ -665,7 +665,7 @@ impl fmt::Debug for UnixDatagram {
665
665
}
666
666
667
667
impl UnixDatagram {
668
- /// Creates a Unix datagram socket from the given path.
668
+ /// Creates a Unix datagram socket bound to the given path.
669
669
pub fn bind < P : AsRef < Path > > ( path : P ) -> io:: Result < UnixDatagram > {
670
670
unsafe {
671
671
let inner = try!( Inner :: new ( libc:: SOCK_DGRAM ) ) ;
@@ -679,19 +679,19 @@ impl UnixDatagram {
679
679
}
680
680
}
681
681
682
- /// Creates a Unix Datagram socket which is not bound to any address
683
- /// you may use send_to to send message (but probably can't receive)
684
- pub fn new ( ) -> io:: Result < UnixDatagram > {
682
+ /// Creates a Unix Datagram socket which is not bound to any address.
683
+ pub fn unbound ( ) -> io:: Result < UnixDatagram > {
685
684
let inner = try!( Inner :: new ( libc:: SOCK_DGRAM ) ) ;
686
685
Ok ( UnixDatagram {
687
686
inner : inner,
688
687
} )
689
688
}
690
689
691
- /// Creates a Unix Datagram socket which is connected to specified addresss
692
- /// the socket is unnamed and similar to one created by `new()` except
693
- /// it will send message to the specified addresss *by default*
694
- pub fn connect < P : AsRef < Path > > ( & mut self , path : P )
690
+ /// Connect the socket to the specified address.
691
+ ///
692
+ /// The `send` method may be used to send data to the specified address.
693
+ /// `recv` and `recv_from` will only receive data from that address.
694
+ pub fn connect < P : AsRef < Path > > ( & self , path : P )
695
695
-> io:: Result < ( ) >
696
696
{
697
697
unsafe {
@@ -731,18 +731,18 @@ impl UnixDatagram {
731
731
732
732
/// Receives data from the socket.
733
733
///
734
- /// On success, returns the number of bytes read
734
+ /// On success, returns the number of bytes read.
735
735
pub fn recv ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
736
736
unsafe {
737
737
let count = try!( cvt_s ( libc:: recv ( self . inner . 0 ,
738
- buf. as_mut_ptr ( ) as * mut _ ,
739
- calc_len ( buf) ,
740
- 0 ) ) ) ;
738
+ buf. as_mut_ptr ( ) as * mut _ ,
739
+ calc_len ( buf) ,
740
+ 0 ) ) ) ;
741
741
Ok ( count as usize )
742
742
}
743
743
}
744
744
745
- /// Sends data on the socket to the given address.
745
+ /// Sends data on the socket to the specified address.
746
746
///
747
747
/// On success, returns the number of bytes written.
748
748
pub fn send_to < P : AsRef < Path > > ( & self , buf : & [ u8 ] , path : P ) -> io:: Result < usize > {
@@ -759,9 +759,10 @@ impl UnixDatagram {
759
759
}
760
760
}
761
761
762
- /// Sends data on the socket to the default address .
762
+ /// Sends data on the socket to the socket's peer .
763
763
///
764
- /// Default address is set when socket was created by `connect()` constructor
764
+ /// The peer address may be set by the `connect` method, and this method
765
+ /// will return an error if the socket has not already been connected.
765
766
///
766
767
/// On success, returns the number of bytes written.
767
768
pub fn send ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
@@ -1103,7 +1104,7 @@ mod test {
1103
1104
let path1 = dir. path ( ) . join ( "sock1" ) ;
1104
1105
1105
1106
let sock1 = or_panic ! ( UnixDatagram :: bind( & path1) ) ;
1106
- let sock2 = or_panic ! ( UnixDatagram :: new ( ) ) ;
1107
+ let sock2 = or_panic ! ( UnixDatagram :: unbound ( ) ) ;
1107
1108
1108
1109
let msg = b"hello world" ;
1109
1110
or_panic ! ( sock2. send_to( msg, & path1) ) ;
@@ -1122,7 +1123,7 @@ mod test {
1122
1123
1123
1124
let bsock1 = or_panic ! ( UnixDatagram :: bind( & path1) ) ;
1124
1125
let bsock2 = or_panic ! ( UnixDatagram :: bind( & path2) ) ;
1125
- let mut sock = or_panic ! ( UnixDatagram :: new ( ) ) ;
1126
+ let sock = or_panic ! ( UnixDatagram :: unbound ( ) ) ;
1126
1127
or_panic ! ( sock. connect( & path1) ) ;
1127
1128
1128
1129
// Check send()
@@ -1153,7 +1154,7 @@ mod test {
1153
1154
let path1 = dir. path ( ) . join ( "sock1" ) ;
1154
1155
1155
1156
let sock1 = or_panic ! ( UnixDatagram :: bind( & path1) ) ;
1156
- let mut sock2 = or_panic ! ( UnixDatagram :: new ( ) ) ;
1157
+ let sock2 = or_panic ! ( UnixDatagram :: unbound ( ) ) ;
1157
1158
or_panic ! ( sock2. connect( & path1) ) ;
1158
1159
1159
1160
let msg = b"hello world" ;
0 commit comments