gpt4 book ai didi

asynchronous - Rust 预期类型找到的结构

转载 作者:行者123 更新时间:2023-12-03 11:44:38 24 4
gpt4 key购买 nike

我有以下代码:

use actix_service::Service;
use actix_web::{web, App, HttpServer, Responder};

use actix_router::{Path, Url};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::error::ResponseError;
use actix_web::{http, http::StatusCode, Error, HttpRequest, HttpResponse};

async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let app = App::new()
.wrap_fn(|req, srv| {
let passed: bool;

// change to false to simulate a failed check
let check = true;

if *&req.path().contains("/item/") {
passed = check;
} else {
passed = true;
}

let fresh_result = match passed {
true => {
let fut = srv.call(req);
Box::pin(async {
let result = fut.await?;
Ok(result)
})
}
false => Box::pin(async {
let result = req.into_response(
HttpResponse::Found()
.header(http::header::LOCATION, "/login")
.finish()
.into_body(),
);
Ok(result)
}),
};
async {
let last_outcome = fresh_result.await?;
Ok(last_outcome)
}
})
.route("/", web::get().to(greet));
return app;
})
.bind("127.0.0.1:8000")?
.run()
.await
}
但是,我收到以下错误:
110 |                         let fresh_result = match passed {
| ________________________________________-
111 | | true => {
112 | | let fut = srv.call(req);
113 | | Box::pin(
| _|_____________________________-
114 | | | async {
115 | | | let result = fut.await?;
116 | | | Ok(result)
117 | | | }
118 | | | )
| |_|_____________________________- this is found to be of type `std::pin::Pin<std::boxed::Box<impl core::future::future::Future>>`
... |
121 | / | Box::pin(
122 | | | async {
123 | | | let result = req.into_response(
124 | | | HttpResponse::Found()
... | |
129 | | | }
130 | | | )
| |_|_____________________________^ expected generator, found a different generator
131 | | }
132 | | };
| |_____________________- `match` arms have incompatible types
|
::: /Users/maxwellflitton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/future/mod.rs:55:43
|
55 | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
| ------------------------------- the found opaque type
|
= note: expected type `std::pin::Pin<std::boxed::Box<impl core::future::future::Future>>` (generator)
found struct `std::pin::Pin<std::boxed::Box<impl core::future::future::Future>>` (generator)

我完全坚持这一点。我不知道如何确保它是一种类型。如果没有 match 语句并且只有一个异步 block ,那么一切运行良好。这适用于 Actix-web 服务器中的中间件。如果凭据不起作用,我正在尝试重定向查看器。

最佳答案

您使用 Box::pin创造盒装 future ,
但是 Rust 仍然认为你想要 impl Future<...>版本(意味着 future 被装箱到堆中,但不使用动态调度)。
这就是错误中的类型为 Pin<Box<impl Future<...>>> 的原因。 ,并且因为任意两个 impl Future<...> s 是不同的类型,你得到 expected/found错误。
你需要告诉 Rust 你对动态调度感兴趣(因为你有两个不同的 future 可以存储在 Box 中,而哪个真正存储在那里只能在运行时知道),例如通过显式指定 return像这样输入:

use std::pin::Pin;
use std::future::Future;

let fresh_result: Pin<Box<dyn Future<Output=_>>> = match passed {
// ...
}
现在你会得到一个关于 cannot infer type for type parameter E 的新错误。这可以通过指定 Output 来解决 future 也一样,
所以新行应该是:
let fresh_result: Pin<Box<dyn Future<Output=Result<ServiceResponse, Error>>>> = match passed {
// ...
}
这将成功编译!

关于asynchronous - Rust 预期类型找到的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64414743/

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