从体流返回结构

问题描述 投票:0回答:1

[请协助,我看过hyper docexample

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

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现在我想轮询以下流并返回结果。await可能会解决我认为的问题,但我使用的是rust Stable。我希望收到poll(),但我会收到NotReady。请告知。

rust hyper
1个回答
0
投票

您正在尝试同步使用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上发布了>

© www.soinside.com 2019 - 2024. All rights reserved.