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.
Add this to your Cargo.toml:
[dependencies]
fnaop = "0.1"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.
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");
}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);
}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
}…
// @see integration_tests.rs