From 7b9ca3e9d9b5837dd79adc863f1bac9b19c25983 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 4 Jan 2022 16:31:59 +0000 Subject: [PATCH] Flip iter operations to keep associated address/header/packets together (backport #22245) (#22256) * Flip iter operations to keep associated address/header/packets together (#22245) Flip iter operations to keep associated address/header/packets together Before this change, if cast_socket_addr() returned a None for any address/header pair, the subsequent zip() would misalign the address/header pair and packet. So, this change zips all three together, then does filter_map() so keep things aligned. Additionally, compute total_size inline to avoid running through packets a second time. (cherry picked from commit 20b61e28b61bf0e327f749a079276bc89d2dcc45) # Conflicts: # streamer/src/recvmmsg.rs * removes backport merge conflicts Co-authored-by: steviez Co-authored-by: behzad nouri --- streamer/src/recvmmsg.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/streamer/src/recvmmsg.rs b/streamer/src/recvmmsg.rs index 0a9e25d908..28337d8485 100644 --- a/streamer/src/recvmmsg.rs +++ b/streamer/src/recvmmsg.rs @@ -99,21 +99,20 @@ pub fn recv_mmsg(sock: &UdpSocket, packets: &mut [Packet]) -> io::Result<(usize, return Err(io::Error::last_os_error()); } let mut npkts = 0; - addrs - .iter() - .zip(&hdrs) + let mut total_size = 0; + + izip!(&addrs, &hdrs, packets.iter_mut()) .take(nrecv as usize) - .filter_map(|(addr, hdr)| { - let addr = cast_socket_addr(addr, &hdr)?.to_std(); - Some((addr, hdr)) + .filter_map(|(addr, hdr, pkt)| { + let addr = cast_socket_addr(addr, hdr)?.to_std(); + Some((addr, hdr, pkt)) }) - .zip(packets.iter_mut()) - .for_each(|((addr, hdr), pkt)| { + .for_each(|(addr, hdr, pkt)| { pkt.meta.size = hdr.msg_len as usize; pkt.meta.set_addr(&addr); npkts += 1; + total_size += pkt.meta.size; }); - let total_size = packets.iter().take(npkts).map(|pkt| pkt.meta.size).sum(); Ok((total_size, npkts)) }