#safe #async #select #future

safina-select

Safe async select function, for awaiting multiple futures

3 releases

0.1.2 Dec 24, 2020
0.1.1 Dec 12, 2020
0.1.0 Dec 12, 2020

#430 in Asynchronous

22 downloads per month
Used in safina

Apache-2.0

59KB
1.5K SLoC

safina-select

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

This is a Rust library for awaiting multiple futures and getting the value of the first one that completes.

It is part of safina, a safe async runtime.

Features

  • forbid(unsafe_code)
  • Depends only on std
  • Good test coverage (96%)
  • Works with safina-executor or any async executor

Limitations

  • Can await 2-5 futures. Nest them if you need more.

Examples

use safina_async_test::async_test;
use safina_select::{select_ab, OptionAB};
let conn = match select_ab(make_new(addr.clone()), get_from_pool(addr.clone())).await {
    OptionAB::A(result) => result?,
    OptionAB::B(result) => result?,
};
// When both futures return the same type, you can use `take`:
let conn = select_ab(make_new(addr.clone()), get_from_pool(addr.clone())).await.take()?;
use safina_async_test::async_test;
use safina_select::{select_ab, OptionAB};
safina_timer::start_timer_thread();
let data = match select_ab(read_data(), safina_timer::sleep_until(deadline)).await {
    OptionAB::A(result) => Ok(result?),
    OptionAB::B(()) => Err("timeout"),
};

Documentation

https://docs.rs/safina-select

TO DO - Alternatives

  • tokio::select
    • very popular
    • Fast
    • internally incredibly complicated
    • full of unsafe
  • futures::select
    • very popular
    • proc macro, very complicated
    • contains a little unsafe code

Changelog

  • v0.1.2 - Satisfy pedantic clippy
  • v0.1.1 - Add badges to readme. Rename safina package to safina-executor.
  • v0.1.0 - First published version

TO DO

  • DONE - Implement sleep_ab
  • DONE - Add tests
  • DONE - Add docs
  • DONE - Add sleep_abc, sleep_abcd, and sleep_abcde
  • DONE - Publish on crates.io

Release Process

  1. Edit Cargo.toml and bump version number.
  2. Run ./release.sh

License: Apache-2.0

No runtime deps