Skip to content

docs(examples): Update web_api example to use async await #1860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

docs(examples): Update web_api example to use async await #1860

wants to merge 1 commit into from

Conversation

messense
Copy link
Contributor

@messense messense commented Jul 12, 2019

Ref #1849

@messense
Copy link
Contributor Author

This does not compile yet, error:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
   --> examples/web_api.rs:118:40
    |
118 |                 response_examples(req, &client)
    |                                        ^^^^^^^
    |
note: first, the lifetime cannot outlive the lifetime '_ as defined on the body at 117:46...
   --> examples/web_api.rs:117:46
    |
117 |             Ok::<_, GenericError>(service_fn(move |req| {
    |                                              ^^^^^^^^^^
note: ...so that closure can access `client`
   --> examples/web_api.rs:118:40
    |
118 |                 response_examples(req, &client)
    |                                        ^^^^^^^
note: but, the lifetime must be valid for the expression at 117:35...
   --> examples/web_api.rs:117:35
    |
117 |             Ok::<_, GenericError>(service_fn(move |req| {
    |                                   ^^^^^^^^^^
note: ...so type `fn([closure@examples/web_api.rs:117:46: 119:14 client:hyper::Client<hyper::client::HttpConnector>]) -> hyper::service::service::ServiceFn<[closure@examples/web_api.rs:117:46: 119:14 client:hyper::Client<hyper::client::HttpConnector>], hyper::Body> {hyper::service::service_fn::<[closure@examples/web_api.rs:117:46: 119:14 client:hyper::Client<hyper::client::HttpConnector>], hyper::Body, impl std::future::Future>}` of expression is valid during the expression
   --> examples/web_api.rs:117:35
    |
117 |             Ok::<_, GenericError>(service_fn(move |req| {
    |                                   ^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `hyper`.

Not sure how to fix it.

@seanmonstar
Copy link
Member

I think it's complaining that the future returned in service_fn has a borrow on &client, but the service_fn closure owns client. So, if the service_fn were dropped after return a future but before that future started, the borrow would be invalid.

It's holding a borrow because any references passed to an async fn are held for the duration of the returned future.

You could either pass in clones of the client, or change the async fns to be normal functions, and use an internal async block. Like:

let fut = client.request(req);
async move {
    let res = fut.await?;
}

@weihanglo weihanglo mentioned this pull request Jul 16, 2019
10 tasks
@weihanglo
Copy link
Contributor

@messense
The lifetime issue was fixed on #1872. Just clone the client again as @seanmonstar said.

@messense
Copy link
Contributor Author

Closing in favor of #1872

@messense messense closed this Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants