#async #macro

过程宏 extension-fn

扩展函数定义无需样板代码

4个稳定版本

1.2.0 2023年5月19日
1.1.1 2023年5月18日
1.0.0 2023年4月12日

1477过程宏

Download history 6/week @ 2024-04-04

每月下载量 68

MITGPL-3.0 许可

10KB
142 代码行

本crate提供了用于扩展类型的扩展函数宏 extension_fn。

示例

例如,有一个针对 str 的 count_numbers 扩展函数

/// Replacement for:
/// Sealed is internal trait that used to provide only one implementation of trait and nobody outside module can implement this
/// pub trait CountNumbers: Sealed {
///     fn count_numbers(&self) -> u32;
/// }
/// impl CountNumbers for str {
///     fn count_numbers(&self) -> u32 { ... }
/// }
#[extension_fn(str)]
pub fn count_numbers(&self) -> u32 {
     self.chars().fold(0, |count, char| {
        if char.is_numeric() {
         count + 1
        } else {
           count
        }
    })
}

你可以通过添加 async-trait 到你的依赖中,使用异步函数进行扩展

[dependencies]
async-trait = "*"

你也可以扩展符合trait约束的类型

#[extension_fn(trait AsRef<str>)]
pub fn count_numbers(&self) { ... }

lib.rs:

扩展函数定义无需样板代码。

依赖项

~270–720KB
~17K SLoC