docs(examples): fix web_api shared client lifetime issue

This commit is contained in:
Weihang Lo
2019-07-16 09:09:36 +08:00
committed by Sean McArthur
parent e8c19fea4c
commit 031036c2e7

View File

@@ -73,7 +73,7 @@ async fn api_get_response() -> Result<Response<Body>, GenericError> {
Ok(res) Ok(res)
} }
async fn response_examples(req: Request<Body>, client: &Client<HttpConnector>) async fn response_examples(req: Request<Body>, client: Client<HttpConnector>)
-> Result<Response<Body>, GenericError> -> Result<Response<Body>, GenericError>
{ {
match (req.method(), req.uri().path()) { match (req.method(), req.uri().path()) {
@@ -82,7 +82,7 @@ async fn response_examples(req: Request<Body>, client: &Client<HttpConnector>)
Ok(Response::new(body)) Ok(Response::new(body))
}, },
(&Method::GET, "/test.html") => { (&Method::GET, "/test.html") => {
client_request_response(client).await client_request_response(&client).await
}, },
(&Method::POST, "/json_api") => { (&Method::POST, "/json_api") => {
api_post_response(req).await api_post_response(req).await
@@ -115,7 +115,8 @@ async fn main() -> Result<(), GenericError> {
let client = client.clone(); let client = client.clone();
async { async {
Ok::<_, GenericError>(service_fn(move |req| { Ok::<_, GenericError>(service_fn(move |req| {
response_examples(req, &client) // Clone again to ensure that client outlives this closure.
response_examples(req, client.to_owned())
})) }))
} }
}); });