I decided to use the hyper create to build a server that reads the body data of a POST method. How do I synchronously return a value calculated in an asynchronous Future in stable Rust? explains a part of what I am looking for, but I don't want to use tokio::run
or future::lazy
because according to my understanding hyper uses Tokio and futures and the hyper body returns a stream. What I am trying to accomplish is to find other ways of handling a stream and get more knowledge on hyper Request
methods.
In the first approach, I concat2
then call wait
. wait
blocks the current thread so my code hangs.
if Method::POST == req.method() {
let body = req.into_body().concat2().wait();
// convert to json and do something with the data.
Ok(Response::new(Body::from("OK: data submitted")))
}
In the second approach, I tried using poll
and and_then
but I always get a NotReady
. The result type is futures::poll::Async<hyper::Chunk>
.
if Method::POST == req.method() {
let body = req.into_body().concat2().poll().and_then(|e| {
// do something
Ok(e)
});
match body {
Ok(e) => println!("{:#?}", e),
Err(r) => println!("{:#?}", r),
};
Ok(Response::new(Body::from("")))
}
- How can I unblock the current thread and return the results?
- How can I poll and then return the results, ?
If possible, please explain good practice on how to handle futures::poll::Async
and wait()
. At the moment, async
/await
is unstable in Rust so I can't use it.