gpt4 book ai didi

rust - 从正文流中返回一个结构

转载 作者:行者123 更新时间:2023-11-29 08:28:14 29 4
gpt4 key购买 nike

请协助,我已经查看了 hyper docexample

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

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 上发布

关于rust - 从正文流中返回一个结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58471848/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com