Skip to content

Commit 066d2db

Browse files
BastiDoodtaiki-e
authored andcommitted
Refactor: prefer early return in future::select (#2587)
Although a relatively small change, it makes the code a little bit more readable than the originally nested `match` expressions.
1 parent ae7dbfb commit 066d2db

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

futures-util/src/future/select.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ where
100100

101101
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
102102
let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
103-
match a.poll_unpin(cx) {
104-
Poll::Ready(x) => Poll::Ready(Either::Left((x, b))),
105-
Poll::Pending => match b.poll_unpin(cx) {
106-
Poll::Ready(x) => Poll::Ready(Either::Right((x, a))),
107-
Poll::Pending => {
108-
self.inner = Some((a, b));
109-
Poll::Pending
110-
}
111-
},
103+
104+
if let Poll::Ready(val) = a.poll_unpin(cx) {
105+
return Poll::Ready(Either::Left((val, b)));
106+
}
107+
108+
if let Poll::Ready(val) = b.poll_unpin(cx) {
109+
return Poll::Ready(Either::Right((val, a)));
112110
}
111+
112+
self.inner = Some((a, b));
113+
Poll::Pending
113114
}
114115
}
115116

0 commit comments

Comments
 (0)