[处理超级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
。请告知。
您正在尝试同步使用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上发布了>