Hyper Client: Perform Http request in one tokio task, read out http_info in another task

143 Views Asked by At

Consider the following minimal example:

use http::{Method, Request, Uri};
use hyper::{client::connect::HttpInfo, Body, Client};

#[tokio::main]
async fn main() -> Result<(), String> {
    let req = Request::builder()
        .method(Method::POST)
        .uri(Uri::from_static("http://foo.bar"))
        .body(Body::from("a lot of data..."))
        .unwrap();

    let hyper_client = Client::new();

    tokio::spawn(async move {
        let resp = hyper_client.request(req).await;
    });

    tokio::spawn(async move {
        let http_info = req.extensions().get::<HttpInfo>().unwrap();
    });

    Ok(())
}

It does not compile due to this error:

use of moved value: `req`
value used here after move
tmp.rs(14, 18): value moved here
tmp.rs(15, 41): variable moved due to use in generator
tmp.rs(19, 25): use occurs due to use in generator
tmp.rs(6, 9): move occurs because `req` has type `http::Request<Body>`, which does not implement the `Copy` trait

I'd expect that it would be possible to read out the http_info from a different tookio task than the HTTP request was performed from.

However, memory management does not allow me to do so.

Does anyone know how to solve this?

0

There are 0 best solutions below