gpt4 book ai didi

rust - 在 IntoFuture 中包装枚举变体?

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

<分区>

我正在尝试编译类似于以下代码的内容。看来我需要帮助它理解我希望所有的匹配臂都被视为 futures::future::IntoFuture,因为这就是外部 and_then 的含义期待回调/闭包/委托(delegate)。

现在所有的 ARM 都被最简单的枚举变量 NothingUseful() stub ,但我的目标最终是根据返回的 HTTP 状态代码和/或正文内容采取各种行动适用。

extern crate futures;
extern crate hyper;
extern crate tokio_core;

use futures::{future, Future, Stream};
use hyper::{Client, Error as HyperError, Response, StatusCode, Uri};
use tokio_core::reactor::Core;

struct RecurseUrl {
uri: Uri,
remaining_attempts: u8,
}

enum FetchResult {
SimpleData(u16),
RecurseUrls(Vec<RecurseUrl>),
NothingUseful(),
}

fn handle_redirect(res: &Response) -> future::FutureResult<FetchResult, HyperError> {
future::ok(FetchResult::NothingUseful())
}

fn main() {
let url = "http://someurl.com"
.parse()
.expect("Unable to parse URL");

let mut core = Core::new().expect("Unable to instantiate Tokio Core");
let client = Client::new(&core.handle());

let work = client.get(url).and_then(|res| {

match res.status() {
StatusCode::TemporaryRedirect => handle_redirect(&res),
StatusCode::PermanentRedirect => handle_redirect(&res),
StatusCode::Ok => {
res.body().concat2().and_then(move |body| {
Ok(FetchResult::NothingUseful())
})
},
_ => {
Ok(FetchResult::NothingUseful())
}
}
});

core.run(work).expect("Problem running work");
}
    error[E0308]: match arms have incompatible types
--> main.rs:34:13
|
34 | / match res.status() {
35 | | StatusCode::TemporaryRedirect => handle_redirect(&res),
36 | | StatusCode::PermanentRedirect => handle_redirect(&res),
37 | | StatusCode::Ok => {
... |
44 | | }
45 | | }
| |_____________^ expected struct `futures::FutureResult`, found struct `futures::AndThen`
|
= note: expected type `futures::FutureResult<FetchResult, hyper::Error>`
found type `futures::AndThen<futures::stream::Concat2<hyper::Body>, std::result::Result<FetchResult, hyper::Error>, [closure@main.rs:38:51: 40:22]>`
note: match arm with an incompatible type
--> main.rs:37:35
|
37 | StatusCode::Ok => {
| ___________________________________^
38 | | res.body().concat2().and_then(move |body| {
39 | | Ok(FetchResult::NothingUseful())
40 | | })
41 | | },
| |_________________^

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