muxers/mplex/benches: Use combinators to reduce nesting (#2688)

This commit is contained in:
Thomas Eizinger 2022-06-03 17:38:02 +02:00 committed by GitHub
parent bd9834aab4
commit fcc987e0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,26 +104,24 @@ fn run(transport: &mut BenchTransport, payload: &Vec<u8>, listen_addr: &Multiadd
}
transport::ListenerEvent::Upgrade { upgrade, .. } => {
let (_peer, conn) = upgrade.await.unwrap();
match poll_fn(|cx| conn.poll_event(cx)).await {
Ok(muxing::StreamMuxerEvent::InboundSubstream(mut s)) => {
let mut buf = vec![0u8; payload_len];
let mut off = 0;
loop {
// Read in typical chunk sizes of up to 8KiB.
let end = off + std::cmp::min(buf.len() - off, 8 * 1024);
let n = poll_fn(|cx| {
conn.read_substream(cx, &mut s, &mut buf[off..end])
})
.await
.unwrap();
off += n;
if off == buf.len() {
return;
}
}
let mut s = poll_fn(|cx| conn.poll_event(cx))
.await
.expect("unexpected error")
.into_inbound_substream()
.expect("Unexpected muxer event");
let mut buf = vec![0u8; payload_len];
let mut off = 0;
loop {
// Read in typical chunk sizes of up to 8KiB.
let end = off + std::cmp::min(buf.len() - off, 8 * 1024);
let n = poll_fn(|cx| conn.read_substream(cx, &mut s, &mut buf[off..end]))
.await
.unwrap();
off += n;
if off == buf.len() {
return;
}
Ok(_) => panic!("Unexpected muxer event"),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
_ => panic!("Unexpected listener event"),