Skip to content

Commit

Permalink
Ensure unreachable branch is eliminated (#2708)
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh authored Feb 18, 2023
1 parent 2610a0e commit b41ede5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions futures-util/src/future/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,24 @@ where
type Output = Either<(A::Output, B), (B::Output, A)>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
/// When compiled with `-C opt-level=z`, this function will help the compiler eliminate the `None` branch, where
/// `Option::unwrap` does not.
#[inline(always)]
fn unwrap_option<T>(value: Option<T>) -> T {
match value {
None => unreachable!(),
Some(value) => value,
}
}

let (a, b) = self.inner.as_mut().expect("cannot poll Select twice");

if let Poll::Ready(val) = a.poll_unpin(cx) {
return Poll::Ready(Either::Left((val, self.inner.take().unwrap().1)));
return Poll::Ready(Either::Left((val, unwrap_option(self.inner.take()).1)));
}

if let Poll::Ready(val) = b.poll_unpin(cx) {
return Poll::Ready(Either::Right((val, self.inner.take().unwrap().0)));
return Poll::Ready(Either::Right((val, unwrap_option(self.inner.take()).0)));
}

Poll::Pending
Expand Down

0 comments on commit b41ede5

Please sign in to comment.