Skip to content

Commit

Permalink
Refactor: prefer early return in future::select (#2587)
Browse files Browse the repository at this point in the history
Although a relatively small change, it makes the code a little bit
more readable than the originally nested `match` expressions.
  • Loading branch information
BastiDood authored and taiki-e committed Aug 14, 2022
1 parent dac38c0 commit fc55afb
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions futures-util/src/future/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ where

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
match a.poll_unpin(cx) {
Poll::Ready(x) => Poll::Ready(Either::Left((x, b))),
Poll::Pending => match b.poll_unpin(cx) {
Poll::Ready(x) => Poll::Ready(Either::Right((x, a))),
Poll::Pending => {
self.inner = Some((a, b));
Poll::Pending
}
},

if let Poll::Ready(val) = a.poll_unpin(cx) {
return Poll::Ready(Either::Left((val, b)));
}

if let Poll::Ready(val) = b.poll_unpin(cx) {
return Poll::Ready(Either::Right((val, a)));
}

self.inner = Some((a, b));
Poll::Pending
}
}

Expand Down

0 comments on commit fc55afb

Please sign in to comment.