Skip to content

Commit

Permalink
muxers/mplex/benches: Use combinators to reduce nesting (#2688)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Jun 3, 2022
1 parent bd9834a commit fcc987e
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions muxers/mplex/benches/split_send_size.rs
Expand Up @@ -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"),
Expand Down

0 comments on commit fcc987e

Please sign in to comment.