gpt4 book ai didi

rust - 在 Rust 中的两个异步函数之间进行选择的变量 - 不兼容的 ARM 类型

转载 作者:行者123 更新时间:2023-12-04 17:10:34 26 4
gpt4 key购买 nike

我有一个变量可以控制哪个函数是我的 Web 应用程序的默认行为,但是这两个函数都是 async 并且它不会让我这样做,因为它们是不同的闭包。像这样的东西会重现同样的错误: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=db0269ef4852a94438e6d4925ca83d3b

现在我的问题是:

  • 如何解决这个问题才能使匹配正常进行?
  • 是否有更好的方法可以根据用户 CLI 标志在 actix 上以编程方式设置默认服务器行为?对于上下文,这或多或少是我所拥有的:

main.rs

// Matches comes from the command line
let a = match matches.occurrences_of("default"){
1 => handlers::forward,
_ => handlers::resource_not_found,
};

... setting up db, auth etc

HttpServer::new(move || {
App::new()
... lots of routes
.default_service(web::route().to(a))

处理程序.rs


pub async fn resource_not_found(
) -> Result<HttpResponse, Error> {
Ok(HttpResponse::NotFound().body("Resource does not exist"))
}
pub async fn forward(
) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().body("Resource does exist"))
}

最佳答案

How do I solve this so that the match works

您可以创建两个都返回装箱 future 的闭包,并将它们强制转换为函数指针:

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

let a: fn() -> Pin<Box<dyn Future<Output = Result<HttpResponse, Error>>>> =
match matches.occurrences_of("default") {
1 => || Box::pin(handlers::forward()),
_ => || Box::pin(handlers::resource_not_found()),
};

如果函数真的那么简单,那么你可以避免装箱并返回 std::future::Ready:

use std::pin::Pin;
use std::future::{Future, Ready, ready};

let a: fn() -> Ready<Result<HttpResponse, Error>> =
match matches.occurrences_of("default") {
1 => || handlers::forward,
_ => || handlers::resource_not_found,
};

// handlers.rs

pub fn resource_not_found(
) -> Ready<Result<HttpResponse, Error>> {
ready(Ok(HttpResponse::NotFound().body("Resource does not exist")))
}

pub fn forward(
) -> Ready<Result<HttpResponse, Error>> {
ready(Ok(HttpResponse::Ok().body("Resource does exist")))
}

Is there a better way to set default server behavior programmatically on actix based on user CLI flags

您可以在新处理程序内部而不是外部进行匹配:

let x = matches.occurrences_of("default");
let a = move || async move {
match x {
1 => handlers::forward().await,
_ => handlers::resource_not_found().await,
}
};

现在,您只需匹配一个整数,而不是每次调用都进行额外的分配和动态分派(dispatch)。

关于rust - 在 Rust 中的两个异步函数之间进行选择的变量 - 不兼容的 ARM 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69534172/

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