Return 来自正文流的结构

Return a struct from a body stream

请帮忙,我已经看过 hyper doc and example

处理 hyper Request<Body> 的大多数示例将 map(|chunk|{ //do something })and_then(|chunk|{ //do something }) 然后 return Stream,这有效但现在我想尝试 return 流中的块或实际项目。见下文

pub fn to_struct(body: Body) -> Option<Person> {
    let person = body.and_then(|chunk|{
        let body = std::str::from_utf8(&chunk).unwrap();
        let results: Person = serde_json::from_str(&body).unwrap();
        Ok(results)
    });
    // match person or if let 
    // return Some or None

    // Don't wan't to Body::wrap_stream(person) then return Response<Body>
}

streams do nothing unless polled 现在我想轮询以下流和 return 结果。我相信 await 可能会解决问题,但我使用的是 Rust Stable。我想 poll() 但我会收到一个 NotReady。请指教。

您正在尝试同步使用 hyper,hyper 本质上不是为此而设计的,需要一层抽象,您应该看看这个 issus

There is no documentation specifically to that effect. Since
you'd need to block the thread at a certain point in execution
until it is ready, you'd need the work of the event loop to occur
in another thread, and send the results over a channel that you
block on.

Alternatively, you can look at reqwest, which even with the hyper
upgrade (not quite released to crate.io, but super soon), it
still offers a synchronous API besides the new async API.

注意:reqwest 现已在 crate.io

发布