[Pre-RFC] write_vectored()/read_vectored() for datagram based sockets

[libs]

[Pre-RFC] write_vectored()/read_vectored() for datagram based sockets

Summary

This RFC would add the following methods to UnixDatagram and UdpSocket or a trait shared by both.

impl UnixDatagramSocket {
         fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize>
         fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> Result<usize>
}

Motivation

Rust already provides a similar interface in the write_vectored() and read_vectored() function in std::io::Write/std::io::Read. These traits however are explicitly meant for byte-sinks, not for datagram-based channels. It would be great if the same functionality could also be provided for those; like UnixDatagram and UdpSocket.

While for byte sinks a vectored write is a nice performance optimiziation it could be emulated completely by looping over all iovecs and writing one by one. For datagrams however this is not possible because the data would be sent in multiple datagrams which is a semantic difference. This forces users to preallocate a buffer, copy all the data into it and send the payload in one send() call.

Unresolved questions

Currently these functions are part of std::io::Write and std::io::Read. Implementing those two traits for for datagrams does not work because many of their other functions don't make sense.

Adding a new trait SimpleWrite (or hopefully some better name) that only contains write() and write_vectored() may make sense.

(And the equivalent for the read functions)

Background

I would like to perform heap-allocation-free writes through a UnixDatagram with a message compromised from multiple parts. This works in C with AF_UNIX SOCK_DGRAM and writev (or sendmsg).

2 Likes