1

Warp has a paradigm like this,

let hi = warp::path("hello")
    .and(warp::path::param())
    .and(warp::header("user-agent"))
    .map(|param: String, agent: String| {
        format!("Hello {}, whose agent is {}", param, agent)
    });

Those filters provide .map() which allows you call a closure with the extracted value (output from prior filter).

How do I operate within this paradigm if in the above example I want to do something like this,

.map(async |param: String, agent: String| {
    foo(&param).await?;
    format!("Hello {}, whose agent is {}", &param, agent)
});

When I use async functions in a closure in the filter's .map, I get this error,

error[E0708]: async non-move closures with parameters are not currently supported

Is there anyway to make warp compatible with a library that is already async?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

1 Answers1

4

You can use and_then:

.and_then(|param: String, agent: String| async move {
    foo(&param).await?;
    Ok(format!("Hello {}, whose agent is {}", &param, agent))
});

Note that this is not actually an async closure, it is a regular closure that returns a future created by an async block. See What is the difference between |_| async move {} and async move |_| {} for more information.

Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54