Вернуть структуру из потока тела - PullRequest
0 голосов
/ 20 октября 2019

Пожалуйста, помогите, я посмотрел на гипер док и пример

В большинстве примеров при работе с гипер Request<Body> будет либо map(|chunk|{ //do something }), либо and_then(|chunk|{ //do something }) затем верните поток, который работает, но теперь я хочу попробовать и вернуть кусок или фактический элемент в потоке. см. ниже

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 может решить проблему, которую я считаю, но я использую Стабильность ржавчины. Я хочу poll(), но я получу NotReady. Пожалуйста, сообщите.

1 Ответ

1 голос
/ 20 октября 2019

Вы пытаетесь использовать гиперсинхронно, по своей природе гипер не предназначен для этого и требует уровня абстракции, вы должны посмотреть на это 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

...