私は次のコードのようなものをコンパイルしようとしています。私がすべての武器をfutures::future::IntoFuture
としたいと思っていることを理解するのを助ける必要があるようです。それは外側のand_then
がコールバック/クロージャー/デリゲートから期待しているものですから。IntoFutureに列挙型をラップしますか?
今のところ、すべてのアームは最も簡単な列挙型の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>, [[email protected]: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 | | },
| |_________________^
IntoFutureは形質であり、安定したRustの形質に何かを作るだけの方法はないのですか? – Shepmaster
@Shepmaster私はそれが特性だと理解しています。私はいくつかの明示的なタイプのヒントやキャストや何かを追加できることを期待しています。 future :: ok()を使ってそれぞれの腕を実際の未来に戻すのは簡単ですが、結果のimplsのいくつかがAndThenでFutureResultがいくつかあり、それらを変数に代入しようとすると問題に遭遇します彼らはすべて共通しているはずの特性でタイプされている、それはSizedのためにマークされていないことについて不平を言う。たぶん私は全く別のアプローチをとるべきでしょうか? – steamer25