#async #select #rust

select-macro

Waits on multiple concurrent branches

4 releases

0.2.0 Dec 28, 2022
0.1.2 Dec 27, 2022
0.1.1 Dec 27, 2022
0.1.0 Dec 27, 2022

#690 in Concurrency

Download history 19/week @ 2024-02-26 4/week @ 2024-03-04 53/week @ 2024-03-11 48/week @ 2024-04-01

102 downloads per month
Used in rucron

MIT license

24KB
529 lines

select_macro

Crates.io version Download

Waits on multiple concurrent branches, returning when the first branch completes, cancelling the remaining branches. The select! macro must be used inside of async functions, closures, and blocks.

The crate was shamelessly stolen from the tokio. More information can be found in the docs.

Motivation

Refer to the issue.

Usage

Add this to your Cargo.toml

[dependencies]
select-macro = "0.2.0"

Quick start:

use async_std::{channel as async_channel, stream::{self, StreamExt}, task};
use std::time::Duration;
use select_macro::{count, select, select_variant};

#[async_std::main]
async fn main(){
    let mut inter = stream::interval(Duration::from_secs(2));
    let (rx, tx) = async_channel::bounded(1);
    task::spawn(async move{
            task::sleep(Duration::from_secs(1)).await;
            rx.send(1).await.unwrap();
    });
    select!{
        _ = inter.next() => {
            panic!("unreachable!");
        }
        data = tx.recv() => {
            assert_eq!(data, Ok(1));
        }
    };
}

License

This project is licensed under the [MIT license].

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in select-macro by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~2.5MB
~50K SLoC