我正在尝试编译类似以下代码的内容。似乎我需要帮助它理解我希望所有匹配臂都被视为futures::future::IntoFuture
,因为这是外部and_then
对回调/关闭/委托的期望。
目前,所有分支都使用最简单的枚举变体 stub NothingUseful()
,但我的目标最终是根据返回的 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 | | },
| |_________________^