Skip to content

Commit

Permalink
macro select no branch, else fallback only
Browse files Browse the repository at this point in the history
  • Loading branch information
GilShoshan94 committed Feb 12, 2024
1 parent 84e41d4 commit 7b76d7d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tokio/src/macros/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ macro_rules! select {

// ===== Entry point =====

($(biased;)? else => $else:expr $(,)? ) => {{
$else
}};

(biased; $p:pat = $($t:tt)* ) => {
$crate::select!(@{ start=0; () } $p = $($t)*)
};
Expand All @@ -617,6 +621,7 @@ macro_rules! select {
// fair and avoids always polling the first future.
$crate::select!(@{ start={ $crate::macros::support::thread_rng_n(BRANCHES) }; () } $p = $($t)*)
};

() => {
compile_error!("select! requires at least one branch.")
};
Expand Down
19 changes: 19 additions & 0 deletions tokio/tests/macros_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ async fn sync_one_lit_expr_comma() {
assert_eq!(foo, 1);
}

#[maybe_tokio_test]
async fn no_branch_else_only() {
let foo = tokio::select! {
else => 1,
};

assert_eq!(foo, 1);
}

#[maybe_tokio_test]
async fn no_branch_else_only_biased() {
let foo = tokio::select! {
biased;
else => 1,
};

assert_eq!(foo, 1);
}

#[maybe_tokio_test]
async fn nested_one() {
let foo = tokio::select! {
Expand Down

0 comments on commit 7b76d7d

Please sign in to comment.