在 Rust 中将函数转换为 trait 的机制是什么?

dav*_*v_i 10 rust actix-web

来自 actix-web 的一个例子如下:

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

async fn index() -> impl Responder {
    "Hello world!"
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(
            web::scope("/app").route("/index.html", web::get().to(index)),
        )
    })
    .bind("127.0.0.1:8088")?
    .run()
    .await
}
Run Code Online (Sandbox Code Playgroud)

我的问题是关于该语句to(index)在 Rust 中是如何工作的。

查看源代码,to我们看到:

pub fn to<F, T, R, U>(mut self, handler: F) -> Self
where
    F: Factory<T, R, U>,
// --- snip
Run Code Online (Sandbox Code Playgroud)

其中Factory定义为

pub trait Factory<T, R, O>: Clone + 'static
where
    R: Future<Output = O>,
    O: Responder,
{
    fn call(&self, param: T) -> R;
}
Run Code Online (Sandbox Code Playgroud)

函数async fn index() -> impl Responder转换为 的机制是什么Factory<T, R, O>

rod*_*igo 11

在您的代码段之后有一个 trait 的实现

impl<F, R, O> Factory<(), R, O> for F
where
    F: Fn() -> R + Clone + 'static,
    R: Future<Output = O>,
    O: Responder,
{
    fn call(&self, _: ()) -> R {
        (self)()
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以理解为:如果一个类型F实现了,Fn() -> Future<Output = impl Responder> + ...那么它也实现了Factory<(), _, _>

anasync fn是一个函数的语法糖,它返回Future某种类型的 a(并且可以在.await内部使用),所以async fn index() -> impl Responder实现了Fn() -> impl Future<Output = impl Responder>它也实现了Factory<(), _, _>.

  • 还有行话更正:发布的代码中没有*转换*,而是有一个*约束*:“F”需要实现指定的特征,但其身份不会改变。 (2认同)