extension-fn

Crates.ioextension-fn
lib.rsextension-fn
version1.2.0
sourcesrc
created_at2023-04-12 17:35:17.532085
updated_at2023-05-19 16:31:57.735772
descriptionNo boilerplate code for extension function definitions
homepage
repositoryhttps://github.com/AlexSherbinin/extension-fn
max_upload_size
id837101
size12,122
Alex (AlexSherbinin)

documentation

https://docs.rs/extension-fn

README

This crate provides the extension_fn macro for extending types with extension functions.

Example

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) { ... }
Commit count: 6

cargo fmt