Skip to content
This repository was archived by the owner on May 17, 2018. It is now read-only.

Commit e1d8575

Browse files
committed
Made UnixDatagram::connect() a method instead of constructor
1 parent e2c36c6 commit e1d8575

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,15 @@ impl UnixDatagram {
691691
/// Creates a Unix Datagram socket which is connected to specified addresss
692692
/// the socket is unnamed and similar to one created by `new()` except
693693
/// it will send message to the specified addresss *by default*
694-
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
694+
pub fn connect<P: AsRef<Path>>(&mut self, path: P)
695+
-> io::Result<()>
696+
{
695697
unsafe {
696-
let inner = try!(Inner::new(libc::SOCK_DGRAM));
697698
let (addr, len) = try!(sockaddr_un(path));
698699

699-
try!(cvt(libc::connect(inner.0, &addr as *const _ as *const _, len)));
700+
try!(cvt(libc::connect(self.inner.0, &addr as *const _ as *const _, len)));
700701

701-
Ok(UnixDatagram {
702-
inner: inner,
703-
})
702+
Ok(())
704703
}
705704
}
706705

@@ -1106,23 +1105,32 @@ mod test {
11061105
fn test_connect_unix_datagram() {
11071106
let dir = or_panic!(TempDir::new("unix_socket"));
11081107
let path1 = dir.path().join("sock1");
1108+
let path2 = dir.path().join("sock2");
11091109

1110-
let sock1 = or_panic!(UnixDatagram::bind(&path1));
1111-
let sock2 = or_panic!(UnixDatagram::connect(&path1));
1110+
let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1111+
let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1112+
let mut sock = or_panic!(UnixDatagram::new());
1113+
or_panic!(sock.connect(&path1));
11121114

11131115
// Check send()
11141116
let msg = b"hello there";
1115-
or_panic!(sock2.send(msg));
1117+
or_panic!(sock.send(msg));
11161118
let mut buf = [0; 11];
1117-
let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1119+
let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
11181120
assert_eq!(usize, 11);
11191121
assert_eq!(addr.address(), AddressKind::Unnamed);
11201122
assert_eq!(msg, &buf[..]);
11211123

1124+
11221125
// Send to should still work too
11231126
let msg = b"hello world";
1124-
or_panic!(sock2.send_to(msg, &path1));
1125-
or_panic!(sock1.recv_from(&mut buf));
1127+
or_panic!(sock.send_to(msg, &path2));
1128+
or_panic!(bsock2.recv_from(&mut buf));
11261129
assert_eq!(msg, &buf[..]);
1130+
1131+
// Changing default socket works too
1132+
or_panic!(sock.connect(&path2));
1133+
or_panic!(sock.send(msg));
1134+
or_panic!(bsock2.recv_from(&mut buf));
11271135
}
11281136
}

0 commit comments

Comments
 (0)