How do I integrate warp with async libraries?

1.6k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.