Skip to content

Commit 532f09f

Browse files
committed
add send_vectored for unix socket
1 parent 3dbade6 commit 532f09f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/libstd/sys/unix/ext/net.rs

+50
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,32 @@ impl UnixDatagram {
14081408
self.0.write(buf)
14091409
}
14101410

1411+
/// Like `send`, except that it sends from a slice of buffers.
1412+
///
1413+
/// The peer address may be set by the `connect` method, and this method
1414+
/// will return an error if the socket has not already been connected.
1415+
///
1416+
/// On success, returns the number of bytes written.
1417+
///
1418+
/// # Examples
1419+
///
1420+
/// ```no_run
1421+
/// use std::os::unix::net::UnixDatagram;
1422+
/// use std::io::IoSlice;
1423+
///
1424+
/// fn main() -> std::io::Result<()> {
1425+
/// let sock = UnixDatagram::unbound()?;
1426+
/// sock.connect("/some/sock").expect("Couldn't connect");
1427+
/// let bufs = [IoSlice::new(b" "), IoSlice::new(b"hello world")];
1428+
/// sock.send_vectored(&bufs).expect("send_vectored function failed");
1429+
/// Ok(())
1430+
/// }
1431+
/// ```
1432+
#[unstable(feature = "unix_socket_send_vectored", issue = "68612")]
1433+
pub fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1434+
self.0.write_vectored(bufs)
1435+
}
1436+
14111437
/// Sets the read timeout for the socket.
14121438
///
14131439
/// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
@@ -1941,6 +1967,30 @@ mod test {
19411967
or_panic!(bsock2.recv_from(&mut buf));
19421968
}
19431969

1970+
#[test]
1971+
fn test_unix_datagram_send_vectored() {
1972+
let dir = tmpdir();
1973+
let path1 = dir.path().join("sock1");
1974+
1975+
let sock1 = or_panic!(UnixDatagram::bind(&path1));
1976+
let sock2 = or_panic!(UnixDatagram::unbound());
1977+
or_panic!(sock2.connect(&path1));
1978+
1979+
let a = [];
1980+
let b = [10];
1981+
let c = [11, 12];
1982+
or_panic!(sock2.send_vectored(&[IoSlice::new(&a), IoSlice::new(&b), IoSlice::new(&c)]));
1983+
let mut buf = [0; 4];
1984+
let len = or_panic!(sock1.recv(&mut buf));
1985+
// some implementations don't support writev, so we may only write the first buffer
1986+
if len == 1 {
1987+
assert_eq!(buf, [10, 0, 0, 0]);
1988+
} else {
1989+
assert_eq!(len, 3);
1990+
assert_eq!(buf, [10, 11, 12, 0]);
1991+
}
1992+
}
1993+
19441994
#[test]
19451995
fn test_unix_datagram_recv() {
19461996
let dir = tmpdir();

0 commit comments

Comments
 (0)