译:揭秘神奇的 Rust Axum 风格的函数实现(Rusts Axum style magic function params example)

原文链接:github.com/alexpusch/r...

原文作者:Alex Puschinsky

在学习 Rust 时,我了解到它是一种严格的、静态类型的语言。尤其是它没有函数重载或可选参数这种特性。但是,当我看到 Axum 时,我惊讶地发现了这种代码:

rust 复制代码
let app = Router::new()
  .route("/users", get(get_users))
  .route("/products", get(get_product));

async fn get_users(Query(params): Query<Params>) -> impl IntoResponse {
    let users = /* ... */

    Json(users)
}

async fn get_product(State(db): State<Db>, Json(payload): Json<Payload>) -> String {
  let product = /* ... */

  product.to_string()
}

get 方法可以接收一个各种类型的函数作为参数!这是什么黑魔法?

为了搞清楚,我写了一个简单的例子:

rust 复制代码
fn print_id(id: Id) {
    println!("id is {}", id.0);
}

// Param(param) is just pattern matching
fn print_all(Param(param): Param, Id(id): Id) {
    println!("param is {param}, id is {id}");
}

pub fn main() {
    let context = Context::new("magic".into(), 33);

    trigger(context.clone(), print_id);
    trigger(context.clone(), print_all);
}

例子中,有一个 trigger,它接收一个 Context 和一个函数作为参数。作为参数的函数可以接收 1 个或 2 个参数,参数类型为 IdParam 类型。神奇吗?

让我们来看看到底是怎么实现的,首先是 Context

rust 复制代码
struct Context {
    param: String,
    id: u32,
}

Context 可以类比为 Axum 里的 Request,它是 print_idprint_all 函数里面数据的来源,这个例子中它仅包括两个字段。

接下来是第一个秘诀, FromContext trait:

rust 复制代码
trait FromContext {
    fn from_context(context: &Context) -> Self;
}

它使得我们可以创建各种 "Extractors" 来提取里面的内容,比如 Param 会提取里面的 param 字段:

rust 复制代码
struct Param(String);

impl FromContext for Param {
    fn from_context(context: &Context) -> Self {
        Param(context.param.clone())
    }
}

第二个秘诀是 Handler trait:

rust 复制代码
trait Handler<T> {
    fn call(self, context: Context);
}

我们会为 Fn<T> 类型来实现这个 trait:

rust 复制代码
impl<F, T> Handler<T> for F
where
    F: Fn(T),
    T: FromContext,
{
    fn call(self, context: Context) {
        self(T::from_context(&context));
    }
}

这样的话,我们在函数调用和它的参数之间就有了一个 "middleware"。这里我们将调用 FromContext::from_context 方法,将上下文转换为预期的函数参数,即 ParamId

译者注:执行 impl<F, T> Handler<T> for F 后,相当于为 Fn<T> 类型实现了 Handler 这个 trait,即 print_id 实现了 Handler,可以调用 call 方法,而 call 方法中的 self 就是 print_id

我们继续添加代码,支持两个函数参数的情况。有趣的是,这个实现与参数的顺序无关--它同时支持 fn foo(p: Param, id: id)fn foo(id: id, p: Param)

rust 复制代码
impl<T1, T2, F> Handler<(T1, T2)> for F
where
    F: Fn(T1, T2),
    T1: FromContext,
    T2: FromContext,
{
    fn call(self, context: Context) {
        (self)(T1::from_context(&context), T2::from_context(&context));
    }
}

译者注:通过宏,可以一次性实现不定参数的情况,例:

rust 复制代码
macro_rules! all_the_tuples {
    ($name:ident) => {
        $name!([]);
        $name!([T1]);
        $name!([T1, T2]);
        $name!([T1, T2, T3]);
        // Can add more
    };
}

macro_rules! impl_handler {
    (
        [$($ty:ident),*]
    ) => {
        impl<F, $($ty,)*> Handler<($($ty,)*)> for F
        where
            F: Fn($($ty,)*),
            $( $ty: FromContext,)*
        {
            fn call(self, context: Context) {
                self($( $ty::from_context(&context), )*);
            }
        }
    };
}

all_the_tuples!(impl_handler);

最后实现 trigger 函数就搞定了:

rust 复制代码
fn trigger<T, H>(context: Context, handler: H)
where
    H: Handler<T>,
{
    handler.call(context);
}

现在,让我解释下下面代码:

rust 复制代码
let context = Context::new("magic".into(), 33);

trigger(context.clone(), print_id);

print_idFn(Id) 类型,它实现了 Handler<Id>,当调用 handler.call 方法时,相当于执行如下代码:

rust 复制代码
print_id(Id::from_context(&context));

魔术揭秘!

附完整代码:

rust 复制代码
#[derive(Clone, Debug)]
struct Context {
    param: String,
    id: u32,
}

impl Context {
    fn new(param: String, id: u32) -> Context {
        Context { param, id }
    }
}

trait FromContext {
    fn from_context(context: &Context) -> Self;
}
trait Handler<T> {
    fn call(self, context: Context);
}

struct Param(String);

impl FromContext for Param {
    fn from_context(context: &Context) -> Self {
        Param(context.param.clone())
    }
}

struct Id(u32);

impl FromContext for Id {
    fn from_context(context: &Context) -> Self {
        Id(context.id.clone())
    }
}

macro_rules! all_the_tuples {
    ($name:ident) => {
        $name!([]);
        $name!([T1]);
        $name!([T1, T2]);
        $name!([T1, T2, T3]);
    };
}

macro_rules! impl_handler {
    (
        [$($ty:ident),*]
    ) => {
        impl<F, $($ty,)*> Handler<($($ty,)*)> for F
        where
            F: Fn($($ty,)*),
            $( $ty: FromContext,)*
        {
            fn call(self, context: Context) {
                self($( $ty::from_context(&context), )*);
            }
        }
    };
}

all_the_tuples!(impl_handler);

fn trigger<T, H>(context: Context, handler: H)
where
    H: Handler<T>,
{
    handler.call(context);
}

fn print_id(id: Id) {
    println!("id is {}", id.0);
}

fn print_param(Param(param): Param) {
    println!("id is {}", param);
}

fn print_all(Param(param): Param, Id(id): Id) {
    println!("param is {param}, id is {id}");
}

pub fn main() {
    let context = Context::new("magic".into(), 33);
    trigger(context.clone(), print_id);
    trigger(context.clone(), print_param);
    trigger(context.clone(), print_all);
}
相关推荐
QMCY_jason4 小时前
Ubuntu 安装RUST
linux·ubuntu·rust
碳苯8 小时前
【rCore OS 开源操作系统】Rust 枚举与模式匹配
开发语言·人工智能·后端·rust·操作系统·os
zaim110 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
凌云行者20 小时前
使用rust写一个Web服务器——单线程版本
服务器·前端·rust
cyz1410011 天前
vue3+vite@4+ts+elementplus创建项目详解
开发语言·后端·rust
超人不怕冷1 天前
[rust]多线程通信之通道
rust
逢生博客1 天前
Rust 语言开发 ESP32C3 并在 Wokwi 电子模拟器上运行(esp-hal 非标准库、LCD1602、I2C)
开发语言·后端·嵌入式硬件·rust
Maer091 天前
WSL (Linux)配置 Rust 开发调试环境
linux·运维·rust
白总Server1 天前
CNN+Transformer在自然语言处理中的具体应用
人工智能·神经网络·缓存·自然语言处理·rust·cnn·transformer
凌云行者1 天前
使用rust写一个Web服务器——async-std版本
服务器·前端·rust