Skip to content

Ensure unreachable branch is eliminated #2708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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