Crates.io | first-ok |
lib.rs | first-ok |
version | 0.1.2 |
source | src |
created_at | 2023-05-09 20:37:49.985292 |
updated_at | 2023-06-08 06:21:31.710639 |
description | 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. |
homepage | |
repository | https://github.com/auoie/first-ok |
max_upload_size | |
id | 860810 |
size | 57,270 |
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(())
}