fnaop

Crates.iofnaop
lib.rsfnaop
version0.1.0
sourcesrc
created_at2024-07-06 18:52:07.380882
updated_at2024-07-06 18:52:07.380882
descriptionfnaop is a lightweight and flexible Rust library designed to bring Aspect-Oriented Programming (AOP) to your Rust functions. By using fnaop, you can easily add pre and post function logic without modifying the core functionality of your functions, enabling cleaner and more maintainable code.
homepagehttps://github.com/photowey/fnaop
repositoryhttps://github.com/photowey/fnaop
max_upload_size
id1294290
size31,288
changjun (photowey)

documentation

https://docs.rs/fnaop

README

fnaop

fnaop is a lightweight and flexible Rust library designed to bring Aspect-Oriented Programming (AOP) to your Rust functions. By using fnaop, you can easily add pre and post function logic without modifying the core functionality of your functions, enabling cleaner and more maintainable code.

APIs Documents

changelog

1.Usage

Add this to your Cargo.toml:

[dependencies]
fnaop = "0.1"

2.APIs

Aspect: An attribute macro for applying Aspect-Oriented Programming (AOP) to functions. The Aspect macro allows you to specify before and after functions that will be called before and after the target function respectively. This is useful for encapsulating cross-cutting concerns such as logging, metrics, or other side effects.

2.1.Noarmal

Ordinary function, adapted to 0 or more ordinary parameters.

#[Aspect(before = "before_fn")]
pub fn say_hello(x: i64) {
    println!("Hello:say_hello, {}", x);
}

#[Aspect(after = "after_fn")]
pub fn say_hello(x: i64) {
    println!("Hello:say_hello, {}", x);
}

#[Aspect(before = "before_fn", after = "after_fn")]
pub fn say_hello(x: i64) {
    println!("Hello:say_hello, {}", x);
}

// ----------------------------------------------------------------

#[Aspect(before = "before_fn_empty", after = "before_fn_empty")]
pub fn say_hello_empty() {
    println!("Hello:say_hello_empty");
}

2.2.Lifetime

Functions with lifetime parameters.

#[Aspect(before = "before::struct_before_fn_lifetime", after = "after::struct_after_fn_lifetime")]
pub fn say_hello_struct_lifetime<'a>(ctx: LifetimeHelloContext<'a>) {
    println!("struct::Hello, {} {}", ctx.x, ctx.y);
}

2.3.Return value

Functions with return values.

#[Aspect(before = "before::struct_before_fn_lifetime", after = "after::struct_after_fn_lifetime")]
pub fn say_hello_struct_lifetime_with_return<'a>(ctx: LifetimeHelloContext<'a>) -> i64 {
    println!("struct::Hello, {} {}", ctx.x, ctx.y);

    *ctx.x
}

2.4.Others

// @see integration_tests.rs
Commit count: 2

cargo fmt