< 返回版块

eric642 发表于 2025-07-05 16:31

pub trait UserCommunication {
    async fn send_sms(self: &Self, phone: &str, code: &str);
    async fn send_email(self: &Self, email: &str, code: &str);
}

pub struct AuthenticationService {
    communicator: Box<dyn UserCommunication>,
}

如上代码,希望communicator是个动态分发的trait,不咋想用async-trait实现

评论区

写评论
苦瓜小仔 2025-07-06 00:12

我今天发的 Rust 日报有介绍 dynify

你的示例已经作为了它的一个 example

其他方式见我总结的 dyn-afit

1 共 1 条评论, 1 页