Skip to content

goldwind-ting/select_macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages