first-ok

Crates.iofirst-ok
lib.rsfirst-ok
version0.1.2
sourcesrc
created_at2023-05-09 20:37:49.985292
updated_at2023-06-08 06:21:31.710639
descriptionThis 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.
homepage
repositoryhttps://github.com/auoie/first-ok
max_upload_size
id860810
size57,270
(auoie)

documentation

README

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(())
}
Commit count: 7

cargo fmt