Skip to content

Commit 5b64af3

Browse files
EFanZhtaiki-e
authored andcommitted
Ensure unreachable branch is eliminated (#2708)
1 parent bc85d23 commit 5b64af3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Diff for: futures-util/src/future/select.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,24 @@ where
9999
type Output = Either<(A::Output, B), (B::Output, A)>;
100100

101101
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
102+
/// When compiled with `-C opt-level=z`, this function will help the compiler eliminate the `None` branch, where
103+
/// `Option::unwrap` does not.
104+
#[inline(always)]
105+
fn unwrap_option<T>(value: Option<T>) -> T {
106+
match value {
107+
None => unreachable!(),
108+
Some(value) => value,
109+
}
110+
}
111+
102112
let (a, b) = self.inner.as_mut().expect("cannot poll Select twice");
103113

104114
if let Poll::Ready(val) = a.poll_unpin(cx) {
105-
return Poll::Ready(Either::Left((val, self.inner.take().unwrap().1)));
115+
return Poll::Ready(Either::Left((val, unwrap_option(self.inner.take()).1)));
106116
}
107117

108118
if let Poll::Ready(val) = b.poll_unpin(cx) {
109-
return Poll::Ready(Either::Right((val, self.inner.take().unwrap().0)));
119+
return Poll::Ready(Either::Right((val, unwrap_option(self.inner.take()).0)));
110120
}
111121

112122
Poll::Pending

0 commit comments

Comments
 (0)