Workaround of "Implementation of `FnOnce` is not general enough"

I encountered a compiler bug which is similar to this post, and I would like to give the closure explicit type annotation to workaround it as @Yandros wrote:

Either you tweak your types / etc. (e.g., by massaging the callback's signature so that it becomes properly higher-order?) into now being able to meet this more demanding higher-order constraint

In the linked post, the code snippet that trigger the compiler bug is:

use futures::{stream, StreamExt, TryStreamExt};
use reqwest::Client;

async fn run() -> Vec<String> {
    let client = &Client::new();
    
    stream::iter(["asdf", "qwer"])
        .map(|s| async move {
            let resp = client.get(format!("https://httpbin.org/get?{s}")).send().await?;
            resp.text().await
        })
        .buffer_unordered(10)
        .try_collect().await.unwrap()
}

And I would like to give an explicit type annotation (with HKTB) to this closure, but don't know how to write it.

|s| async move {
    let resp = client.get(format!("https://httpbin.org/get?{s}")).send().await?;
    resp.text().await
}

The snippet compiles. Do you have an example that's problematic?

Though be forewarned, there's often not a satisfying workaround due to the combination of the compiler's poor higher-ranked closure inference, unnameable async types, and not enough knobs to overrule the inference.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.