Crates.io | extension-fn |
lib.rs | extension-fn |
version | 1.2.0 |
source | src |
created_at | 2023-04-12 17:35:17.532085 |
updated_at | 2023-05-19 16:31:57.735772 |
description | No boilerplate code for extension function definitions |
homepage | |
repository | https://github.com/AlexSherbinin/extension-fn |
max_upload_size | |
id | 837101 |
size | 12,122 |
This crate provides the extension_fn macro for extending types with extension functions.
For example there is a count_numbers extension function for str:
/// 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
}
})
}
You can extend using async functions by adding async-trait to your dependencies:
[dependencies]
async-trait = "*"
Also you can extend types that matching trait bound:
#[extension_fn(trait AsRef<str>)]
pub fn count_numbers(&self) { ... }