I’m trying to use the select!
macro’s pre-condition to only include a branch if an Option
is the Some
variant.
I observe behavour which surprises me: the unwrap is eagerly evaluated causing panic!
unless contained within async { }
.
That is, this works as expected:
<code>async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = async { foo(maybe.unwrap()).await }, if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
</code>
<code>async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = async { foo(maybe.unwrap()).await }, if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
</code>
async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = async { foo(maybe.unwrap()).await }, if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
…whereas this causes the thread to panic!
:
<code>async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = foo(maybe.unwrap()), if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
</code>
<code>async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = foo(maybe.unwrap()), if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
</code>
async fn foo(x: u32) {}
let maybe: Option<u32> = None;
select! {
_ = foo(maybe.unwrap()), if maybe.is_some() => (),
_ = async { some_other_branch().await } => (),
}
What’s the reason for the difference in behaviour here?