#first #ok #items #bounded #async #set

first-ok

This provides the function first_ok::get_first_ok_bounded which takes an async function and a set of items. It applies the async function to all of the items and returns the first Ok result

3 releases

0.1.2 Jun 8, 2023
0.1.1 May 9, 2023
0.1.0 May 9, 2023

#18 in #ok

Download history 22/week @ 2024-02-19 8/week @ 2024-02-26 19/week @ 2024-03-11 61/week @ 2024-04-01

80 downloads per month

MIT license

15KB
85 lines

first-ok

This provides the function first_ok::get_first_ok_bounded which takes an async function and a set of items. It applies the async function to all of the items and returns the first Ok result.

// examples/port.rs
use anyhow::{anyhow, Context};

#[tokio::main]
fn main() -> anyhow::Result<()> {
    let client = reqwest::Client::builder().build()?;
    let items = (1024..=65535u16).map(move |elem| (elem, client.clone()));
    let url = first_ok::get_first_ok_bounded(items, 0, move |(port, client)| async move {
        let url = format!("http://127.0.0.1:{}", port);
        let response = client.get(&url).send().await?;
        if response.status().as_u16() != 200 {
            return Err(anyhow!(format!("{}", response.status())));
        }
        Ok(url)
    })
    .await
    .context("nothing reported")??;
    println!("{}", url);
    Ok(())
}

Dependencies

~3–5MB
~86K SLoC