Hi,
I run another issue using actix-web and and actix_web_lab.
I am writing an actix-web-lab
async
function as a middleware. Depending on some condition, this middleware either forwards the existing response to the client as is, or creating a specific response and forwards this response to the client.
I am following this official example actix-web-lab/actix-web-lab/examples/from_fn.rs, particularly the functions async fn mutate_body_type
and async fn mutate_body_type_with_extractors
.
Both of "my" code blocks work fine individually, but when combine into if
-else
block, the compiler gives a scary looking error, while I understand the nature of the error, I don't know how to solve it. Please help.
To start off, this is the async
function as a middleware, which has only a single code block, where I create ONLY a specific response and sends it to the client:
async fn finalise_request(
req: ServiceRequest,
next: Next<impl MessageBody + 'static>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
let res = next.call(req).await?;
Ok(res.map_body(move |_head, _body|
HttpResponse::Ok().body("behai<br/>Got stuck đ").into_body()
))
}
Where:
-
res
isServiceResponse<impl MessageBody>
-
_head
is&mut RespondHead
-
_body
isimpl MessageBody
And it is registered with the application as follows:
pub async fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
...
let server = HttpServer::new(move || {
App::new()
...
/*>>>*/ .wrap(from_fn(finalise_request))
.wrap(auth_middleware::CheckLogin)
...
.wrap(cors_config(&config))
.service(
...
})
.listen_openssl(listener, ssl_builder())?
.run();
Ok(server)
}
This code compiles and works as expected, that is, if I access the login page, I would get this instead:
behai
Got stuck đ
And this version of finalise_request
also works, it just forward the response as is to the clients, without it, the application does the same thing:
async fn finalise_request(
req: ServiceRequest,
next: Next<impl MessageBody + 'static>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
let res = next.call(req).await?;
Ok(res.map_into_left_body::<()>())
}
But this version of finalise_request
, which I should need, gives scary compiler errors, which I am stuck:
async fn finalise_request(
req: ServiceRequest,
next: Next<impl MessageBody + 'static>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
let res = next.call(req).await?;
let xx: bool = false;
if !xx {
Ok(res.map_into_left_body::<()>())
}
else {
Ok(res.map_body(move |_head, _body|
HttpResponse::Ok().body("behai<br/>got stuck :(").into_body()
))
}
}
The error is in the else
block, while I understand what error E0308
means, I don't know how to solve it:
error[E0308]: mismatched types
--> src/lib.rs:223:12
|
223 | Ok(res.map_body(move |_head, _body|
| _________--_^
| | |
| | arguments to this enum variant are incorrect
224 | | HttpResponse::Ok().body("behai<br/>got stuck :(").into_body()
225 | | ))
| |_________^ expected `ServiceResponse<EitherBody<..., ...>>`, found `ServiceResponse`
|
= note: expected struct `ServiceResponse<EitherBody<impl MessageBody + 'static, ()>>`
found struct `ServiceResponse<BoxBody>`
help: the type constructed contains `ServiceResponse` due to the type of the argument passed
--> src/lib.rs:223:9
|
223 | Ok(res.map_body(move |_head, _body|
| __________^__-
| | _________|
| ||
224 | || HttpResponse::Ok().body("behai<br/>got stuck :(").into_body()
225 | || ))
| ||_________-^
| |__________|
| this argument influences the type of `Ok`
note: tuple variant defined here
--> C:\Users\behai\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\result.rs:506:5
|
506 | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
| ^^
Thank you and best regards,
...behai.