1
Fork 0

Rollup merge of #82589 - LinkTed:master, r=joshtriplett

unix: Non-mutable bufs in send_vectored_with_ancillary_to

This is the same PR as [#79753](https://github.com/rust-lang/rust/pull/79753). It was closed because of inactivity. Therefore, I create a new one. ````@lukaslihotzki````
This commit is contained in:
Yuki Okushi 2021-03-03 16:27:41 +09:00 committed by GitHub
commit b16b6820d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 45 deletions

View file

@ -1,6 +1,6 @@
use super::{sockaddr_un, SocketAddr}; use super::{sockaddr_un, SocketAddr};
use crate::convert::TryFrom; use crate::convert::TryFrom;
use crate::io::{self, IoSliceMut}; use crate::io::{self, IoSlice, IoSliceMut};
use crate::marker::PhantomData; use crate::marker::PhantomData;
use crate::mem::{size_of, zeroed}; use crate::mem::{size_of, zeroed};
use crate::os::unix::io::RawFd; use crate::os::unix::io::RawFd;
@ -68,7 +68,7 @@ pub(super) fn recv_vectored_with_ancillary_from(
pub(super) fn send_vectored_with_ancillary_to( pub(super) fn send_vectored_with_ancillary_to(
socket: &Socket, socket: &Socket,
path: Option<&Path>, path: Option<&Path>,
bufs: &mut [IoSliceMut<'_>], bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>, ancillary: &mut SocketAncillary<'_>,
) -> io::Result<usize> { ) -> io::Result<usize> {
unsafe { unsafe {
@ -78,7 +78,7 @@ pub(super) fn send_vectored_with_ancillary_to(
let mut msg: libc::msghdr = zeroed(); let mut msg: libc::msghdr = zeroed();
msg.msg_name = &mut msg_name as *mut _ as *mut _; msg.msg_name = &mut msg_name as *mut _ as *mut _;
msg.msg_namelen = msg_namelen; msg.msg_namelen = msg_namelen;
msg.msg_iov = bufs.as_mut_ptr().cast(); msg.msg_iov = bufs.as_ptr() as *mut _;
msg.msg_control = ancillary.buffer.as_mut_ptr().cast(); msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] { if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
@ -567,7 +567,7 @@ impl<'a> SocketAncillary<'a> {
/// #![feature(unix_socket_ancillary_data)] /// #![feature(unix_socket_ancillary_data)]
/// use std::os::unix::net::{UnixStream, SocketAncillary}; /// use std::os::unix::net::{UnixStream, SocketAncillary};
/// use std::os::unix::io::AsRawFd; /// use std::os::unix::io::AsRawFd;
/// use std::io::IoSliceMut; /// use std::io::IoSlice;
/// ///
/// fn main() -> std::io::Result<()> { /// fn main() -> std::io::Result<()> {
/// let sock = UnixStream::connect("/tmp/sock")?; /// let sock = UnixStream::connect("/tmp/sock")?;
@ -577,7 +577,7 @@ impl<'a> SocketAncillary<'a> {
/// ancillary.add_fds(&[sock.as_raw_fd()][..]); /// ancillary.add_fds(&[sock.as_raw_fd()][..]);
/// ///
/// let mut buf = [1; 8]; /// let mut buf = [1; 8];
/// let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..]; /// let mut bufs = &mut [IoSlice::new(&mut buf[..])][..];
/// sock.send_vectored_with_ancillary(bufs, &mut ancillary)?; /// sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
/// Ok(()) /// Ok(())
/// } /// }

View file

@ -19,7 +19,7 @@ use super::{sockaddr_un, SocketAddr};
target_os = "netbsd", target_os = "netbsd",
target_os = "openbsd", target_os = "openbsd",
))] ))]
use crate::io::IoSliceMut; use crate::io::{IoSlice, IoSliceMut};
use crate::net::Shutdown; use crate::net::Shutdown;
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::path::Path; use crate::path::Path;
@ -506,23 +506,24 @@ impl UnixDatagram {
/// ```no_run /// ```no_run
/// #![feature(unix_socket_ancillary_data)] /// #![feature(unix_socket_ancillary_data)]
/// use std::os::unix::net::{UnixDatagram, SocketAncillary}; /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
/// use std::io::IoSliceMut; /// use std::io::IoSlice;
/// ///
/// fn main() -> std::io::Result<()> { /// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?; /// let sock = UnixDatagram::unbound()?;
/// let mut buf1 = [1; 8]; /// let buf1 = [1; 8];
/// let mut buf2 = [2; 16]; /// let buf2 = [2; 16];
/// let mut buf3 = [3; 8]; /// let buf3 = [3; 8];
/// let mut bufs = &mut [ /// let bufs = &[
/// IoSliceMut::new(&mut buf1), /// IoSlice::new(&buf1),
/// IoSliceMut::new(&mut buf2), /// IoSlice::new(&buf2),
/// IoSliceMut::new(&mut buf3), /// IoSlice::new(&buf3),
/// ][..]; /// ][..];
/// let fds = [0, 1, 2]; /// let fds = [0, 1, 2];
/// let mut ancillary_buffer = [0; 128]; /// let mut ancillary_buffer = [0; 128];
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
/// ancillary.add_fds(&fds[..]); /// ancillary.add_fds(&fds[..]);
/// sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock").expect("send_vectored_with_ancillary_to function failed"); /// sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock")
/// .expect("send_vectored_with_ancillary_to function failed");
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
@ -538,7 +539,7 @@ impl UnixDatagram {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>( pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
&self, &self,
bufs: &mut [IoSliceMut<'_>], bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>, ancillary: &mut SocketAncillary<'_>,
path: P, path: P,
) -> io::Result<usize> { ) -> io::Result<usize> {
@ -554,23 +555,24 @@ impl UnixDatagram {
/// ```no_run /// ```no_run
/// #![feature(unix_socket_ancillary_data)] /// #![feature(unix_socket_ancillary_data)]
/// use std::os::unix::net::{UnixDatagram, SocketAncillary}; /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
/// use std::io::IoSliceMut; /// use std::io::IoSlice;
/// ///
/// fn main() -> std::io::Result<()> { /// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?; /// let sock = UnixDatagram::unbound()?;
/// let mut buf1 = [1; 8]; /// let buf1 = [1; 8];
/// let mut buf2 = [2; 16]; /// let buf2 = [2; 16];
/// let mut buf3 = [3; 8]; /// let buf3 = [3; 8];
/// let mut bufs = &mut [ /// let bufs = &[
/// IoSliceMut::new(&mut buf1), /// IoSlice::new(&buf1),
/// IoSliceMut::new(&mut buf2), /// IoSlice::new(&buf2),
/// IoSliceMut::new(&mut buf3), /// IoSlice::new(&buf3),
/// ][..]; /// ][..];
/// let fds = [0, 1, 2]; /// let fds = [0, 1, 2];
/// let mut ancillary_buffer = [0; 128]; /// let mut ancillary_buffer = [0; 128];
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
/// ancillary.add_fds(&fds[..]); /// ancillary.add_fds(&fds[..]);
/// sock.send_vectored_with_ancillary(bufs, &mut ancillary).expect("send_vectored_with_ancillary function failed"); /// sock.send_vectored_with_ancillary(bufs, &mut ancillary)
/// .expect("send_vectored_with_ancillary function failed");
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
@ -586,7 +588,7 @@ impl UnixDatagram {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn send_vectored_with_ancillary( pub fn send_vectored_with_ancillary(
&self, &self,
bufs: &mut [IoSliceMut<'_>], bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>, ancillary: &mut SocketAncillary<'_>,
) -> io::Result<usize> { ) -> io::Result<usize> {
send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary) send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)

View file

@ -530,23 +530,24 @@ impl UnixStream {
/// ```no_run /// ```no_run
/// #![feature(unix_socket_ancillary_data)] /// #![feature(unix_socket_ancillary_data)]
/// use std::os::unix::net::{UnixStream, SocketAncillary}; /// use std::os::unix::net::{UnixStream, SocketAncillary};
/// use std::io::IoSliceMut; /// use std::io::IoSlice;
/// ///
/// fn main() -> std::io::Result<()> { /// fn main() -> std::io::Result<()> {
/// let socket = UnixStream::connect("/tmp/sock")?; /// let socket = UnixStream::connect("/tmp/sock")?;
/// let mut buf1 = [1; 8]; /// let buf1 = [1; 8];
/// let mut buf2 = [2; 16]; /// let buf2 = [2; 16];
/// let mut buf3 = [3; 8]; /// let buf3 = [3; 8];
/// let mut bufs = &mut [ /// let bufs = &[
/// IoSliceMut::new(&mut buf1), /// IoSlice::new(&buf1),
/// IoSliceMut::new(&mut buf2), /// IoSlice::new(&buf2),
/// IoSliceMut::new(&mut buf3), /// IoSlice::new(&buf3),
/// ][..]; /// ][..];
/// let fds = [0, 1, 2]; /// let fds = [0, 1, 2];
/// let mut ancillary_buffer = [0; 128]; /// let mut ancillary_buffer = [0; 128];
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
/// ancillary.add_fds(&fds[..]); /// ancillary.add_fds(&fds[..]);
/// socket.send_vectored_with_ancillary(bufs, &mut ancillary).expect("send_vectored_with_ancillary function failed"); /// socket.send_vectored_with_ancillary(bufs, &mut ancillary)
/// .expect("send_vectored_with_ancillary function failed");
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
@ -562,7 +563,7 @@ impl UnixStream {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn send_vectored_with_ancillary( pub fn send_vectored_with_ancillary(
&self, &self,
bufs: &mut [IoSliceMut<'_>], bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>, ancillary: &mut SocketAncillary<'_>,
) -> io::Result<usize> { ) -> io::Result<usize> {
send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary) send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)

View file

@ -485,14 +485,14 @@ fn test_unix_datagram_peek_from() {
fn test_send_vectored_fds_unix_stream() { fn test_send_vectored_fds_unix_stream() {
let (s1, s2) = or_panic!(UnixStream::pair()); let (s1, s2) = or_panic!(UnixStream::pair());
let mut buf1 = [1; 8]; let buf1 = [1; 8];
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..]; let bufs_send = &[IoSlice::new(&buf1[..])][..];
let mut ancillary1_buffer = [0; 128]; let mut ancillary1_buffer = [0; 128];
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]); let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
assert!(ancillary1.add_fds(&[s1.as_raw_fd()][..])); assert!(ancillary1.add_fds(&[s1.as_raw_fd()][..]));
let usize = or_panic!(s1.send_vectored_with_ancillary(&mut bufs_send, &mut ancillary1)); let usize = or_panic!(s1.send_vectored_with_ancillary(&bufs_send, &mut ancillary1));
assert_eq!(usize, 8); assert_eq!(usize, 8);
let mut buf2 = [0; 8]; let mut buf2 = [0; 8];
@ -542,8 +542,8 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() {
or_panic!(bsock2.set_passcred(true)); or_panic!(bsock2.set_passcred(true));
let mut buf1 = [1; 8]; let buf1 = [1; 8];
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..]; let bufs_send = &[IoSlice::new(&buf1[..])][..];
let mut ancillary1_buffer = [0; 128]; let mut ancillary1_buffer = [0; 128];
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]); let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
@ -554,7 +554,7 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() {
assert!(ancillary1.add_creds(&[cred1.clone()][..])); assert!(ancillary1.add_creds(&[cred1.clone()][..]));
let usize = let usize =
or_panic!(bsock1.send_vectored_with_ancillary_to(&mut bufs_send, &mut ancillary1, &path2)); or_panic!(bsock1.send_vectored_with_ancillary_to(&bufs_send, &mut ancillary1, &path2));
assert_eq!(usize, 8); assert_eq!(usize, 8);
let mut buf2 = [0; 8]; let mut buf2 = [0; 8];
@ -603,15 +603,15 @@ fn test_send_vectored_with_ancillary_unix_datagram() {
let bsock1 = or_panic!(UnixDatagram::bind(&path1)); let bsock1 = or_panic!(UnixDatagram::bind(&path1));
let bsock2 = or_panic!(UnixDatagram::bind(&path2)); let bsock2 = or_panic!(UnixDatagram::bind(&path2));
let mut buf1 = [1; 8]; let buf1 = [1; 8];
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..]; let bufs_send = &[IoSlice::new(&buf1[..])][..];
let mut ancillary1_buffer = [0; 128]; let mut ancillary1_buffer = [0; 128];
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]); let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
assert!(ancillary1.add_fds(&[bsock1.as_raw_fd()][..])); assert!(ancillary1.add_fds(&[bsock1.as_raw_fd()][..]));
or_panic!(bsock1.connect(&path2)); or_panic!(bsock1.connect(&path2));
let usize = or_panic!(bsock1.send_vectored_with_ancillary(&mut bufs_send, &mut ancillary1)); let usize = or_panic!(bsock1.send_vectored_with_ancillary(&bufs_send, &mut ancillary1));
assert_eq!(usize, 8); assert_eq!(usize, 8);
let mut buf2 = [0; 8]; let mut buf2 = [0; 8];